Search code examples
pythonbotstelebot

Having a problem with running my Telegram bot


i'm learning about creating Telegram bots and tried to make a simple bot with simple responses using telebot but i'm having a problem with the function that return the responses it returns only "i don't understand."

This is my code:

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()

i expected to get the responses that i supposed to get when say somethings but i'm only getting "i don't know"


Solution

  • Your message is not a string now. You need to get text from Message object first.

    Try this:

    @bot.message_handler(func=lambda m:True)
    def reply(message):
        bot.reply_to(message, greet(message.text))