python Telegram bot pytelegrambotapi telegram bot simple – Telegram Group

Build your first telegram bot in python

What are Telegram bots..?

Bots are simply Telegram accounts operated by software — not people — and they’ll often have AI features. They can do anything — teach, play, search, broadcast, remind, connect, integrate with other services, or even pass commands to the Internet of Things.

We will build a password generator bot in python.

Before we start to build a bot. We need to create/register a bot in the telegram.

  • Go to @BotFather
  • Send the /newbot command .which leads to create a new bot.
  • Set the name and username for your bot.
  • BotFather will send the Bot API token.
ScreenShot : Create/Register a chatbot with bot father
  • Create Virtual Environment
python3 -m venv .venv
  • Activate Virtual Environment

For Linux :

source .venv/bin/activate

For Windows:

.venv\Scripts\Activate
  • Install required Libraries

Libraries we need for our bot –

pyTelegramBotAPI: A simple, but extensible Python implementation for the Telegram Bot API.

pip install pyTelegramBotAPI
  • Import Require Libaries
import telebot
import string
from random
  • Create an instance of the TeleBot class
bot = telebot.TeleBot("TOKEN")

Note: Replace TOKEN with your own API token.

  • Define a message handler that handles incoming /start command
@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message,f"Hello,{message.chat.username}")
  • Define another message handler that handles /genrate_password command

When users send this command to bot. The bot will send Generated password string

@bot.message_handler(commands=['genrate_password'])
def genrate_password(message): characters = string.ascii_letters + string.punctuation + string.digits#Generate random charecter password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
bot.send_message(message.chat.id,f"Genarated Password {password}")
  • Start the bot using polling
bot.polling()
  • Our code file looks like
import telebot
import string
from randombot = telebot.TeleBot("TOKEN")@bot.message_handler(commands=['start'])
def send_welcome(message):
bot.reply_to(message,f"Hello,{message.chat.username}")@bot.message_handler(commands=['genrate_password'])
def genrate_password(message):characters = string.ascii_letters + string.punctuation + string.digits#Generate random charecterpassword = "".join(random.choice(characters) for x in range(random.randint(8, 16)))bot.send_message(message.chat.id,f"Genarated Password {password}")bot.polling()
  • Run the code
python filename.py

I hope you enjoy building your first Telegram bot.

Ten articles before and after

How I Connected with 10,000 people over a couple of weeks – Telegram Group

Written in collaboration with Sushil Shaw – Telegram Group

Stream Tweets to a Telegram channel – Telegram Group

Django Telegram Bot. В данной статье я расскажу о том как… – Telegram Group

data-rh=”true”>Telegram Bot for Lazy – Max Makhrov – Medium – Telegram Group

Bot sederhana dengan menggunakan python! – Telegram Group

Telegram Secret Chat — Enable Now to be Secure – Telegram Group

Create Telegram Chatbot. In January 2022, Telegram announced… – Telegram Group

How to Write a Quick and Easy Telegram Bot that Posts Messages with Python – Telegram Group

Membuat MJPEG Stream Camera Menggunakan ESP32-CAM — Part 2 – Telegram Group