Simple Telegram Bot using API’s. A Simple Telegram Bot which can respond… – Telegram Group

Simple Telegram Bot using API’s

Step 1: Create your Telegram Bot using Telegram messenger

@Botfather with verified blue check box

Step 2: Copy the Token from the response

Step 3: Get your Telegram messenger chat id.

Search for @idbot
Your Chat Id will be at the bottom “P.S. Your ID: “

Step 4: Start coding

---
BOT_API_KEY : 'bot1234567890:ABCdE1FgHIJKlMnoPQR234-stQuv_wX1yzA'
BOT_URL : 'https://api.telegram.org/'
BOT_GET_UPDATE : '/getUpdates'
BOT_SEND_MSG : '/sendMessage'
USER_CHAT_ID : [1234567890]
import requests
import json
import yaml
import time
with open('Config.yml') as env_setup:
try:
env_det=yaml.safe_load(env_setup)
except:
print('Error')
API_KEY=env_det['BOT_API_KEY']
def telegram_get_message(tele_token,p_offset_id):
url=env_det['BOT_URL']+tele_token+env_det['BOT_GET_UPDATE']
data={'offset':p_offset_id}
try:
response=requests.request("POST",url,params=data)
telegram_data=json.loads(response.text)
return telegram_data
except:
return
def telegram_send_message(tele_token,tele_chat_id,message):
url=env_det['BOT_URL']+tele_token+env_det['BOT_SEND_MSG']
data={"chat_id": tele_chat_id, "text": message}
try:
response=requests.request("POST",url,params=data)
telegram_data=json.loads(response.text)
return telegram_data['ok']
except:
return telegram_data
# Loop so that this bot can run for some time. I will be running this bot for 10 times with a sleep for 2 sec.
for _ in range(10):
response_data=''

# Dictonary to create a queue for user request.
data_request={}

# Getting the response from the user
response_data=(telegram_get_message(API_KEY,p_offset))

# Parsing the user response to extract his queries.
for i in response_data['result']:

# Getting the offset_id from the result and incrementing it with 1
p_offset=int(i['update_id'])+1

chat_id=i['message']['chat']['id']

# We just want that this bot respond to only to us, hence making sure that Chat id is mine.
if chat_id!=env_det['USER_CHAT_ID']:
continue

# If my queue is empty then start building it based on my chat id
if chat_id not in data_request:
data_request[chat_id]=[i['message']['text']]
else:
data_request[chat_id].append(i['message']['text'])

# Start the loop based on the queue and start responding.
for idx,val in data_request.items():
for user_request in val:
telegram_send_message(API_KEY,env_det['USER_CHAT_ID'], 'Hi. I am currently in a built Stage')
time.sleep(2)

Step 5: Expand your bot as per your requirement

Ten articles before and after

Making our First Telegram Bot [Part -2] – Telegram Group

Bots! What are they? Let’s make a Telegram Bot [Part -1] – Telegram Group

How I Create “Sembako Bot” using Google Cloud – Telegram Group

How to automatically and repeatedly send messages in Telegram groups (every few minutes or hours) – Telegram Group

Metatrader +Python +Telegram. Metatrader is a popular platform for… – Telegram Group

How to build a Telegram bot to show Chainlink price feeds – Telegram Group

Send APK automatically to Telegram using Dart/Flutter – Telegram Group

IntruderDet —An Intruder Detection Bot for my Appartment – Telegram Group

Телеграм бот для уведомлений об ордерах Binance – Telegram Group

How to create a Telegram bot with Python in under 10 min! – Telegram Group