From 8b02a75a5e9176fbec409d9b12e4486486070484 Mon Sep 17 00:00:00 2001 From: faridik Date: Fri, 23 Jul 2021 18:59:35 +0300 Subject: [PATCH] Add echo to bot --- PythonCode/{bot.start.py => bot.update.py} | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) rename PythonCode/{bot.start.py => bot.update.py} (52%) diff --git a/PythonCode/bot.start.py b/PythonCode/bot.update.py similarity index 52% rename from PythonCode/bot.start.py rename to PythonCode/bot.update.py index 2335524..bfbac14 100644 --- a/PythonCode/bot.start.py +++ b/PythonCode/bot.update.py @@ -1,6 +1,4 @@ -def start(update, context): - context.bot.send_message(chat_id=update.effective_chat.id, text="Hello World! Это не просто копипаста, я вот что-то подправил даже") - +#Подключение к боту from telegram.ext import Updater updater = Updater(token='1903048555:AAFK-d7prKvOvwwXo0IPzeRwJ1ZcuOT5ty8', use_context=True) @@ -10,8 +8,21 @@ import logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) +#Стартовое сообщение (к команде /start) +def start(update, context): + context.bot.send_message(chat_id=update.effective_chat.id, text="Hello World! Это не просто копипаста, я вот что-то подправил даже") + from telegram.ext import CommandHandler start_handler = CommandHandler('start', start) -dispatcher.add_handler(start_handler) +dispatcher.add_handler(start_handler) +#Ответ на не командное сообщение (отвечает тем же сообщением) +def echo(update, context): + context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text) + +from telegram.ext import MessageHandler, Filters +echo_handler = MessageHandler(Filters.text & (~Filters.command), echo) +dispatcher.add_handler(echo_handler) + +#Начало работы бота updater.start_polling() \ No newline at end of file