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

Error code 400 (Bad request) while trying to send message through Telegram API

我正在尝试制作一个比特币电报机器人,它将发送当前的比特币汇率。

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

 @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"],'рублей')

json、requests、telebot 等所需的库都已正确安装。

其他命令,如 /start 或仅回答普通消息都可以正常工作,但这是我在尝试发送比特币汇率时遇到的错误:

Error text: ERROR - TeleBot: "A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: unsupported parse_mode"

Image representation of code here

您确定要遵守 api docs 中详述的 send_message() 的签名吗?

您似乎为文本参数提供了多个参数,请尝试将其包含在一个语句中,例如:

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)”。