skip to Main Content

I am creating a Telegram bot using pytelegrambotapi. But when I test the code, my Telegram Bot always replies with quoting my input like this, I don’t want it to quote my input message but send the message directly.

Also how can I get replies by just using simple Hi or Hello not /hi or /hello.

My Code:

import telebot
import time
bot_token = ''

bot= telebot.TeleBot(token=bot_token)

@bot.message_handler(commands=['start'])
def send_welcome(message):
    bot.reply_to(message, 'Hi')

@bot.message_handler(commands=['help'])
def send_welcome(message):
    bot.reply_to(message, 'Read my description')

while True:
    try:
        bot.polling()
    except Exception:
        time.sleep(10)

2

Answers


  1. If i understood corectly:
    cid = message.chat.id
    bot.send_message(cid, "Hello")

    Login or Signup to reply.
  2. I don’t want it to quote my input message but send the message directly.

    bot.reply_to replies to the message itself. If you wish to send a separate message, use bot.send_message. You’ll need to pass the ID of the user you’re wishing to send a message to. You can find this id on message.chat.id so you’re sending the message to the same chat.

    @bot.message_handler(commands=['help'])
    def send_welcome(message):
    
        # Reply to message
        bot.reply_to(message, 'This is a reply')
    
        # Send message to person
        bot.send_message(message.chat.id, 'This is a seperate message')
    

    Also how can I get replies by just using simple Hi or Hello not /hi or /hello.

    Instead off using a message_handler with a commands=['help'] you can remove the parameter to catch each message not catched by any command message handler.


    Example with above implemented:

    import telebot
    
    bot_token = '12345'
    
    bot = telebot.TeleBot(token=bot_token)
    
    
    # Handle /help
    @bot.message_handler(commands=['help'])
    def send_welcome(message):
    
        # Reply to message
        bot.reply_to(message, 'This is a reply')
    
        # Send message to person
        bot.send_message(message.chat.id, 'This is a seperate message')
    
    
    # Handle normal messages
    @bot.message_handler()
    def send_normal(message):
    
        # Detect 'hi'
        if message.text == 'hi':
            bot.send_message(message.chat.id, 'Reply on hi')
    
        # Detect 'help'
        if message.text == 'help':
            bot.send_message(message.chat.id, 'Reply on help')
    
    
    bot.polling()
    

    Visual result:
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search