{"id":20429238,"url":"https://github.com/antirez/micropython-telegram-bot","last_synced_at":"2025-05-08T17:32:23.139Z","repository":{"id":264985539,"uuid":"874631544","full_name":"antirez/micropython-telegram-bot","owner":"antirez","description":"MicroPython telegram bot library: simple way to put your IoT projects on the cloud","archived":false,"fork":false,"pushed_at":"2024-10-19T16:25:24.000Z","size":13,"stargazers_count":37,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-04T13:59:20.512Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antirez.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-10-18T07:18:58.000Z","updated_at":"2025-04-12T15:00:24.000Z","dependencies_parsed_at":"2024-11-27T06:31:20.600Z","dependency_job_id":null,"html_url":"https://github.com/antirez/micropython-telegram-bot","commit_stats":null,"previous_names":["antirez/micropython-telegram-bot"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antirez%2Fmicropython-telegram-bot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antirez%2Fmicropython-telegram-bot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antirez%2Fmicropython-telegram-bot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antirez%2Fmicropython-telegram-bot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antirez","download_url":"https://codeload.github.com/antirez/micropython-telegram-bot/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253114686,"owners_count":21856548,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-15T08:00:43.838Z","updated_at":"2025-05-08T17:32:23.131Z","avatar_url":"https://github.com/antirez.png","language":"Python","readme":"This code implements a very simple non-blocking Telegram bot library\nfor MicroPython based MCUs such as the ESP32 and similar microcontrollers.\n\nQuick facts about how this can be useful and how it is implemented:\n\n* **What you can do with this code?** You can implement bots that run into microcontrollers so that you can control your projects / IoT devices via Telegram.\n* **What the library implements?** It calls your callback when the bot receives a message, and then you have an API to send messages. Just that. No advanced features are supported.\n* **This implementation has limits.** The code uses non blocking sockets. It cut corners in order to be simple and use few memory, it may break, it's not a technically super \"sane\" implementation, but it is extremely easy to understand and modify.\n* The code is BSD licensed.\n* The MicroPython JSON library does not translate surrogate UTF-16 characters, so this library implements a quick and dirty conversion to UTF-8.\n\n## How to test it?\n\n1. Create your bot using the Telegram [@BotFather](https://t.me/botfather).\n2. After obtaining your bot API key, edit the `example.py` file and put there your API key (also called *token*). Make sure to also put your WiFi credentials, as the microcontroller needs to connect to the Internet for this to work.\n3. Put the `telegram.py` file into the device flash memory with:\n\n    mp cp telegram.py :\n\n4. Execute the example with:\n\n    mp run example.y\n\n5. Talk to your bot. It will echo what you write to it.\n6. If your bot is inside a group, make sure to give it admin privileges, otherwise it will be unable to get messages.\n\n## How to use the API?\n\nRun the bot in its own coroutine with:\n\n\n```python\nbot = TelegramBot(Token,mycallback)\nbot.connect_wifi(WifiNetwork, WifiPassword)\nasyncio.create_task(bot.run())\nloop = asyncio.get_event_loop()\nloop.run_forever()\n```\n\nThe callback looks like that:\n\n\n```python\ndef mycallback(bot,msg_type,chat_name,sender_name,chat_id,text,entry):\n    print(msg_type,chat_name,sender_name,chat_id,text)\n    bot.send(sender_id,\"Ehi! \"+text)\n```\n\nThe arguments it receives are:\n\n* `msg_type` is private, group, supergroup, channel, depending on the entity that sent us the message.\n* `chat_name` Group/Channel name if the message is not a private message. Otherwise `None`.\n* `sender_name` is the Telegram username of the caller, or the name of the group/channel.\n* `chat_id` is the Telegram ID of the user/chat: this ID is specific of the user/group/channel. You need to use this ID with the `.send()` method to reply in the same place the message arrived to you.\n* `text` is the content of the message. UTF-8 encoded.\n* `entry` is the raw JSON entry received from Telegram. From there you can take all the other stuff not directly passed to the function.\n\nThe only two methods you can call are:\n\n1. `.send()`, with the ID of the recipient and your text message. A third optional argument called **glue** can be `True` or `False`. By default it is `False`. When it is `True`, messages having the same target ID as the previous message are *glued* together, up to 2k of text, so we can avoid sending too many messages via the API.\n2. `.stop()` that will just terminate the task handling the bot. This should be called before discarding the `TelegramBot` object.\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Communications"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantirez%2Fmicropython-telegram-bot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantirez%2Fmicropython-telegram-bot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantirez%2Fmicropython-telegram-bot/lists"}