Creating a TeleBot
Getting Started
Open up the IDE of your choice (PyCharm, VS Code, Google Colab, etc.)
Locate the terminal - usually found in a panel at the bottom or side of your IDE
Install the telebot package by typing
pip install python-telegram-bot
Note: Colab uses ! as a prefix to shell commandsRun the command
Link to the Library: https://github.com/python-telegram-bot/python-telegram-bot
Creating a Bot
Launch Telegram
Search for the user @BotFather

Create a new bot by using the command '/start '
Follow the prompts and set the bot's name and username Username must end with "bot" -> e.g. 'untitled_bot'
Receive your bot token: BotFather will provide you with a unique token that will be used to authenticate your bot

Keep your token secure and do not share it publicly
Input token into your environment
# Replace "YOUR_API_TOKEN" with token received from BotFather
token = "YOUR_API_TOKEN"
(Optional) Add a description, profile picture, etc.
Import Packages
We will be creating the telebot in the context of Google Colab!
So, let's get started! Paste the following code blocks into your file:
Allow for asynchronous programming capabilities
# Workaround for nested event loops in Jupyter (Google Colab)
# Jupyter notebooks already have a running loop via Tornado and the asyncio lib does not allow a nested loop, we need to use a separate package called nest_asyncio
import asyncio
import nest_asyncio
nest_asyncio.apply()
# for non-Jupyter notebook IDE's
import asyncio
Enable logging
import logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
This is setting up logging
module, so you know when (and why) things don't work as expected.
Click here for more info.
Import from telegram package
from telegram import Update
from telegram.constants import ParseMode
from telegram import InlineQueryResultArticle, InputTextMessageContent
from telegram.ext import filters, MessageHandler, ApplicationBuilder, CommandHandler, ContextTypes, InlineQueryHandler
(Optional) Other useful packages
import math
from random import random
import datetime
Last updated