Telegram bot pytelegrambotapi telegram bot simple | Medium – Telegram 安装

Haley

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.

Getting Started

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.

Create a chatbot with Bot Father

  • 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

Creating bot environment

  • 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

Write a program for bot

  • 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 to Use the Android NDK When Writing an App in Kotlin

青龙安装Bot监控dog——2022.4.3更新 – – Telegram 简体版Telegram中文版下载

Telegram主题——桌面版本的创新

Join 600+ Telegram Hot Group – Telegram Tips

😼😻貓貓聯盟◾️▫️寵物群电报新群推送TelegramGroupLinks-电报telegram技巧分享

86300 Seconds to Hours | Telegram

956 Weeks to Years | – Telegram 下载

1157 Minutes to Days | Telegram

1193 Minutes to Days | Telegram

1023 Minutes to Days | Telegram

About Me

Pretium lorem primis senectus habitasse lectus donec ultricies tortor adipiscing fusce morbi volutpat pellentesque consectetur risus curae malesuada dignissim lacus convallis massa mauris.