Bharath
Bharath

Reputation: 26

How can I reply to a message after a command in Telebot API in python

I have this code..

import telebot

#telegram API 
api_key = os.environ['Tele_API_key']
bot = telebot.TeleBot(api_key)

@bot.message_handler(commands=['start'])
def help_command(message):
    bot.send_message(chat.id,"send a message")

@bot.message_handler(func = lambda msg: msg.text is not None and '/' not in msg.text)
    if message.text == "Hi":
        bot.send_message(chat.id,"Hello!")

It sends me "Hello" Everytime I send "Hi"

I want it to be like, after I do /start it should ask "send a message" and when I send "Hello" it should send "Hi"

But the problem is, it sends "Hi" everytime I send "Hello"

But I only want it to say "Hello" after I send /start and then the "Hi" message

I am beginner, Thanks for the help

Upvotes: 1

Views: 9463

Answers (2)

mohammad esmaili
mohammad esmaili

Reputation: 1747

You can do like this

@bot.message_handler(commands=[all_commands['start']])
def say(message):
    bot.send_message(message.chat.id, "send a message")

@bot.message_handler(func=lambda msg: msg.text is not None and '/' not in msg.text)
def sayHi(message):
    if message.text == "Hi":
         bot.reply_to(message, "Hello!")

You can reply a message with reply_to(message, "your reply text")

Upvotes: 1

Vishal Singh
Vishal Singh

Reputation: 21

You was created object msg inside @message_handler , But haven't used it.

These are silly mistakes

  • message.text
  • chat.id

Correct ✅

  • msg.text
  • msg.chat.id

Final code

@bot.message_handler(func = lambda msg: msg.text is not None and '/' not in msg.text)
if msg.text == "Hi":
    bot.send_message(msg.chat.id,"Hello!")

Upvotes: 1

Related Questions