1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#!/usr/bin/env python3
import logging
import os
import datetime
import sys
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Send a message when the command /help is issued."""
await update.message.reply_text("Help!")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Echo the user message."""
if (update.message.text == "hello"):
logging.info("Hello called")
await update.message.reply_text(
'Hello {}. This is Debian autopkgtest. Today is {}.'.format(
update.message.from_user.first_name,
datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
)
)
def main() -> None:
telegramToken = None
if ("TELEGRAM_TOKEN" in os.environ):
telegramToken = os.environ["TELEGRAM_TOKEN"]
else:
# We cannot do actual test because it connects to telegram server.
return
logging.info("TELEGRAM_TOKEN is given. Starting app.")
# Create the Application and pass it your bot's token.
application = Application.builder().token(telegramToken).build()
# on different commands - answer in Telegram
application.add_handler(CommandHandler("help", help_command))
# on non command i.e message - echo the message on Telegram
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
logging.info("Start polling...")
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
if __name__ == "__main__":
main()
|