Example of a telegram bot in python without using specific libraries – Telegram 中文版

Telegram

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 中文版

How to write UK Rent Telegram Bot – Telegram 中文版

Tasker and Telegram integration: live location – Telegram 中文版

Setup Telegram bot app. Register a bot – Telegram 中文版

How to send a message to your Telegram channel by Python? – Telegram 中文版

Monitorar suas aplicações na AWS usando o Telegram – Telegram 中文版

Telegram bot in Go app. Notifications are important part of any… – Telegram 中文版

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

data-rh=”true”>Telegram Bot for Lazy – Max Makhrov – Medium – Telegram 中文版

Django Telegram Bot. В данной статье я расскажу о том как… – 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.