{"id":13801031,"url":"https://github.com/gabrielebarola/telegram-upy","last_synced_at":"2025-05-13T10:31:04.621Z","repository":{"id":43318227,"uuid":"370355109","full_name":"gabrielebarola/telegram-upy","owner":"gabrielebarola","description":"Telegram API wrapper for micropython","archived":false,"fork":false,"pushed_at":"2022-03-08T16:54:22.000Z","size":69,"stargazers_count":43,"open_issues_count":1,"forks_count":8,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-08-04T00:05:47.528Z","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":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gabrielebarola.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}},"created_at":"2021-05-24T13:07:12.000Z","updated_at":"2024-07-11T22:25:37.000Z","dependencies_parsed_at":"2022-09-06T05:22:34.946Z","dependency_job_id":null,"html_url":"https://github.com/gabrielebarola/telegram-upy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielebarola%2Ftelegram-upy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielebarola%2Ftelegram-upy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielebarola%2Ftelegram-upy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gabrielebarola%2Ftelegram-upy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gabrielebarola","download_url":"https://codeload.github.com/gabrielebarola/telegram-upy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225198966,"owners_count":17437008,"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-08-04T00:01:18.760Z","updated_at":"2024-11-18T15:31:29.291Z","avatar_url":"https://github.com/gabrielebarola.png","language":"Python","funding_links":["https://www.paypal.me/gabrielebarola"],"categories":["Libraries"],"sub_categories":["Communications"],"readme":"# Telegram-uPy\nTelegram API wrapper for micropython, built for ESP32, cannot verify support for other MCUs\n\n---\n# INSTALLING\nClone the repository:\n\n```bash\ngit clone https://github.com/gabrielebarola/telegram-upy.git\n```\n\nupload the **utelegram.py** file to your board using your favourite software (i use ampy):\n\n```bash\nampy -b 115200 -p /dev/ttyUSB0 put path/to/utelegram.py\n```\n\nInstall urequests library using upip in repl\n\n```python\nimport upip\n\nupip.install('urequests')\n```\n\n---\n# USAGE\nYou can find simple examples in the **examples** folder.\n## Creating the bot\n```python\nfrom utelegram import Bot\n\nTOKEN = 'your-bot-token-12345'\n\nbot = Bot(TOKEN)\n```\n\nBot token is provided by **BotFather** when creating a new bot on the telegram client\n\n## Adding command handlers\nYou can create functions that are triggered when a **command** (message starting with '/') is sent to the bot.\n\n\nFor example let's write a function that replies \"hello\" when **/start** is sent to the bot\n\n```python\n@bot.add_command_handler('start')\ndef start(update):\n    update.reply('hello')\n```\n\n**Every function used as a handler should take the update as an argument**\n\n## Adding message handlers\nYou can also create functions triggered when a message that matches a **regular expression** is sent to the bot.\n\nIf you need a regex cheat sheet you can find it here https://www.w3schools.com/python/python_regex.asp\n\nFor example let's write a function that replies \"hello\" when a message starting with **\"Hi\"** is sent is sent to the bot\n\n```python\n@bot.add_message_handler('^Hi')\ndef hello(update):\n    update.reply('hello')\n```\n\n**The regular expression must be given to the decorator as argument**\n\n## Using custom keyboards\nYou can define custom keyboards to send with a reply\n```python\nfrom utelegram import ReplyKeyboardMarkup, KeyboardButton\n\nkeyboard = [\n    [KeyboardButton('Btn1')], #each list is a row, each element is a column\n    [KeyboardButton('Btn2'), KeyboardButton('Btn3')],\n]\n\nreply_keyboard = ReplyMarkupKeyboard(keyboard) #pass your array as the keyboard\n\n@bot.add_message_handler('^Show keyboard')\ndef show(update):\n    update.reply('here it is!', reply_markup=reply_keyboard)\n```\n\n## Using conversations\nA conversation is a multi-step handler with subhandlers.\nTo create a conversation:\n\n```python\nfrom utelegram import Conversation\n\nc = Conversation(['STEP1','STEP2']) #ENTRY STEP IS DEFAULT\n```\n\nHandlers can be added similarly to the bot ones:\n```python\n@c.add_command_handler('ENTRY', '/started')\ndef do(update):\n    update.reply('moving to step 1')\n    return 'STEP1'\n\n@c.add_command_handler('STEP1', '/dosomething')\ndef do(update):\n    update.reply('did it!')\n    return 'STEP2'\n\n@c.add_message_handler('STEP2', '^end$')\ndef end(update):\n    update.reply('ending')\n    return c.END\n```\n\n**An handler must always return the next step you want to take in the conversation, use c.END to end the conversation**\n\nAdd the conversation to the bot:\n\n```python\nbot.add_conversation_handler(c)\n```\n\n## Starting the bot loop\n\n```python\nbot.start_loop()\n```\n\nif you want to use another function in parallel with the bot do it this way:\n\n```python\ndef main(text):\n    while True:\n        print(text)\n\t\nbot.start_loop(main, (text,))\n```\n\nif your function has arguments pass them as a touple\n\n\n# Donating\nIf you appreciate my work and want to donate, you can do it with PayPal at https://www.paypal.me/gabrielebarola\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabrielebarola%2Ftelegram-upy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgabrielebarola%2Ftelegram-upy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgabrielebarola%2Ftelegram-upy/lists"}