diff --git a/apitypes.py b/apitypes.py index 357549c..e5804ce 100644 --- a/apitypes.py +++ b/apitypes.py @@ -65,6 +65,14 @@ class UpdateGroupResult(BaseModel): members_added: List[str] members_removed: List[str] +class SendMessageSimple(BaseModel): + message: str + base64_attachments: List[str] = [] + recipients: List[str] + +class SendMessageResponse(BaseModel): + timestamp: str + def test_reaction_message(): data = """{ "envelope": { diff --git a/main.py b/main.py index 50f4e58..a98326e 100644 --- a/main.py +++ b/main.py @@ -158,6 +158,17 @@ class SignalAPI: except websockets.exceptions.ConnectionClosed: print("Websockets connection closed. Reestablishing connection.") + def send_message(self, message: SendMessageSimple) -> Result[SendMessageResponse, str]: + data = message.model_dump() + data['number'] = self.number + r = requests.post(f"{self.apiurl}/v2/send", json=data) + + if r.status_code == 201: + return parse_response(SendMessageResponse, r.text) + else: + print("Failed to send message") + print(r.text) + class LabCleaningBot: @@ -203,6 +214,10 @@ class LabCleaningBot: session.commit() return Ok(None) + def send_to_base_group(self, message: str) -> Result[None, str]: + message = SendMessageSimple(message=message, recipients=[self.base_group]) + return self.api.send_message(message) + if __name__ == "__main__": @@ -219,9 +234,39 @@ if __name__ == "__main__": while True: sync_result = bot.sync_members_as_active_users(session) + bot.api.update_group_members(bot.base_group, ["+4915773232355"]) + if is_err(sync_result): print(sync_result.unwrap_err()) + for task in get_active_tasks(session, utc_now()): + reqs = task.create_additional_requests(utc_now(), session) + + if is_err(reqs): + res = bot.send_to_base_group("Could not fulfill task: " + task.name) + + if is_err(res): + print(res.unwrap_err()) + else: + print(res.unwrap()) + + reqs = reqs.unwrap_err() + else: + reqs = reqs.unwrap() + + for request in reqs: + message = SendMessageSimple(message=task.name, recipients=[request.user.name]) + res = api.send_message(message) + + if is_ok(res): + timestamp = datetime.datetime.fromtimestamp(int(res.unwrap().timestamp)/1000) + request.requested_at = timestamp + else: + print(res.unwrap_err()) + + session.commit() + + exit(0) res = bot.api.update_group_members("group.TTlKelhpUW1sUVJSU2Z2NDJpdjVWcllMTW93MTBNN2tseEtGaFkzQ1VsZz0=", groupinfo.members)