如何使我的Telegram机器人确认以前看到的消息?

问题描述

问题:

  • Telegram机器人无法识别看到的消息,并一直响应最新消息,直到我在命令行中发送“ quit”或crtl-c为止。

完全是python的新手。我的编程逻辑可能存在缺陷。 在“ for updates_“的last_update_id” [“结果”]“中,我尝试在每个循环后将1添加到last_update_id变量。但是变量似乎没有更新。



# chatbot.py not included. It trains NN model. 

import json 
import requests
import time
import urllib
import telegram

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


def get_url(url):
        response = requests.get(url)
        content = response.content.decode("utf8")
        return content

def get_json_from_url(url):
        content = get_url(url)
        js = json.loads(content)
        return js


def get_updates(offset): #gets json file from URL
        url = URL + "getUpdates"
        if offset:
                url += "?offset={}".format(offset)
        js = get_json_from_url(url)
        return js

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 get_last_chat_text(updates):
        num_updates = len(updates["result"])
        last_update = num_updates - 1
        text = updates["result"][last_update]["message"]["text"] #text input
        return text

def get_last_chat_id(updates):
        chat_id = updates["result"][-1]["message"]["chat"]["id"]
        return chat_id


def send_message(output,chat_id):
        bot = telegram.Bot(token=TOKEN)
        bot.sendMessage(chat_id=chat_id,text = output)
     
def main():
        input_text = get_last_chat_text(updates)
        return input_text                
           

print("Let's chat! (type 'quit' to exit)")
last_update_id = None
while True:
        updates = get_updates(last_update_id) #returns json file
        last_update_id = get_last_update_id(updates) #returns max_update_id

        for last_update_id in updates["result"]:
                main()
                input_text = main()
                if input_text == "quit":
                        break
                input_text = tokenize(input_text)
                X = bag_of_words(input_text,all_words)
                X = X.reshape(1,X.shape[0])
                X = torch.from_numpy(X).to(device)

                output = model(X)
                _,predicted = torch.max(output,dim=1)

                tag = tags[predicted.item()]

                probs = torch.softmax(output,dim=1)
                prob = probs[0][predicted.item()]
                if prob.item() > 0.75:
                        for intent in intents['intents']:
                                if tag == intent["tag"]:
                                        output = f"{random.choice(intent['responses'])}"
                else:
                        output = f"{bot_name}: I do not understand..."

                print(output)

                chat_id = get_last_chat_id(updates)
                print(chat_id)

                send_message(output,chat_id)
                time.sleep(0.1)

                last_update_id =+ 1 #returns max_id in the json file and adds 1
                continue

解决方法

我设法通过在循环中添加一个中断来解决此问题,以便使其循环回到外部“ while”循环。下面是修改后的代码:

# chatbot.py module imported above this line not included. It trains NN model. 

import json 
import requests
import time
import urllib
import telegram

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


def get_url(url):
        response = requests.get(url)
        content = response.content.decode("utf8")
        return content

def get_json_from_url(url):
        content = get_url(url)
        js = json.loads(content)
        return js


def get_updates(offset): #gets json file from URL
        url = URL + "getUpdates"
        if offset:
                url += "?offset={}".format(offset)
        js = get_json_from_url(url)
        return js

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

def get_last_chat_text(updates):
        # num_updates = len(updates["result"])
        # last_update = num_updates - 1
        text = updates["result"][-1]["message"]["text"] #text input
        return text

def get_last_chat_id(updates):
        chat_id = updates["result"][-1]["message"]["chat"]["id"]
        return chat_id


def send_message(output,chat_id):
        bot = telegram.Bot(token=TOKEN)
        bot.sendMessage(chat_id=chat_id,text = output)

def main():
        input_text = get_last_chat_text(updates)
        return input_text                

bot_name = "XXX"
print("Let's chat! (type 'quit' to exit)")
last_update_id = 0
while True:
        updates = get_updates(last_update_id) #returns json file

        for last_update_id in updates["result"]:
                main()
                input_text = main()
                if input_text == "quit":
                        break
                input_text = tokenize(input_text)
                X = bag_of_words(input_text,all_words)
                X = X.reshape(1,X.shape[0])
                X = torch.from_numpy(X).to(device)

                output = model(X)
                _,predicted = torch.max(output,dim=1)

                tag = tags[predicted.item()]

                probs = torch.softmax(output,dim=1)
                prob = probs[0][predicted.item()]
                if prob.item() > 0.75:
                        for intent in intents['intents']:
                                if tag == intent["tag"]:
                                        output = f"{random.choice(intent['responses'])}"
                else:
                        output = f"{bot_name}: I do not understand..."

                print(output)

                chat_id = get_last_chat_id(updates)
                print(chat_id)

                send_message(output,chat_id)
                time.sleep(0.1)
        
                break
        last_update_id = get_last_update_id(updates) + 1 #returns max_id in the json file and adds 1