add alerts to telegram channel

This commit is contained in:
Il'ya (Marshal) 2021-04-25 16:35:16 +02:00
parent 034322f52b
commit cf1d491005
4 changed files with 89 additions and 1 deletions

View file

@ -48,3 +48,10 @@ jobs:
git add .
git commit -m "Update content of files"
git push
- name: Send alert to Telegram channel.
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
run: |
git checkout main
python make_and_send_alert.py

View file

@ -46,3 +46,10 @@ jobs:
git add .
git commit -m "Update tracked links"
git push
- name: Send alert to Telegram channel.
env:
TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
run: |
git checkout main
python make_and_send_alert.py

View file

@ -53,7 +53,6 @@ Copy of Telegram websites stored **[here](https://github.com/MarshalX/telegram-c
### TODO list
- alert system;
- add storing history of content using hashes;
- add storing hashes of image, svg, video.

75
make_and_send_alert.py Normal file
View file

@ -0,0 +1,75 @@
import asyncio
import logging
import os
import aiohttp
TELEGRAM_BOT_TOKEN = os.environ['TELEGRAM_BOT_TOKEN']
REPOSITORY = os.environ.get('REPOSITORY', 'MarshalX/telegram-crawler')
CHAT_ID = os.environ.get('CHAT_ID', '@tgcrawl')
ROOT_TREE_DIR = os.environ.get('ROOT_TREE_DIR', 'data')
BASE_GITHUB_API = 'https://api.github.com/'
GITHUB_LAST_COMMITS = 'repos/{repo}/commits'
BASE_TELEGRAM_API = 'https://api.telegram.org/bot{token}/'
TELEGRAM_SEND_MESSAGE = 'sendMessage'
logger = logging.getLogger(__name__)
STATUS_TO_EMOJI = {
'added': '',
'modified': '📝',
'removed': '',
}
async def main():
async with aiohttp.ClientSession() as session:
url = f'{BASE_GITHUB_API}{GITHUB_LAST_COMMITS}'.format(repo=REPOSITORY)
params = {
'per_page': 1
}
async with session.get(url, params=params) as response:
commits = await response.json()
if not commits:
logger.error('No commits found')
return
last_commit = commits[0]
last_commit_url = last_commit['url']
changes = []
async with session.get(last_commit_url) as response:
json = await response.json()
html_url = json['html_url']
files = json.get('files', [])
for file in files:
filename = file['filename'].replace(f'{ROOT_TREE_DIR}/', '').replace('.html', '')
status = STATUS_TO_EMOJI[file['status']]
changes.append(f'{status} <code>{filename}</code>')
alert_text = [
'<b>New changes on Telegram sites</b>\n',
'\n'.join(changes) + '\n',
f'<a href="{html_url}">View diff on GitHub...</a>'
]
url = f'{BASE_TELEGRAM_API}{TELEGRAM_SEND_MESSAGE}'.format(token=TELEGRAM_BOT_TOKEN)
params = {
'chat_id': CHAT_ID,
'parse_mode': 'HTML',
'text': '\n'.join(alert_text),
}
async with await session.get(url, params=params) as response:
if response.status != 200:
params['text'] = f'<b>❗️ Too many new changes on Telegram sites</b>\n\n' \
f'<a href="{html_url}">View diff on GitHub...</a>'
await session.get(url, params=params)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())