【问题标题】:Telegram bot can't send direct messages电报机器人无法发送直接消息
【发布时间】:2021-09-10 11:30:06
【问题描述】:

我正在使用pytelegrambotapi 创建一个 Telegram 机器人。但是当我测试代码时,我的 Telegram Bot 总是回复引用我的输入 like this,我不希望它引用我的输入消息而是直接发送消息。

另外我怎样才能通过使用简单的HiHello 而不是/hi/hello 来获得回复。

我的代码:

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)

【问题讨论】:

  • 我猜 reply_to 这样做是为了表明它正在回复什么消息。也许尝试 bot.send_message(message.chat.id, “Hi”) 回复频道而不是消息?

标签: bots telegram php-telegram-bot py-telegram-bot-api telegram-api


【解决方案1】:

如果我理解正确: cid = message.chat.id bot.send_message(cid, "你好")

【讨论】:

    【解决方案2】:

    我不希望它引用我的输入消息,而是直接发送消息。

    bot.reply_to 回复消息本身。如果您想发送单独的消息,请使用bot.send_message。您需要传递您希望向其发送消息的用户的 ID。您可以在 message.chat.id 上找到此 ID,以便您将消息发送到同一个聊天室。

    @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')
    

    另外,我怎样才能通过使用简单的 Hi 或 Hello 而不是 /hi 或 /hello 来获得回复。

    您可以删除参数以捕获任何命令消息处理程序未捕获的每条消息,而不是使用 message_handlercommands=['help']


    上面实现的例子:

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

    视觉效果:

    【讨论】:

      猜你喜欢
      • 2017-03-19
      • 2017-09-01
      • 2019-03-13
      • 2016-03-11
      • 1970-01-01
      • 2019-10-30
      • 2019-12-31
      • 2017-05-11
      • 2021-01-29
      相关资源
      最近更新 更多