-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMasToTel.py
More file actions
64 lines (39 loc) · 1.44 KB
/
MasToTel.py
File metadata and controls
64 lines (39 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import mastodon
import telegram
from telegram.ext import Updater, CommandHandler, MessageHandler
mastodon_instance = 'https://mastodon.example.com'
mastodon_access_token = ' your_access_token_here'
telegram_token = 'your_telegram_token_here'
telegram_channel_id = 'your_telegram_channel_id_here'
m = mastodon.Mastodon(
access_token=mastodon_access_token,
api_base_url=mastodon_instance
)
bot = telegram.Bot(token=telegram_token)
def send_message(text, media=None):
if media:
bot.send_photo(chat_id=telegram_channel_id, photo=media, caption=text)
else:
bot.send_message(chat_id=telegram_channel_id, text=text)
statuses = m.timeline(timeline='home', limit=100)
for status in statuses:
content = status['content']
sender = status['account']['username']
sender_url = f'https://{mastodon_instance}/@{sender}'
media_url = None
for attachment in status['media_attachments']:
if attachment['type'] == 'image':
media_url = attachment['url']
text = f'{content}\n{sender} - {sender_url}'
send_message(text, media_url)
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text='Mastodon bot started!')
def main():
updater = Updater(telegram_token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()