GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Introduction to the API Introduction to the API
Hinrich Mahler edited this page
The Bot API is exposed via the To generate an Access Token, you have to talk to BotFather and follow a few simple steps (described here). For full details see the official Telegram documentation at Bots: An introduction for developers. You might also find the official tutorial useful for getting to know the principles of working with Telegram API (although Java is used in examples there, you will find a link to equivalent Python code). To get a feeling for the API and how to use it with We first want to create an instance of the import asyncio import telegram async def main(): bot = telegram.Bot("TOKEN") async with bot: print(await bot.get_me()) if __name__ == '__main__': asyncio.run(main()) Here we simply call the API method getMe.
The $ python main.py User(first_name="Toledo's Palace Bot", is_bot=True, username="ToledosPalaceBot", ...) So far so good. Now we can try and actually do something - let's send a message. Important Bots can't initiate conversations with users.
A user must either add them to a group or send them a message first.
People can use Because of that restriction, we'll have to first send a message to the bot.
After we've done that, we can fetch the update by refactoring the async def main(): bot = telegram.Bot("TOKEN") async with bot: updates = (await bot.get_updates())[0] print(updates) The output should now look something like this (we abbreviated the output a bit): $ python main.py Update(message=Message(chat=Chat(first_name='John', id=1234567890, last_name='Doe', ...), from_user=User(first_name='John', id=1234567890, last_name='Doe', ...), text='Hi!', ...), update_id=219017225) We copy the chat id, here async def main(): bot = telegram.Bot("TOKEN") async with bot: await bot.send_message(text='Hi John!', chat_id=1234567890) That's all very nice, but usually you want your bot to actually react to more complex user input. That is, you want to build a chat-bot. Wiki of
Clone this wiki locally |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |