如何推文可能超过 280 个字符?因此,如果 string > 280,则打印前 280 个字符,然后在该线程中使用其余字符再次推文

问题描述

抱歉标题不连贯,字数限制(讽刺)。我创建了一个机器人,当提到它时,它会从列表中随机选择一个字符串进行响应,这很正常。

问题是我列表中的大多数字符串都超过 280 个字符,twitter 字数限制。此外,用户 twitter 句柄、'@' 和用户名后面的空格(字符串应该开始的地方)也计入字数限制

这是我想出的代码,但我被难住了...

import replies # Separate file where all replies are stored
def reply_to_tweets():
    print("Scanning for new mentions...")
    last_seen_id = retrieve_id(FILE)
    mentions = api.mentions_timeline(last_seen_id,tweet_mode="extended")
    myself = api.me()
    for tweet in reversed(mentions): 
        if myself.screen_name in tweet.full_text: # If my @ is in the full text
            if tweet.in_reply_to_status_id is not None:
                continue
            last_seen_id = tweet.id 
            store_id(last_seen_id,FILE) 
            random_choice = random.choice(replies.strings_more_than_280)  # Random string chosen
            def splitter(chosen_string):
                return [char for char in chosen_string] # This part may be obsolete but it gives 
                                                        # me peace of mind as I kNow it counts 
                                                        # emojis and spaces!

            username = '@' + tweet.user.screen_name 
# There is a space at the beginning of every string in my list,so this should 
# account for the character after the @ where the string should go
            length_of_user = len(username) # WORKS
            
            splitter_in_action = splitter(random_choice)
            length_of_string = len(splitter_in_action)
# This is where the space is counted,since there's a space in the beginning of 
# the string already,then it'll count towards the length of the string when we 
# calculate it using the splitter() function

            difference = length_of_string - length_of_user
            difference2 = difference - length_of_user
            if length_of_string + length_of_user > 280: 
                print(username + random_choice[:difference2]) # status update goes here
                
# Now those last few lines of code,I'm just guessing,I have no idea how to 
# calculate for the difference in my string minus the user's @ and then tweet up
# to 280 chars,then tweet the rest later. It's probably simple but I am perplexed!!!

错误 [{'code': 186,'message': 'Tweet needs to be a bit shorter.'}]

任何帮助都会非常有用,因为我已经被困在这里一段时间了。这不是为了向人们发送垃圾邮件和烦人,只是我想让机器人打印的消息太长,所以是的,我知道这是很多代码,但我真的希望有人能启发我!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)