Update to newer pyrogram.

This commit is contained in:
Danial Behzadi 2023-12-12 07:46:30 -05:00
parent b8c8775d13
commit 878f4c8882
5 changed files with 46 additions and 46 deletions

5
.gitignore vendored
View file

@ -3,6 +3,9 @@
.env
*.pyc
__pycache__
config.ini
config.py
tgchannel2toot.session
downloads/
nohup.out
unknown_errors.txt
tgchannel2toot.session-journal

View file

@ -12,9 +12,8 @@ To make it work, do the following:
Config
======
Make a copy of each config file:
Make a copy of config file:
(.env)$ cp config.ini.temp config.ini
(.env)$ cp config.py.temp config.py
Fill out config files.

View file

@ -1,3 +0,0 @@
[pyrogram]
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef

View file

@ -2,3 +2,5 @@ channel_id = 'iranFSevents'
access_token = '5He72z-Dz6DEruFjvAVYL8ieTRhha5xPwkLGv19rfgA'
instance = 'https://botsin.space'
character_limit = 500
api_id = 12345
api_hash = 0123456789abcdef0123456789abcdef

View file

@ -9,78 +9,77 @@ And post them on Mastodon
import os
from pyrogram import Client, idle
from mastodon import Mastodon
import pyrogram
import mastodon
import config
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
def get_text_and_media(message):
'''
"""
returns the text and media from post
'''
"""
text = " "
media = None
if message['text']:
text = message['text']
elif message['media']:
text = message['caption']
if hasattr(message, "media"):
text = message.caption
media = message.download()
elif hasattr(message, "text"):
text = message.text
return splitext(text), media
def connect():
'''
"""
initialize mastodon
'''
mastodon = Mastodon(
access_token=config.access_token,
api_base_url=config.instance
"""
mast = mastodon.Mastodon(
access_token=config.access_token, api_base_url=config.instance
)
return mastodon
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
return mast
def main():
'''
"""
main function
'''
telegram = Client("tgchannel2toot")
"""
telegram = pyrogram.Client(
"tgchannel2toot", api_id=config.api_id, api_hash=config.api_hash
)
mastodon = connect()
@telegram.on_message()
def check_message(client, message):
chat = message['chat']
chat = message.chat
if chat['type'] == 'channel':
if chat['username'] == config.channel_id:
if chat.type==pyrogram.enums.ChatType.CHANNEL:
if chat.username == config.channel_id:
text, media = get_text_and_media(message)
if media:
mid = mastodon.media_post(media)
os.remove(media)
else:
mid = None
post = mastodon.status_post(
text[0],
media_ids=mid)
post = mastodon.status_post(text[0], media_ids=mid)
for part in range(1, len(text)):
post = mastodon.status_post(
text[part],
in_reply_to_id=post['id'])
text[part], in_reply_to_id=post["id"]
)
telegram.start()
idle()
telegram.run()
if __name__ == '__main__':
if __name__ == "__main__":
main()