rawapibot.py

This example uses only the pure, “bare-metal” API wrapper.

 1#!/usr/bin/env python
 2"""Simple Bot to reply to Telegram messages.
 3
 4This is built on the API wrapper, see echobot.py to see the same example built
 5on the telegram.ext bot framework.
 6This program is dedicated to the public domain under the CC0 license.
 7"""
 8
 9import asyncio
10import contextlib
11import datetime as dtm
12import logging
13from typing import NoReturn
14
15from telegram import Bot, Update
16from telegram.error import Forbidden, NetworkError
17
18logging.basicConfig(
19    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
20)
21# set higher logging level for httpx to avoid all GET and POST requests being logged
22logging.getLogger("httpx").setLevel(logging.WARNING)
23
24logger = logging.getLogger(__name__)
25
26
27async def main() -> NoReturn:
28    """Run the bot."""
29    # Here we use the `async with` syntax to properly initialize and shutdown resources.
30    async with Bot("TOKEN") as bot:
31        # get the first pending update_id, this is so we can skip over it in case
32        # we get a "Forbidden" exception.
33        try:
34            update_id = (await bot.get_updates())[0].update_id
35        except IndexError:
36            update_id = None
37
38        logger.info("listening for new messages...")
39        while True:
40            try:
41                update_id = await echo(bot, update_id)
42            except NetworkError:
43                await asyncio.sleep(1)
44            except Forbidden:
45                # The user has removed or blocked the bot.
46                update_id += 1
47
48
49async def echo(bot: Bot, update_id: int) -> int:
50    """Echo the message the user sent."""
51    # Request updates after the last update_id
52    updates = await bot.get_updates(
53        offset=update_id, timeout=dtm.timedelta(seconds=10), allowed_updates=Update.ALL_TYPES
54    )
55    for update in updates:
56        next_update_id = update.update_id + 1
57
58        # your bot can receive updates without messages
59        # and not all messages contain text
60        if update.message and update.message.text:
61            # Reply to the message
62            logger.info("Found message %s!", update.message.text)
63            await update.message.reply_text(update.message.text)
64        return next_update_id
65    return update_id
66
67
68if __name__ == "__main__":
69    with contextlib.suppress(KeyboardInterrupt):  # Ignore exception when Ctrl-C is pressed
70        asyncio.run(main())