1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/examples/telebot_bot/telebot_bot.py

85 lines
3.0 KiB
Python
Raw Normal View History

2015-09-02 00:20:24 +03:00
# -*- coding: utf-8 -*-
# This bot was made specifically for the pyTelegramAPI Telegram chat,
# and goes by the name 'TeleBot (@pyTeleBot)'. Join our group to talk to him!
# WARNING: Tested with Python 2.7
import os
2018-08-17 12:54:26 +03:00
import telebot
2015-09-02 00:20:24 +03:00
text_messages = {
'welcome':
u'Please welcome {name}!\n\n'
u'This chat is intended for questions about and discussion of the pyTelegramBotAPI.\n'
u'To enable group members to answer your questions fast and accurately, please make sure to study the '
u'project\'s documentation (https://github.com/eternnoir/pyTelegramBotAPI/blob/master/README.md) and the '
u'examples (https://github.com/eternnoir/pyTelegramBotAPI/tree/master/examples) first.\n\n'
u'I hope you enjoy your stay here!',
'info':
u'My name is TeleBot,\n'
u'I am a bot that assists these wonderful bot-creating people of this bot library group chat.\n'
u'Also, I am still under development. Please improve my functionality by making a pull request! '
u'Suggestions are also welcome, just drop them in this group chat!',
'wrong_chat':
u'Hi there!\nThanks for trying me out. However, this bot can only be used in the pyTelegramAPI group chat.\n'
u'Join us!\n\n'
u'https://telegram.me/joinchat/067e22c60035523fda8f6025ee87e30b'
}
if "TELEBOT_BOT_TOKEN" not in os.environ or "GROUP_CHAT_ID" not in os.environ:
raise AssertionError("Please configure TELEBOT_BOT_TOKEN and GROUP_CHAT_ID as environment variables")
bot = telebot.AsyncTeleBot(os.environ["TELEBOT_BOT_TOKEN"])
GROUP_CHAT_ID = int(os.environ["GROUP_CHAT_ID"])
2015-09-02 00:20:24 +03:00
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
def is_api_group(chat_id):
2018-08-17 13:01:03 +03:00
return chat_id == GROUP_CHAT_ID
2015-09-02 00:20:24 +03:00
@bot.message_handler(func=lambda m: True, content_types=['new_chat_participant'])
def on_user_joins(message):
if not is_api_group(message.chat.id):
return
name = message.new_chat_participant.first_name
if hasattr(message.new_chat_participant, 'last_name') and message.new_chat_participant.last_name is not None:
2015-09-02 00:20:24 +03:00
name += u" {}".format(message.new_chat_participant.last_name)
if hasattr(message.new_chat_participant, 'username') and message.new_chat_participant.username is not None:
2015-09-02 00:20:24 +03:00
name += u" (@{})".format(message.new_chat_participant.username)
bot.reply_to(message, text_messages['welcome'].format(name=name))
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
@bot.message_handler(commands=['info', 'help'])
def on_info(message):
if not is_api_group(message.chat.id):
bot.reply_to(message, text_messages['wrong_chat'])
return
bot.reply_to(message, text_messages['info'])
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
@bot.message_handler(commands=["ping"])
def on_ping(message):
bot.reply_to(message, "Still alive and kicking!")
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
@bot.message_handler(commands=['start'])
def on_start(message):
if not is_api_group(message.chat.id):
bot.reply_to(message, text_messages['wrong_chat'])
return
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
def listener(messages):
for m in messages:
print(str(m))
2015-09-02 00:20:24 +03:00
2018-08-17 13:01:03 +03:00
2015-09-02 00:20:24 +03:00
bot.set_update_listener(listener)
2021-09-28 19:17:09 +03:00
bot.infinity_polling()