Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 电报机器人不返回API请求_Python_Telegram_Python Telegram Bot - Fatal编程技术网

Python 电报机器人不返回API请求

Python 电报机器人不返回API请求,python,telegram,python-telegram-bot,Python,Telegram,Python Telegram Bot,我对创建聊天机器人还不熟悉 import telebot bot = telebot.TeleBot('123345677789') def sendMessage(message, text): bot.send_message(message.chat.id, text) @bot.message_handler(func=lambda msg: msg.text is not None) def reply_to_message(message): if 'hello'

我对创建聊天机器人还不熟悉

import telebot

bot = telebot.TeleBot('123345677789')

def sendMessage(message, text):
   bot.send_message(message.chat.id, text)

@bot.message_handler(func=lambda msg: msg.text is not None)
def reply_to_message(message):
    if 'hello' in message.text.lower():
        sendMessage(message, 'Hello! How are you doing today?')
    else:
        bot.reply_to(message,'try hi or hello')

@bot.message_handler(func=lambda msg: msg.text is not None)
def getresponse(user_input):
    if 'virus' in user_input.text.lower():
        url = "https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats"
        querystring = {"country":"USA"}
        headers = {
            'x-rapidapi-host': "covid-19-coronavirus-statistics.p.rapidapi.com",
            'x-rapidapi-key': "ea33a4fd9cmshd4ead0c7290"}
        response = requests.request("GET", url, headers=headers, params=querystring)
        bot.reply_to(user_input,response.text)
    else:
        bot.reply_to(user_input,'type virus')
我一直在尝试让api返回数据。但是每当我尝试发送请求时,机器人都不会提醒我任何事情。感谢您的帮助


谢谢

问题在于,两个函数的筛选器相同,因此第一个函数始终具有优先级并回答您的消息。您的选项是:融合两个函数,删除第一个函数,从消息更改为命令一个函数,或者您可以尝试使用register\u next\u step\u handler(),因此在询问信息之前,您必须始终向机器人致敬(我觉得这听起来有些过分)

好的,让我们来谈谈熔合:

import telebot
import requests

bot = telebot.TeleBot(tgtoken)

def sendMessage(message, text):
    bot.send_message(message.chat.id, text)

@bot.message_handler(func=lambda msg: msg.text is not None)
def getresponse(user_input):
    if user_input.text.lower() in ["hello", "hi"]:
        sendMessage(user_input, 'Hello! How are you doing today?')
    elif 'virus' in user_input.text.lower():
        url = "https://covid-19-coronavirus-statistics.p.rapidapi.com/v1/stats"
        querystring = {"country":"Denmark"}
        headers = {
            'x-rapidapi-host': "covid-19-coronavirus-statistics.p.rapidapi.com",
            'x-rapidapi-key': rapidapitoken}
        response = requests.request("GET", url, headers=headers, params=querystring)
        if not response.json()["error"]:
            bot.reply_to(user_input,str(response.json()["data"]))
        else:
            bot.reply_to(user_input,"Error: {!s} , StatusCode: {!s}, Message: {!s}".format(response.json()["error"], response.json()["statusCode"], response.json()["message"]))
    else:
        bot.reply_to(user_input,'type hi, hello, or virus')

bot.polling()
就这样。好吧,我作弊了,我用的是丹麦而不是美国,因为丹麦的信息比美国的要少。但这不是问题,莱特?解决这个问题的最佳方法是发送所需的最少信息,否则您将达到两个限制:消息中的最大字符数和拆分消息时的请求过多

PS:也许当您显示从API检索信息时出现的错误时,代码并不完美,我无法测试它