Skip to content
This repository has been archived by the owner on Jun 23, 2020. It is now read-only.

Commit

Permalink
Text can now be added to Tweets
Browse files Browse the repository at this point in the history
  • Loading branch information
rogama25 committed Feb 12, 2019
1 parent c796a61 commit 305a86b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
16 changes: 14 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def tg_audio_handler(message):
file.write(downloaded_voice)
converter.convert(filename,duration)
tw.tweet(filename + ".mp4")
tgclass.send_msg("Audio sent. If you were replying to a Tweet, send \"/cancel\" to exit reply mode.")
tgclass.send_msg("Audio sent. If you were replying to a Tweet, send \"/cancel\" to exit reply mode. Tweet text is now empty.")
os.remove(filename + ".ogg")
os.remove(filename + ".mp4")

Expand All @@ -52,7 +52,7 @@ def tg_message_handler(message):
cfg.telegram_user_id = message.from_user.id
tgclass.user = message.from_user.id
print("Bot linked to " + str(message.from_user.id) + " (" + message.from_user.first_name + ")")
tgclass.send_msg("Bot successfully linked. You can send me voice notes and I will Tweet them as a video or send me a link to a Tweet and then the voice note and I will tweet them as a reply to the specified Tweet.")
tgclass.send_msg("Bot successfully linked. You can send me voice notes and I will Tweet them as a video or send me a link to a Tweet and then the voice note and I will tweet them as a reply to the specified Tweet.\nYou can also add text to the tweet using \"/text <your text here>\". To remove the text, just send that command with no text.")
cfg.save_settings("config.cfg")
else:
if message.from_user.id == cfg.telegram_user_id:
Expand All @@ -69,6 +69,18 @@ def tg_message_handler(message):
if message.text == "/cancel":
tw.set_reply(None)
tgclass.send_msg("Now posting as a Tweet.")
elif message.text.startswith("/text"):
if message.text == "/text" or message.text == "/text ":
text = ""
else:
text = message.text[6:]
if len(text) > 240:
text = text[:239]
tw.set_text(text)
if text == "":
tgclass.send_msg("Text cleared.")
else:
tgclass.send_msg("Text set to: " + text)

tg.polling()
tg.stop_bot()
Expand Down
6 changes: 5 additions & 1 deletion twitter_.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def __init__(self, cfg: settings.Settings):
self.reply_id = None
if self.tw.VerifyCredentials() is None:
raise ValueError("Twitter credentials are wrong.")
self.text = ""

def set_reply(self, reply_id: int):
if reply_id is not None:
Expand All @@ -23,9 +24,12 @@ def set_reply(self, reply_id: int):
self.reply_id = None
return None, None

def set_text(self, text:str):
self.text = text

def tweet(self, media: str):
if self.reply_id is None:
autopop = False
else:
autopop = True
self.tw.PostUpdate(media=media, in_reply_to_status_id=self.reply_id, status="", auto_populate_reply_metadata=autopop)
self.tw.PostUpdate(media=media, in_reply_to_status_id=self.reply_id, status=self.text, auto_populate_reply_metadata=autopop)

0 comments on commit 305a86b

Please sign in to comment.