def new_message_handler(update): message_content = update['message']['content'].get('text', {}) # we need this because of different message types: photos, files, etc. message_text = message_content.get('text', '').lower() if message_text == 'ping': chat_id = update['message']['chat_id'] print(f'Ping has been received from {chat_id}') tg.send_message( chat_id=chat_id, text='pong', ) tg.add_message_handler(new_message_handler) tg.idle() # blocking waiting for CTRL+C class Hero: def __init__(self, id, username): self.id = id self.money = 10 self.stamina = 5 self.lvl = 1 self.name = username def ch_money(self, value): self.money = self.money + value def ch_stamina(self, value):
from telegram.client import Telegram from json import dumps from secret import * client = Telegram( api_id=API_ID, api_hash=API_HASH, phone=PHONE_NUMBER, database_encryption_key=DBENC, ) client.login() def new_message_handler(update): print(dumps(update['message']['chat_id'], indent=4)) if str(update['message']['chat_id']) == CHAT_ID_SENDER: print(dumps(update, indent=4)) message_content = update['message']['content'].get('text', {}) message_entities = message_content.get('entities', '') message_text = message_content.get('text', {}) print('\n\n\n', 'Encaminhando mensagem', '\n\n\n') client.send_message( chat_id=CHAT_ID_RECEIVER, text=message_text, ) client.add_message_handler(new_message_handler) client.idle()
def command_on_message(update): # check if message is in update if not 'message' in update: return # get message from update message = update['message'] msg_id = message['id'] msg_chat_id = message['chat_id'] msg_content = get_message_as_text(message) if msg_content == '.chat': pretty_print( bmodes.DBG, f"Chat: {colored(f'#{msg_chat_id}', 'magenta')}, Message: {colored(f'#{msg_id}', 'magenta')}" ) if settings['del-edit-detector']['enable']['delete'] or s['del-edit-detector'][ 'enable']['edit']: tg.add_message_handler(detector_on_message) tg.add_update_handler('updateDeleteMessages', detector_on_messages_delete) tg.add_update_handler('updateMessageEdited', on_message_edit) if settings['forwarder']['enable']: tg.add_message_handler(forwarder_on_message) tg.add_message_handler(command_on_message) tg.idle()
user_id = update["message"]["sender_user_id"] new_users = update["message"]["content"]["member_user_ids"] # Kick this user out of the group if user_id in new_users: params = { "chat_id": chat_id, "user_id": user_id, "status": { "@type": "chatMemberStatusBanned" }, } result = tg.call_method("setChatMemberStatus", params=params, block=True) elif msg_type == "messageText": text = update["message"]["content"]["text"]["text"] # Defaut answer for the mandatory command /start if text == "/start": chat_id = update["message"]["chat_id"] msg = "Sorry, I have no powers here. I can only moderate a Telegram location-based group" tg.send_message( chat_id=chat_id, text=msg, ) if __name__ == "__main__": tg.login() tg.add_message_handler(search_and_kick_out_spammers) tg.idle()