3

I'm trying to retrieve messages from Reddit subreddits using PRAW. It is working properly for most of the cases but I'm getting the following error that the message is too long.I'm using pytelegrambotApi

Snippet:

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

Error: Error Is there any workaround that I can do possibly do here to access the full message?

Anandakrishnan
  • 347
  • 1
  • 4
  • 18
  • 1
    How long is your longest message? You could try compressing it, sending it in two parts, storing it and providing a link for user access, etc.... There's loads of "workarounds" but I'd try to figure out what my limits are, and what my average/maximum lengths will be to decide what solution to implement. – Isolated Dec 03 '21 at 17:46

1 Answers1

5

One Telegram message must contain no more than 4096 characters. The message is then split into another message (that is, the remainder). Add this code to your 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)
hoosnick
  • 136
  • 4