su-automod/functions/send_message.py

66 lines
2.3 KiB
Python
Executable File

import requests
from functions.roll_token import roll_token
import var
from beloved_logger import logger
async def send_message(text, chat_id=None):
if chat_id is None:
logger.debug(f"No chat id was given, using {var.chat_number}")
chat_id = int(var.chat_number)
logger.info(f"Sending message to {chat_id}")
logger.debug(f"Message contents: {text}")
await roll_token()
if type(chat_id) != int:
cookies = {
var.cookie_name: var.cookie_value,
'sketchers_united_session': var.session
}
headers = {
'User-Agent': 'automod',
'Accept': 'application/json, text/plain, */*',
}
response = requests.get(f'https://{var.site}/chats/@{chat_id}',
cookies=cookies,
headers=headers)
if response.status_code != 200 and response.status_code != 201:
logger.error(f"There was an issue with getting an id from the {chat_id} "
f"with error {response.status_code}")
return False
chat_id = response.json()['id']
cookies = {
var.cookie_name: var.cookie_value,
'sketchers_united_session': var.session,
'XSRF-TOKEN': var.xsrf_token
}
headers = {
'User-Agent': 'automod',
'Accept': 'application/json, */*',
'X-CSRF-TOKEN': var.csrf_token,
'X-XSRF-TOKEN': var.xsrf_token,
'Content-Type': 'multipart/form-data; boundary=---------------------------9051447283201882072943098316'
}
data = (f'-----------------------------9051447283201882072943098316'
f'\r\nContent-Disposition: form-data; '
f'name="text"\r\n\r\n{text}'
f'\r\n-----------------------------9051447283201882072943098316--\r\n').encode()
response = requests.post(f'https://{var.site}/chats/{chat_id}/messages', cookies=cookies,
headers=headers,
data=data)
logger.info(f"Sending message concluded with response {response.status_code}")
if response.status_code != 200 and response.status_code != 201:
logger.error(f"There was an issue with sending a message to the {chat_id} "
f"with error {response.status_code}")
return False