Created
November 10, 2021 03:03
-
-
Save CristoJV/446a9af9c26cccfc0fa1d2b72d394337 to your computer and use it in GitHub Desktop.
Telebot.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Telebot.ipynb", | |
"provenance": [], | |
"collapsed_sections": [], | |
"authorship_tag": "ABX9TyNthb8QiZJgrWMaXc+9ILTt", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/CristoJV/446a9af9c26cccfc0fa1d2b72d394337/telebot.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Zhc0taDiZtxE" | |
}, | |
"source": [ | |
"# Install Python-telegram-bot library\n", | |
"[Here](https://python-telegram-bot.readthedocs.io/en/latest/index.html) you have the lastest documentation, including guides, tutorials and examples.\n", | |
"\n", | |
"[Here](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Bots-built-with-PTB) you can find different examples built by the community\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "4fpTMeVmOlXf" | |
}, | |
"source": [ | |
"!pip install python-telegram-bot" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "NydPsCMCaO4g" | |
}, | |
"source": [ | |
"## Import the module" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XX_zHHtIQdVd" | |
}, | |
"source": [ | |
"from telegram import Bot\n", | |
"from telegram.error import TelegramError" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "P73ecYcSyx2P" | |
}, | |
"source": [ | |
"## Get the bot token\n", | |
"The bot ```token``` is obtained after creating a new bot with the BotFather.\n", | |
"\n", | |
"To create the bot use the command ```/newbot```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "tnrcg5JIy6CA" | |
}, | |
"source": [ | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "cmxlrO8yzEb0" | |
}, | |
"source": [ | |
"## Get the chat id\n", | |
"For the chat id you need to create a chat with the new bot using the /start command. Then, access ```https://api.telegram.org/bot<Insert here your token>/getUpdates``` to get your ```chat_id```\n", | |
"\n", | |
"In the json response you will find the ```chat id```" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "apvbNtZfy-5m" | |
}, | |
"source": [ | |
"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "r0oh6oB9Oouc" | |
}, | |
"source": [ | |
"token='<Insert your bot token>'\n", | |
"chat_id='<Insert your chat id>'" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "Fqm0NZ7hbVvO" | |
}, | |
"source": [ | |
"## Create a bot and send your \"Hello world\" Message" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "y9E9gh6xQU_V" | |
}, | |
"source": [ | |
"bot = Bot(token)\n", | |
"try: msg = bot.send_message(chat_id, text='Hello World')\n", | |
"except TelegramError as e: print(e)" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "JYrJ9x7uzIYn" | |
}, | |
"source": [ | |
"# Congratulations!" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "xwrZWpm9zKkO" | |
}, | |
"source": [ | |
"" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "oS5lGBXLbeI_" | |
}, | |
"source": [ | |
"## Do not forget to close bot if it is not needed" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "-0LECJmIZZ0r" | |
}, | |
"source": [ | |
"try:\n", | |
" bot.close()\n", | |
"except telegram.error.RetryAfter as e:\n", | |
" print(TAG, \"\", \"Closing bot too early. It should not be closed in the first 10 minutes after the bot was launched.\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "W3t49fgFbqy8" | |
}, | |
"source": [ | |
"## (Optional) Create a context manager" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "wetsvymFUb5f" | |
}, | |
"source": [ | |
"\"Context Manager approach\"\n", | |
"\n", | |
"class TelegramBot(object):\n", | |
"\n", | |
" def __init__(self, token, chat_id):\n", | |
" self._token=token\n", | |
" self._chat_id=chat_id\n", | |
"\n", | |
" def __enter__(self):\n", | |
" self._bot = telegram.Bot(self._token, request=telegram.utils.request.Request(read_timeout=2))\n", | |
" return self\n", | |
"\n", | |
" def send_message(self, text, **kwargs):\n", | |
" self._bot.send_message(self._chat_id, text, **kwargs)\n", | |
"\n", | |
" def __exit__(self, exc_type, exc_val, exc_tb):\n", | |
" try:\n", | |
" self._bot.close()\n", | |
" except telegram.error.RetryAfter as e:\n", | |
" print(TAG, \"\", \"Closing bot too early. It should not be closed in the first 10 minutes after the bot was launched.\")\n", | |
"\n", | |
"with TelegramBot(token, chat_id) as telebot:\n", | |
" telebot.send_message(\"Hello\")" | |
], | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "tbIto0lqVd3u" | |
}, | |
"source": [ | |
"" | |
], | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment