2020-07-16 19:17:57 +02:00
|
|
|
#!/usr/bin/env python
|
2021-10-08 08:17:00 +02:00
|
|
|
# pylint: disable=global-statement
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Simple Bot to reply to Telegram messages.
|
|
|
|
|
2021-02-01 17:59:39 +01:00
|
|
|
This is built on the API wrapper, see echobot.py to see the same example built
|
2020-07-16 19:17:57 +02:00
|
|
|
on the telegram.ext bot framework.
|
|
|
|
This program is dedicated to the public domain under the CC0 license.
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
import asyncio
|
2020-07-16 19:17:57 +02:00
|
|
|
import logging
|
2020-10-31 16:33:34 +01:00
|
|
|
from typing import NoReturn
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
from telegram import Bot
|
|
|
|
from telegram.error import NetworkError, Forbidden
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
|
|
|
|
)
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def main() -> NoReturn:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Run the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Here we use the `async with` syntax to properly initialize and shutdown resources.
|
|
|
|
async with Bot("TOKEN") as bot:
|
|
|
|
# get the first pending update_id, this is so we can skip over it in case
|
|
|
|
# we get a "Forbidden" exception.
|
2020-07-16 19:17:57 +02:00
|
|
|
try:
|
2022-04-24 12:38:09 +02:00
|
|
|
update_id = (await bot.get_updates())[0].update_id
|
|
|
|
except IndexError:
|
|
|
|
update_id = None
|
|
|
|
|
|
|
|
logger.info("listening for new messages...")
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
update_id = await echo(bot, update_id)
|
|
|
|
except NetworkError:
|
|
|
|
await asyncio.sleep(1)
|
|
|
|
except Forbidden:
|
|
|
|
# The user has removed or blocked the bot.
|
|
|
|
update_id += 1
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def echo(bot: Bot, update_id: int) -> int:
|
2020-07-16 19:17:57 +02:00
|
|
|
"""Echo the message the user sent."""
|
|
|
|
# Request updates after the last update_id
|
2022-04-24 12:38:09 +02:00
|
|
|
updates = await bot.get_updates(offset=update_id, timeout=10)
|
|
|
|
for update in updates:
|
|
|
|
next_update_id = update.update_id + 1
|
2020-07-16 19:17:57 +02:00
|
|
|
|
2021-05-27 20:34:58 +02:00
|
|
|
# your bot can receive updates without messages
|
|
|
|
# and not all messages contain text
|
|
|
|
if update.message and update.message.text:
|
|
|
|
# Reply to the message
|
2022-04-24 12:38:09 +02:00
|
|
|
logger.info("Found message %s!", update.message.text)
|
|
|
|
await update.message.reply_text(update.message.text)
|
|
|
|
return next_update_id
|
|
|
|
return update_id
|
2020-07-16 19:17:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-04-24 12:38:09 +02:00
|
|
|
try:
|
|
|
|
asyncio.run(main())
|
|
|
|
except KeyboardInterrupt: # Ignore exception when Ctrl-C is pressed
|
|
|
|
pass
|