117 lines
3.7 KiB
Python
Executable File
117 lines
3.7 KiB
Python
Executable File
#!/bin/env python3
|
|
|
|
# scel-buc - scripts to send and receive messages through a Telegram bot
|
|
# Copyright (C) 2023 bursa-pastoris
|
|
#
|
|
# This file is part of scel-buc.
|
|
#
|
|
# scel-buc is free software: you can redistribute it and/or modify it under the
|
|
# terms of the GNU Affero General Public License as published by the Free
|
|
# Software Foundation, version 3 of the License.
|
|
#
|
|
# scel-buc is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
# details.
|
|
#
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
# along with scel-buc. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
import urllib.request
|
|
import json
|
|
from datetime import datetime as dt
|
|
from os.path import exists as path_exists
|
|
|
|
from constants import *
|
|
|
|
|
|
def get_updates(offset=0):
|
|
try:
|
|
with urllib.request.urlopen(f'{API}/getUpdates?offset={offset}') as request:
|
|
updates = json.loads(request.read())
|
|
with open(MESSAGES, 'a') as f:
|
|
for i in updates['result']:
|
|
if 'message' in i:
|
|
f.write(json.dumps(i['message'], indent=4)+',\n')
|
|
except urllib.error.HTTPError as e:
|
|
print(f'HTTP error {e.code}: {e.reason}')
|
|
if e.code == 401:
|
|
print('Maybe you are using an invalid token?')
|
|
|
|
|
|
def send_message(chat_id, text):
|
|
text = urllib.parse.quote(text)
|
|
with urllib.request.urlopen(f'{API}/sendMessage?chat_id={chat_id}&text={text}') as request:
|
|
reply = json.loads(request.read())
|
|
message = reply['result']
|
|
with open(MESSAGES, 'a') as f:
|
|
f.write(json.dumps(message, indent=4)+'\n')
|
|
|
|
|
|
def append_message(message):
|
|
# Extract the data
|
|
message_id = message['message_id']
|
|
sender_id = message['from']['id']
|
|
chat_id = message['chat']['id']
|
|
date = message['date']
|
|
text = message['text']
|
|
|
|
# Process the data
|
|
chat_file = CHAT_PATH+f'/{chat_id}.html'
|
|
date = dt.strftime(dt.utcfromtimestamp(date), '%Y-%m-%d %H:%M:%S') + ' UTC'
|
|
text = text.replace('\n','</p>\n<p>')
|
|
text = '<p>'+text+'</p>'
|
|
|
|
with open(chat_file, 'a') as f:
|
|
# Start the divs
|
|
f.write(f' <div class="message received">\n'
|
|
f' <p class="metadata">\n'
|
|
f' {sender_id} - {date} - Message ID: {message_id}\n'
|
|
f' </p>\n'
|
|
f' <div class="content">\n')
|
|
# Enter the content
|
|
f.write(text)
|
|
# End the divs
|
|
f.write(f' </div>\n'
|
|
f' </div>\n')
|
|
|
|
|
|
def generate_pages():
|
|
with open(MESSAGES,'r') as f:
|
|
messages = f.read()
|
|
# Make messages a single, valid JSON object
|
|
messages = messages.rstrip().rstrip(',')
|
|
messages = '['+messages+']'
|
|
# Read messages as JSON
|
|
messages = json.loads(messages)
|
|
|
|
# Generate chat files
|
|
chat_files = set()
|
|
for i in messages:
|
|
chat_id = i['chat']['id']
|
|
chat_file = CHAT_PATH+f'/{chat_id}.html'
|
|
chat_files.add(chat_file)
|
|
for i in chat_files:
|
|
with open(i, 'w') as f:
|
|
f.write(f'<html>\n'
|
|
f' <head>\n'
|
|
f' <link rel="stylesheet" href="./style.css" />\n'
|
|
f' <title>{chat_id}</title>\n'
|
|
f' </head>\n'
|
|
f'<body>\n'
|
|
f' <h1>{chat_id}</h1>\n')
|
|
|
|
# Write messages
|
|
for i in messages:
|
|
append_message(i)
|
|
|
|
# Complete chat files
|
|
for i in chat_files:
|
|
with open(i, 'a') as f:
|
|
f.write(f'</body>\n'
|
|
f'</html>\n')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
generate_pages()
|