Skip to content

Commit

Permalink
make sure tweet won't exceed length
Browse files Browse the repository at this point in the history
  • Loading branch information
BennyThink committed Jan 15, 2024
1 parent 7300a39 commit 49d4b98
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

Supports Twitter API v2! 🎉🎉🎉🎉🎉🎉

# Public Bot has been discontinued

![](assets/announcement.png)

# Features

All the following features rely on authorized users.
Expand Down
Binary file removed assets/announcement.png
Binary file not shown.
16 changes: 11 additions & 5 deletions teletweet/tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,22 @@ def send_tweet(message, pics: Union[list, None] = None) -> dict:
if not text:
text = ""
tweet_id = __get_tweet_id_from_reply(message)
client, api = __connect_twitter(chat_id)
logging.info("Tweeting...")
ids = upload_media(api, pics)
try:
client, api = __connect_twitter(chat_id)
logging.info("Tweeting...")
ids = upload_media(api, pics)
status = client.create_tweet(text=text, media_ids=ids, in_reply_to_tweet_id=tweet_id)
logging.info("Tweeted")
response = status.data
except Exception as e:
logging.error(traceback.format_exc())
response = {"error": str(e)}
if "Your Tweet text is too long." in str(e):
logging.warning("Tweet too long, trying to make it shorter...")
# try to post by making it shorter
status = client.create_tweet(text=text[:110] + "...", media_ids=ids, in_reply_to_tweet_id=tweet_id)
response = status.data
else:
logging.error(traceback.format_exc())
response = {"error": str(e)}

return response

Expand Down

0 comments on commit 49d4b98

Please sign in to comment.