2021-03-09 20:09:33 +03:30
|
|
|
#!/usr/bin/python3
|
|
|
|
# Released under GPLv3+ License
|
|
|
|
# Danial Behazdi <dani.behzi@ubuntu.com>, 2018-2021
|
2020-01-04 17:16:29 +03:30
|
|
|
|
2021-06-01 04:20:14 +04:30
|
|
|
"""
|
|
|
|
Listen for a new post on a Telegram channel
|
|
|
|
And post them on Mastodon
|
|
|
|
"""
|
|
|
|
|
2020-01-05 13:04:04 +03:30
|
|
|
import os
|
2021-06-01 04:20:14 +04:30
|
|
|
|
2023-12-12 07:46:30 -05:00
|
|
|
import pyrogram
|
|
|
|
import mastodon
|
2020-01-04 17:16:29 +03:30
|
|
|
|
2021-06-01 04:20:14 +04:30
|
|
|
import config
|
|
|
|
|
|
|
|
|
2023-12-12 07:46:30 -05:00
|
|
|
def splitext(text: str):
|
|
|
|
"""
|
|
|
|
split text into parts
|
|
|
|
"""
|
|
|
|
limit = config.character_limit
|
|
|
|
result = [
|
|
|
|
text[limit * part : limit * (part + 1)]
|
|
|
|
for part in range(len(text) // limit + 1)
|
|
|
|
]
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2020-01-05 13:04:04 +03:30
|
|
|
def get_text_and_media(message):
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
2021-06-01 04:20:14 +04:30
|
|
|
returns the text and media from post
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
2020-01-05 13:04:04 +03:30
|
|
|
text = " "
|
|
|
|
media = None
|
2024-04-07 16:41:09 +03:30
|
|
|
if hasattr(message, "media") and message.media:
|
2023-12-12 07:46:30 -05:00
|
|
|
text = message.caption
|
2020-01-05 13:04:04 +03:30
|
|
|
media = message.download()
|
2023-12-12 07:46:30 -05:00
|
|
|
elif hasattr(message, "text"):
|
|
|
|
text = message.text
|
2021-06-01 05:03:52 +04:30
|
|
|
return splitext(text), media
|
2020-01-05 13:04:04 +03:30
|
|
|
|
2021-06-01 04:20:14 +04:30
|
|
|
|
2020-01-04 17:16:29 +03:30
|
|
|
def connect():
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
2021-06-01 04:20:14 +04:30
|
|
|
initialize mastodon
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
|
|
|
mast = mastodon.Mastodon(
|
|
|
|
access_token=config.access_token, api_base_url=config.instance
|
|
|
|
)
|
|
|
|
return mast
|
2021-06-01 04:20:14 +04:30
|
|
|
|
|
|
|
|
2020-01-04 17:16:29 +03:30
|
|
|
def main():
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
2021-06-01 04:20:14 +04:30
|
|
|
main function
|
2023-12-12 07:46:30 -05:00
|
|
|
"""
|
|
|
|
telegram = pyrogram.Client(
|
|
|
|
"tgchannel2toot", api_id=config.api_id, api_hash=config.api_hash
|
|
|
|
)
|
2020-01-04 17:16:29 +03:30
|
|
|
mastodon = connect()
|
|
|
|
|
|
|
|
@telegram.on_message()
|
|
|
|
def check_message(client, message):
|
2023-12-12 07:46:30 -05:00
|
|
|
chat = message.chat
|
2020-01-04 17:16:29 +03:30
|
|
|
|
2023-12-12 07:46:30 -05:00
|
|
|
if chat.type==pyrogram.enums.ChatType.CHANNEL:
|
|
|
|
if chat.username == config.channel_id:
|
2020-01-05 13:04:04 +03:30
|
|
|
text, media = get_text_and_media(message)
|
|
|
|
if media:
|
2021-06-01 04:20:14 +04:30
|
|
|
mid = mastodon.media_post(media)
|
2020-01-05 13:04:04 +03:30
|
|
|
os.remove(media)
|
|
|
|
else:
|
2021-06-01 04:20:14 +04:30
|
|
|
mid = None
|
2023-12-12 07:46:30 -05:00
|
|
|
post = mastodon.status_post(text[0], media_ids=mid)
|
2021-06-01 05:03:52 +04:30
|
|
|
for part in range(1, len(text)):
|
2021-06-01 04:20:14 +04:30
|
|
|
post = mastodon.status_post(
|
2023-12-12 07:46:30 -05:00
|
|
|
text[part], in_reply_to_id=post["id"]
|
|
|
|
)
|
2020-01-04 17:16:29 +03:30
|
|
|
|
2023-12-12 07:46:30 -05:00
|
|
|
telegram.run()
|
2020-01-04 17:16:29 +03:30
|
|
|
|
2021-06-01 04:20:14 +04:30
|
|
|
|
2023-12-12 07:46:30 -05:00
|
|
|
if __name__ == "__main__":
|
2020-01-04 17:16:29 +03:30
|
|
|
main()
|