From efc98c084f2c014302395142334b62899dd3ca9a Mon Sep 17 00:00:00 2001 From: pieter Date: Tue, 1 Sep 2015 23:20:24 +0200 Subject: [PATCH] Added TeleBot bot source --- examples/telebot_bot/telebot_bot.py | 78 +++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 examples/telebot_bot/telebot_bot.py diff --git a/examples/telebot_bot/telebot_bot.py b/examples/telebot_bot/telebot_bot.py new file mode 100644 index 0000000..0d78dfb --- /dev/null +++ b/examples/telebot_bot/telebot_bot.py @@ -0,0 +1,78 @@ +# -*- 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 telebot +import os + +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 = os.environ["GROUP_CHAT_ID"]\ + +def is_api_group(chat_id): + return chat_id== GROUP_CHAT_ID + +@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'): + name += u" {}".format(message.new_chat_participant.last_name) + + if hasattr(message.new_chat_participant, 'username'): + name += u" (@{})".format(message.new_chat_participant.username) + + bot.reply_to(message, text_messages['welcome'].format(name=name)) + +@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']) + +@bot.message_handler(commands=["ping"]) +def on_ping(message): + bot.reply_to(message, "Still alive and kicking!") + +@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 + +def listener(messages): + for m in messages: + print str(m) + +bot.set_update_listener(listener) +bot.polling() + +