Created
December 27, 2018 04:21
-
-
Save lbellomo/1002b02c63dfbc9a0cd879007c8bac06 to your computer and use it in GitHub Desktop.
A Simple Welcome Telegram Bot that gives simple (configurable) welcome messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''A Simple Welcome Telegram Bot that gives simple (configurable) welcome messages''' | |
import os | |
import json | |
from telegram.ext import Updater, CommandHandler, MessageHandler, BaseFilter | |
# link to mwt file: https://gist.github.com/jh0ker/56f5b4fb7d015b1b9e4c74d4a91d4568 | |
from mwt import MWT | |
def read_json(path_json): | |
'''Lee el json con los mensajes''' | |
with open(path_json, 'r') as f: | |
new_dict = json.load(f) | |
return new_dict | |
def save_json(path_json, dict_object): | |
'''Salva a disco los mensajes''' | |
with open(path_json, 'w') as f: | |
json.dump(dict_object, f) | |
# sacado de 'code-snippers': | |
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#get-the-add-group-message | |
class NewMember(BaseFilter): | |
def filter(self, message): | |
if not message.new_chat_members: | |
return False | |
return True | |
def get_group_message(chat_id): | |
'''Si existe devuelve el mensaje, sino el defaut mensaje''' | |
chat_id = str(chat_id) | |
if chat_id not in msjs.keys(): | |
msj = defaut_msj | |
else: | |
msj = msjs[chat_id] | |
return msj | |
def add_group(bot, update): | |
chat_id = update.message.chat_id | |
msj = get_group_message(chat_id) | |
for members in update.message.new_chat_members: | |
bot.send_message(update.message.chat_id, | |
text=msj) | |
# https://github.com/python-telegram-bot/python-telegram-bot/wiki/Code-snippets#cached-telegram-group-administrator-check | |
@MWT(timeout=60*60) | |
def get_admin_ids(bot, chat_id): | |
"""Returns a list of admin IDs for a given chat. Results are cached for 1 hour.""" | |
return [admin.user.id for admin in bot.get_chat_administrators(chat_id)] | |
def change_message(bot, update): | |
'''Guarda un nuevo mensaje, si tiene los permisos para hacerlo''' | |
chat_id = str(update.message.chat_id) | |
all_admin = update.message.chat.all_members_are_administrators | |
is_a_admin = update.message.from_user.id in get_admin_ids(bot, chat_id) | |
if all_admin or is_a_admin: | |
update_msj = update.message.text.split(maxsplit=1)[1] | |
msjs[chat_id] = update_msj | |
save_json(path_json, msjs) | |
bot.send_message(chat_id=chat_id, text="Welcome message update") | |
else: | |
bot.send_message(chat_id=chat_id, text="You need be a admin to change the message") | |
def print_message(bot, update): | |
'''Muestra el mensaje acutal del grupo''' | |
chat_id = update.message.chat_id | |
msj = get_group_message(chat_id) | |
bot.send_message(chat_id=chat_id, text=msj) | |
def help_message(bot, update): | |
'''Muestra una help basica''' | |
help_msj = '''Show welcome messages when a user join a group. | |
Only the group admin could change it. | |
Usage: | |
/change_message new_mesage - Change the welcome group message | |
/print_message - Display the current welcome group message | |
/help - Display this help message | |
Example: | |
/change_message My awesome new welcome message!!''' | |
bot.send_message(chat_id=update.message.chat_id, text=help_msj) | |
with open('secret') as f: | |
token = f.readline().strip() | |
path_json = 'msjs.json' | |
if os.path.isfile(path_json): | |
msjs = read_json(path_json) | |
else: | |
msjs = dict() | |
defaut_msj = 'Welcome!' | |
updater = Updater(token=token) | |
dispatcher = updater.dispatcher | |
add_group_handle = MessageHandler(callback=add_group, filters=NewMember()) | |
dispatcher.add_handler(add_group_handle) | |
change_message_handler = CommandHandler('change_message', change_message) | |
dispatcher.add_handler(change_message_handler) | |
print_message_handler = CommandHandler('print_message', print_message) | |
dispatcher.add_handler(print_message_handler) | |
help_handler = CommandHandler('help', help_message) | |
dispatcher.add_handler(help_handler) | |
updater.start_polling() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment