refacto
This commit is contained in:
parent
ab1a169d45
commit
618213572f
7 changed files with 260 additions and 96 deletions
36
message_sender.py
Normal file
36
message_sender.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import base64
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
|
||||
from menus import MenuMessageFormatter, Menus
|
||||
|
||||
|
||||
class SignalMessager:
|
||||
def __init__(self, api_url: str, user_number: str):
|
||||
self.api_url = api_url
|
||||
self.user_number = user_number
|
||||
|
||||
@staticmethod
|
||||
def create_headers() -> dict[str, str]:
|
||||
return {"Content-Type": "application/json"}
|
||||
|
||||
@staticmethod
|
||||
def create_attachment(pdf: Path):
|
||||
return base64.b64encode(pdf.read_bytes())
|
||||
|
||||
def send_message(self, message: str, pdf_file: Path, recipients: list[str]):
|
||||
headers = self.create_headers()
|
||||
attachment = self.create_attachment(pdf_file)
|
||||
|
||||
body = {
|
||||
"message": message,
|
||||
"text_mode": "styled",
|
||||
"number": self.user_number,
|
||||
"recipients": recipients,
|
||||
"base64_attachments": [
|
||||
f"data:application/pdf;filename=menu.pdf;base64,{attachment.decode()}"
|
||||
],
|
||||
}
|
||||
|
||||
requests.post(self.api_url, headers=headers, json=body)
|
Loading…
Add table
Add a link
Reference in a new issue