-
Notifications
You must be signed in to change notification settings - Fork 7
/
telegram_handler.py
65 lines (58 loc) · 2.97 KB
/
telegram_handler.py
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
65
import logging
import os
import telegram
from datetime import datetime, timedelta, timezone
from sftp_handler import sftp_checkpoint_utils
# Create logger
logger = logging.getLogger("__main__.telegram_handler")
async def send_posts_to_channel(posts):
# Start Telegram bot
bot = telegram.Bot(os.getenv("TELEGRAM_BOT_TOKEN"))
# Get sent posts and last post date to get new posts
sent_posts = sftp_checkpoint_utils('r')
if sent_posts:
last_post = list(sent_posts)[-1].strip()
last_post_date_str = last_post.split(",")[-1]
last_post_date = datetime.fromisoformat(last_post_date_str).astimezone(timezone.utc)
else:
last_post_date = (datetime.utcnow() - timedelta(days=1)).astimezone(timezone.utc)
logger.info("Looking for new Instagram posts")
channel_id = os.getenv("TELEGRAM_CHANNEL_ID")
new_posts = []
for post in posts:
if post.taken_at > last_post_date and f'{post.pk},{post.code},{post.taken_at}' not in sent_posts:
if post.media_type == 1:
logger.info("Sending new picture post to Telegram channel")
photo = post.thumbnail_url
caption = post.caption_text
await bot.send_photo(chat_id=channel_id, photo=photo, caption=caption)
if post.media_type == 2:
logger.info("Sending new video post to Telegram channel")
video = post.video_url
caption = post.caption_text
await bot.send_video(chat_id=channel_id, video=video, caption=caption)
elif post.media_type == 8:
logger.info("Sending new album post to Telegram channel")
media_group = []
caption = post.caption_text
caption_added = False
for p in post.resources:
if p.media_type == 1:
if not caption_added:
media_group.append(telegram.InputMediaPhoto(p.thumbnail_url, caption=caption))
caption_added = True
else:
media_group.append(telegram.InputMediaPhoto(p.thumbnail_url))
elif p.media_type == 2:
if not caption_added:
media_group.append(telegram.InputMediaVideo(p.video_url, caption=caption))
caption_added = True
else:
media_group.append(telegram.InputMediaVideo(p.video_url))
await bot.send_media_group(chat_id=channel_id, media=media_group)
# Append new posts to sent posts list
new_posts.append(f'{post.pk},{post.code},{post.taken_at}')
# Sort new posts to keep them sorted in the checkpoint file
new_posts_sorted = sorted(new_posts, key=lambda x: datetime.fromisoformat(x.split(",")[-1]))
sftp_checkpoint_utils('w', new_posts_sorted)
sftp_checkpoint_utils('d')