2015-07-12 14:54:03 +02:00
|
|
|
#!/usr/bin/env python
|
2022-05-15 14:08:40 +02:00
|
|
|
# pylint: disable=unused-argument, wrong-import-position
|
2020-07-16 19:17:57 +02:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2017-10-20 20:24:00 +02:00
|
|
|
|
|
|
|
"""
|
2020-07-16 19:17:57 +02:00
|
|
|
Simple Bot to reply to Telegram messages.
|
|
|
|
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
2022-04-24 12:38:09 +02:00
|
|
|
the Application and registered at their respective places.
|
2020-07-16 19:17:57 +02:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Basic Echobot example, repeats messages.
|
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
|
|
|
"""
|
|
|
|
|
2015-07-30 08:04:59 +02:00
|
|
|
import logging
|
2015-07-12 14:54:03 +02:00
|
|
|
|
2022-05-15 14:08:40 +02:00
|
|
|
from telegram import __version__ as TG_VER
|
|
|
|
|
|
|
|
try:
|
|
|
|
from telegram import __version_info__
|
|
|
|
except ImportError:
|
|
|
|
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
|
|
|
|
|
|
|
|
if __version_info__ < (20, 0, 0, "alpha", 1):
|
|
|
|
raise RuntimeError(
|
|
|
|
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
|
|
|
|
f"{TG_VER} version of this example, "
|
2022-06-09 17:22:32 +02:00
|
|
|
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
|
2022-05-15 14:08:40 +02:00
|
|
|
)
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import ForceReply, Update
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
2022-05-05 17:40:22 +02:00
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2023-06-07 22:32:04 +02:00
|
|
|
# set higher logging level for httpx to avoid all GET and POST requests being logged
|
|
|
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
|
|
|
|
2020-07-16 19:17:57 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
# Define a few command handlers. These usually take the two arguments update and
|
2021-03-28 18:53:44 +02:00
|
|
|
# context.
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Send a message when the command /start is issued."""
|
2021-03-28 18:53:44 +02:00
|
|
|
user = update.effective_user
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_html(
|
2022-05-05 17:40:22 +02:00
|
|
|
rf"Hi {user.mention_html()}!",
|
2021-03-28 18:53:44 +02:00
|
|
|
reply_markup=ForceReply(selective=True),
|
|
|
|
)
|
2015-07-12 14:54:03 +02:00
|
|
|
|
2020-07-16 19:17:57 +02:00
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Send a message when the command /help is issued."""
|
2022-05-05 17:40:22 +02:00
|
|
|
await update.message.reply_text("Help!")
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Echo the user message."""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(update.message.text)
|
2016-06-12 15:30:56 +02:00
|
|
|
|
2017-10-20 20:24:00 +02:00
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Start the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
# on different commands - answer in Telegram
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("start", start))
|
|
|
|
application.add_handler(CommandHandler("help", help_command))
|
2020-07-16 19:17:57 +02:00
|
|
|
|
2021-03-28 18:53:44 +02:00
|
|
|
# on non command i.e message - echo the message on Telegram
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
|
2020-07-16 19:17:57 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
2023-06-04 17:11:58 +02:00
|
|
|
application.run_polling(allowed_updates=Update.ALL_TYPES)
|
2015-08-20 19:58:57 +02:00
|
|
|
|
2015-07-12 14:54:03 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if __name__ == "__main__":
|
2015-07-30 08:04:59 +02:00
|
|
|
main()
|