【问题标题】:"A request to the Telegram API was unsuccessful. Error code: 400. Description: Bad Request: message is too long"“对 Telegram API 的请求不成功。错误代码:400。描述:错误请求:消息太长”
【发布时间】:2022-01-10 01:46:21
【问题描述】:

我正在尝试使用 PRAW 从 Reddit 子版块检索消息。它在大多数情况下都能正常工作,但我收到以下错误消息太长。我正在使用pytelegrambotApi

片段:

import praw
import telebot

bot = telebot.TeleBot(token)  

reddit = praw.Reddit(
    client_id=client, #these details are given accordingly and are correct. No errors here.
    client_secret=secret,
    user_agent="user_agent",
)

def get_posts(sub):
   for submission in reddit.subreddit(sub).hot(limit=10):
    print(submission)
    if submission.author.is_mod:
      continue
    elif submission.selftext=="":
      return submission.title,submission.url
    else:
      print("It's working")
      print(submission.url)
      return submission.title,submission.selftext 

@bot.message_handler(func=lambda message: True)
def echo_message(message):
    subreddit = message.text
    title,post = get_posts(subreddit)
    m = title + "\n" + post
    bot.reply_to(message,m)

bot.infinity_polling()

错误: 我可以在这里做些什么来访问完整的消息?

【问题讨论】:

  • 您最长的消息是多长时间?您可以尝试压缩它,将其分成两部分发送,存储它并提供用户访问链接等......有很多“解决方法”,但我会尝试弄清楚我的限制是什么,以及我的平均水平是多少/maximum lengths 将决定实施何种解决方案。

标签: python-3.x telegram-bot reddit praw py-telegram-bot-api


【解决方案1】:

一个电报消息必须包含不超过4096 个字符。然后将消息拆分为另一条消息(即剩余部分)。 将此代码添加到您的message_handler

m = title + "\n" + post
if len(m) > 4095:
    for x in range(0, len(m), 4095):
        bot.reply_to(message, text=m[x:x+4095])
else:
    bot.reply_to(message, text=m)

【讨论】:

  • 明白,谢谢。不过有一个问题,我想知道您从哪里获得 4096 个字符的详细信息。
猜你喜欢
  • 1970-01-01
  • 2014-08-23
  • 2015-03-08
  • 2018-08-09
  • 2018-10-06
  • 2015-11-02
  • 2022-01-04
  • 2020-09-24
  • 1970-01-01
相关资源
最近更新 更多