尝试通过 Telegram API 发送消息时出现错误代码 400(错误请求)

时间:2021-02-27 10:25:49

标签: python chatbot telegram-bot

我正在尝试制作一个比特币 Telegram Bot,它将发送当前的比特币汇率。

我被我的代码的 Telegram API 部分困住了,我需要在那里发送我的消息。

String[] s = line.split("\\n")

json、requests、telebot等需要的库都安装好了。

其他命令,如 @bot.message_handler(commands=['bit']) def bit(message): link = 'https://blockchain.info/ru/ticker' response = requests.get(link).text text = json.loads(response) bot.send_message('Продажа ',text["RUB"]["sell"],'рублей','\nПокупка ',text["RUB"]["buy"],'рублей') 或只是回答通常的消息工作正常,但这是我在尝试发送比特币汇率时遇到的错误:

/start

Image representation of code here

1 个答案:

答案 0 :(得分:1)

您确定要坚持使用 API docs 中详述的 send_message() 签名吗?

看起来您为 text 参数提供了多个参数,请尝试将其包装在一个语句中,例如:

text = json.loads(response)
messageText = 'Продажа '+text["RUB"]["sell"]+'рублей'+'\nПокупка '+text["RUB"]["buy"]+'рублей'

然后调用 send_message() 方法,提供以下三个非可选参数:

  1. chat_id
  2. text
  3. parse_mode

所以:

@bot.message_handler(commands=['bit']) 
def bit(message):
    link = 'https://blockchain.info/ru/ticker'
    response = requests.get(link).text
    text = json.loads(response)
    chatId = '@channelusername'
    messageText = 'Продажа '+text["RUB"]["sell"]+'рублей'+'\nПокупка '+text["RUB"]["buy"]+'рублей'
    bot.send_message(chatId, messageText, parse_mode=HTML)

当然,不要忘记编辑您的 chatId,它必须是“目标聊天的唯一标识符或目标频道的用户名(格式为@channelusername)”。