The whole solution is divided into three files which are server.py, config.cfg, and telegram.py. You need to
Fantastic news! We've Found the answer you've been seeking!
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 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
config.cfg
[creds] token = your token
screenshot of this code to get better idea about indentation
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
Can anybody make telegram bot using this code I need for my unblur question thanks
Related Book For
Principles of Auditing An Introduction to International Standards on Auditing
ISBN: 978-0273768173
3rd edition
Authors: Rick Hayes, Philip Wallage, Hans Gortemaker
Posted Date: