我正在学习创建 Telegram 机器人,并尝试使用 Telebot 制作一个具有简单响应的简单机器人,但我在返回响应的函数上遇到问题,它仅返回“我不明白”。
这是我的代码:
import telebot
#constants is another file that have the API key
from constants import API_KEY
bot = telebot.TeleBot(API_KEY)
def greet(user_input):
if str(user_input).lower() in ["hi", "hello"]:
return "Hello ! How are you?"
elif str(user_input).lower() in ["am good", "great"]:
return "Good to hear that!"
elif str(user_input).lower() == "who are you":
return "I'm a bot!"
else:
return "I don't understand."
@bot.message_handler(commands = ["start"])
def welcom(message):
bot.send_message(message.chat.id, "Howdy, how are you doing?")
@bot.message_handler(commands = ["help"])
def welcom(message):
bot.send_message(message.chat.id, "what can i do for you today")
@bot.message_handler(func=lambda m:True)
def reply(message):
bot.reply_to(message, greet(message))
bot.polling()
我期望得到当我说某事时应该得到的回应,但我只得到“我不知道”
您的消息现在不是字符串。您需要首先从 Message 对象获取文本。
试试这个:
@bot.message_handler(func=lambda m:True)
def reply(message):
bot.reply_to(message, greet(message.text))