Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Telegram Echo Bot in Python


import requests
import time
import urllib


TOKEN = "Enter Token here"
URL = "https://api.telegram.org/bot{}/".format(TOKEN)

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

def get_last_update_id(updates):
update_ids = []
for update in updates["result"]:
update_ids.append(int(update["update_id"]))
return max(update_ids)

def send_message(text, chat_id):
text = urllib.parse.quote_plus(text)
url=URL + "sendMessage?text={}&chat_id={}".format(text, chat_id)
try:
r=requests.get(url,timeout=2)
if r.reason!='OK':print(r.text)
except Exception as e:
print(e)
print(url)

def main():
last_update_id = None
while True:
try:
updates = get_updates(last_update_id)
z=updates.get("result")
if z and len(z) > 0:
last_update_id = get_last_update_id(updates) + 1
echo_all(updates)
time.sleep(0.5)
except Exception as e:
print(e)


def echo_all(updates):
for update in updates["result"]:
try:
print(update)
chat = update["message"]["chat"]["id"]
a = update["message"].get("text")
if a: print(chat,a)
send_message(a,chat)
except Exception as e:
print(e)

if __name__ == '__main__':
main()


This post first appeared on SMARTMANOJ, please read the originial post: here

Share the post

Telegram Echo Bot in Python

×

Subscribe to Smartmanoj

Get updates delivered right to your inbox!

Thank you for your subscription

×