2

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)

0stone0
  • 34,288
  • 4
  • 39
  • 64
Yash Dwivedi
  • 47
  • 1
  • 5
  • I’m guessing reply_to does that to indicate what message it’s replying to. Maybe try bot.send_message(message.chat.id, “Hi”) to reply to the channel instead of the message? – Joachim Isaksson Jun 28 '21 at 06:48

2 Answers2

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

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

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