Skip to content Skip to sidebar Skip to footer

Telegram Bot Is Not Working

I made a telegram bot with python-telegram-bot. I have defined a list of words for the bot and I want to manage the chat bot in the group. That is, if there is a word in the chat t

Solution 1:

# -*- coding: utf8 -*-
#!python2
import time
import json 
import requests
#TOKEN = XXXXXX
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 delete_message(message_id, chat_id,msg):
    mlist=['Hello', 'سلام']
    url=URL + "deleteMessage?message_id={}&chat_id={}".format(message_id, chat_id)
    for i in mlist:
        if i in msg:request.get(url)

def echo_all(updates):
    for update in updates["result"]:
        cid = update["message"]["chat"]["id"]
        msg = update["message"].get("text")
        mid = update["message"].get("message_id")
        if msg:delete_message(mid,cid,msg)

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)    

if __name__ == '__main__':
    main()

Post a Comment for "Telegram Bot Is Not Working"