Compare commits
10 commits
83d8b75b34
...
572a93a8f1
Author | SHA1 | Date | |
---|---|---|---|
![]() |
572a93a8f1 | ||
![]() |
878f4c8882 | ||
![]() |
b8c8775d13 | ||
![]() |
2ab3e0181c | ||
![]() |
36a2257a95 | ||
![]() |
71e0b2b489 | ||
![]() |
21f8672e3d | ||
![]() |
4bedba24bd | ||
![]() |
d13b76fa3e | ||
![]() |
62c45a4538 |
6 changed files with 76 additions and 34 deletions
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -3,6 +3,9 @@
|
|||
.env
|
||||
*.pyc
|
||||
__pycache__
|
||||
config.ini
|
||||
config.py
|
||||
tgchannel2toot.session
|
||||
downloads/
|
||||
nohup.out
|
||||
unknown_errors.txt
|
||||
tgchannel2toot.session-journal
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -1,3 +0,0 @@
|
|||
[pyrogram]
|
||||
api_id = 12345
|
||||
api_hash = 0123456789abcdef0123456789abcdef
|
|
@ -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
6
run.sh
|
@ -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
86
tgchannel2toot.py
Executable file → Normal 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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue