Reputation: 26
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
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
Reputation: 21
You was created object msg inside @message_handler , But haven't used it.
These are silly mistakes
Correct ✅
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