Example of a telegram bot in python without using specific libraries – Telegram Group

Example of a telegram bot in python without using specific libraries

Photo by Adem AY on Unsplash

To make a telegram bot there are a lot of libraries that allow you to do that.
However, if you want to make a bot in python without using specific libraries, this is the right article for you.

The libraries used are:

import json
import requests
import urllib

First you have to create the bot using BotFather (https://t.me/BotFather)

To check if someone has contacted the bot:

def get_updates(offset=None):
url = URL + "getUpdates?timeout=100"
if offset:
url += "&offset={}".format(offset)
js = get_json_from_url(url)
return js

To handle requests made to the bot:

def echo_all(updates):
for update in updates["result"]:
if update.get("message") != None:
if update.get("message", {}).get("text") != None:
text = update["message"]["text"]
chat = update["message"]["chat"]["id"]
print(text)
if text == "/test" or text == "/test@" + USERNAME_BOT:
text = "test response"
send_message(text, chat)
elif text == "/start" or text == "/start@" + USERNAME_BOT:
send_message("/test for test the bot", chat)

This is an example, which sends the response “test response” on receiving the /text command and on the /start command responds “/test for test the bot

This is the function for sending text messages:

def send_message(text, chat_id):
tot = urllib.parse.quote_plus(text)
url = URL + "sendMessage?text={}&chat_id={}".format(tot, chat_id)
get_url(url)

This is the function for sending messages with a document (the “doc” variable indicates the path to the file):

def send_document(doc, chat_id):
files = {'document': open(doc, 'rb')}
requests.post(URL + "sendDocument?chat_id={}".format(chat_id), files=files)

This is the function for sending messages with an image (the “doc” variable indicates the path to the file):

def send_image(doc, chat_id):
files = {'photo': open(doc, 'rb')}
requests.post(URL + "sendPhoto?chat_id={}".format(chat_id), files=files)

Here you can find all the code:

If you have any problems or if you have solved them in any other way do not hesitate to write them in the comments!

To get access to unlimited stories, you can also consider signing up to become a Medium member for just $5. If you sign up using my link, I’ll receive a small commission.

Ten articles before and after

Домашняя бухгалтерия в telegram. Электронные чеки. QR. – Telegram Group

How to write UK Rent Telegram Bot – Telegram Group

Tasker and Telegram integration: live location – Telegram Group

Setup Telegram bot app. Register a bot – Telegram Group

How to send a message to your Telegram channel by Python? – Telegram Group

Monitorar suas aplicações na AWS usando o Telegram – Telegram Group

Telegram bot in Go app. Notifications are important part of any… – Telegram Group

Домашняя бухгалтерия в telegram. Начало. – Telegram Group

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

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