Код: Выделить всё
from typing import Final
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
import sys
# function that implements the message handler
TOKEN: Final = '6925586576:AAG73ASaMl2kMRE5JrurKP6rBeKyURZbfG8'
BOT_USERNAME: Final = '@flissi_bot'
# command
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I'm pizza_bot . I can help you with your shopping needs.")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I'm pizza_bot. tape somthing so i can help you.")
async def custom_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("the a custon command.")
# reponses
def handel_responses(text: str) -> str:
processed: str = text.lower()
if 'hello' in processed:
return 'hello'
if 'how are you' in processed:
return 'I am fine, thank you'
if 'goodbye' in processed:
return 'goodbye'
if 'thank you' in processed:
return 'you are welcome'
return 'I do not understand you'
async def handel_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
message_type: str = update.message.chat.type
text: str = update.message.text
print(f'User ({update.message.chat_id})in {message_type}: "{text}"')
if message_type == 'group':
if BOT_USERNAME in text:
new_text: str = text.replace(BOT_USERNAME, '').strip()
response: str = handel_responses(new_text)
else:
return
else:
response: str = handel_responses(text)
print('Bot', response)
await update.message.reply_text(response)
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f'Update "{update}" caused error "{context.error}"')
if __name__ == '__main__':
print('starting bot...')
app = Application.builder().token(TOKEN).build()
# commands
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('custom', custom_command))
app.add_handler(CommandHandler('help', help_command))
# Messages
app.add_handler(MessageHandler(filters.Text, handel_message))
# Errors
app.add_error_handler(error)
# Poll the bot
print('polling bot...')
app.run_polling(poll_interval=3)
может ли кто-нибудь помочь?
Подробнее здесь: https://stackoverflow.com/questions/784 ... e-telegram