#!/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()
