Question: The whole solution is divided into three files which are server.py, config.cfg, and telegram.py. You need to generate your token in order to use the

The whole solution is divided into three files which are server.py, config.cfg, and telegram.py.

You need to generate your token in order to use the bot and paste the token in the config.cfg file in place of your token.

The files are:

telegram.py

import requests import json import configparser as cfg class telegram_chatbot():    def __init__(self, config):        self.token = self.read_token_from_config_file(config)        self.base = "https://api.telegram.org/bot{}/".format(self.token)     def get_updates(self, offset=None):        url = self.base + "getUpdates?timeout=100"         if offset:            url = url + "&offset={}".format(offset + 1)        r = requests.get(url)         return json.loads(r.content)     def send_message(self, msg, chat_id):        url = self.base + "sendMessage?chat_id={}&text={}".format(chat_id, msg)        if msg is not None:            requests.get(url)     def read_token_from_config_file(self, config):        parser = cfg.ConfigParser()        parser.read(config)        return parser.get('creds', 'token') screenshot of this code to get better understanding

1 2 3 4 5 6 7 8 9 10 11 22499529872

12 13 14 15 16 17 18 20 A 21 19 E

config.cfg

[creds] token = your token

screenshot of this code to get better idea about indentation

import requests import json import configparser as cfg class telegram chatbot ():

server.py

from telegram import telegram_chatbot bot = telegram_chatbot("config.cfg") def make_reply(msg):    f = open("index.html", 'w')     if "http" in msg:        f.write(msg)        f.close()        return "saved the url under index.html file name"    else:         reply = "Okay received this " + msg         return reply  update_id = None while True:    updates = bot.get_updates(offset=update_id)    updates = updates["result"]    # print(updates)    if updates:        for item in updates:            update_id = item["update_id"]            try:                message = item["message"]["text"]            except:                message = None            from_ = item["message"]["from"]["id"]             print(type(message))            reply = make_reply(message)            bot.send_message(reply, from_) 

screenshot of this code to get better idea about indentation

A A def get_updates (self, offset=None): def A init (self, config): self.token= self.read_token_from_config_file(config) self.base = "https://api.telegram.org/bot{}/".format (self.token) url = self.base + "getUpdates?timeout=100" if

Can anybody make telegram bot using this code I need for my unblur question thanks

1 2 3 4 5 6 7 8 9 10 11 22499529872 12 13 14 15 16 17 18 20 A 21 19 E import requests import json import configparser as cfg class telegram chatbot (): A A def get_updates (self, offset=None): def A init (self, config): self.token = self.read_token_from_config_file(config) self.base = "https://api.telegram.org/bot{}/".format (self.token) url = self.base + "getUpdates?timeout=100" if offset: url = url + "&offset={}".format (offset + 1) r = requests.get (url) return json.loads (r.content) def send message (self, msg, chat_id): url = self.base + "sendMessage?chat_id={}&text={}".format (chat_id, msg) if msg is not None: requests.get (url) 22 A 23 24 25 26 27 28 29 N N D requests.get (url) def read token from config file (self, config): parser = cfg.ConfigParser() parser.read (config) return parser.get('creds', 'token') 1 2 [creds] token = your token H ~ M 1 2 3 4 LO 5 6 7 8 9 10 11 12 13 14 15 16 ~ m 17 18 19 20 21 22 io co s D from telegram import telegram_chatbot bot D = telegram_chatbot ("config.cfg") def make_reply (msg): f = open("index.html", 'w') if "http" in msg: f.write (msg) f.close() return "saved the url under index.html file name" else: update id reply "Okay received this " + msg return reply = None while True: 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 D-D D while True: updates updates = updates ["result"] # print (updates) if updates: = bot.get_updates (offset=update_id) for item in updates: update_id = item["update_id"] try: message = item ["message"] ["text"] except: message None from = item ["message"]["from"]["id"] print (type (message)) reply = make_reply (message) bot.send_message (reply, from_)

Step by Step Solution

3.25 Rating (154 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!