Compare commits

...

10 commits

Author SHA1 Message Date
Danial Behzadi
572a93a8f1
Make sure message has media. 2024-04-07 16:41:09 +03:30
Danial Behzadi
878f4c8882 Update to newer pyrogram. 2023-12-12 07:46:30 -05:00
Danial Behzadi
b8c8775d13 take the int part 2021-06-01 05:08:34 +04:30
Danial Behzadi
2ab3e0181c make things easier to develop in future 2021-06-01 05:03:52 +04:30
Danial Behzadi
36a2257a95 fix that last character 2021-06-01 04:34:19 +04:30
Danial Behzadi
71e0b2b489 spacing 2021-06-01 04:23:54 +04:30
Danial Behzadi
21f8672e3d handle long posts 2021-06-01 04:20:14 +04:30
Danial Behzadi
4bedba24bd let the script run from everywhere 2021-05-29 20:37:27 +04:30
Danial Behzadi
d13b76fa3e run the script with venv python 2021-05-29 20:02:16 +04:30
Danial Behzadi
62c45a4538 update to latest pyrogram api 2021-03-09 20:09:33 +03:30
6 changed files with 76 additions and 34 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

@ -6,15 +6,14 @@ To make it work, do the following:
$ git clone https://gitlab.com/danialbehzadi/tgchannel2toot.git
$ cd tgchannel2toot
$ virtualenv -p python3 --no-site-packages --distribute .env
$ python3 -m venv .env
$ source .env/bin/activate
(.env)$ pip3 install -r requirements.txt
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

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

6
run.sh
View file

@ -1,4 +1,8 @@
#!/bin/bash
# Released under GPLv3+ License
# Danial Behazdi <dani.behzi@ubuntu.com>, 2018-2021
export SHELL=/bin/bash
cd "$( dirname "${BASH_SOURCE[0]}" )"
source .env/bin/activate
./tgchannel2toot.py
python3 tgchannel2toot.py

86
tgchannel2toot.py Executable file → Normal file
View file

@ -1,49 +1,85 @@
#! /usr/bin/env python3
# Danial Behzadi - 2018
# Released under GPLV3+
#!/usr/bin/python3
# Released under GPLv3+ License
# Danial Behazdi <dani.behzi@ubuntu.com>, 2018-2021
"""
Listen for a new post on a Telegram channel
And post them on Mastodon
"""
import os
import pyrogram
import mastodon
import config
import os
from pyrogram import Client
from mastodon import 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
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") and message.media:
text = message.caption
media = message.download()
return text, media
elif hasattr(message, "text"):
text = message.text
return splitext(text), media
def connect():
mastodon = Mastodon(
access_token = config.access_token,
api_base_url = config.instance
"""
initialize mastodon
"""
mast = mastodon.Mastodon(
access_token=config.access_token, api_base_url=config.instance
)
return mastodon
return mast
def main():
telegram = Client("tgchannel2toot")
"""
main function
"""
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:
media_id = mastodon.media_post(media)
mastodon.status_post(text, media_ids=media_id)
mid = mastodon.media_post(media)
os.remove(media)
else:
mastodon.toot(text)
mid = None
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"]
)
telegram.start()
telegram.idle()
telegram.run()
if __name__ == '__main__':
if __name__ == "__main__":
main()