main: new main loop
This commit is contained in:
parent
3e6f39ba86
commit
5a2ff14d35
69
main.py
69
main.py
@ -148,15 +148,13 @@ class SignalAPI:
|
|||||||
print("Failed to trust identity")
|
print("Failed to trust identity")
|
||||||
print(r.text)
|
print(r.text)
|
||||||
|
|
||||||
async def receive_messages(self):
|
async def websocket_connect_receive(self):
|
||||||
ws_apiurl = self.apiurl.replace("http", "ws")
|
ws_url = self.apiurl.replace("http", "ws")
|
||||||
|
async for websocket in websockets.connect(f"{ws_url}/v1/receive/{self.number}"):
|
||||||
|
yield websocket
|
||||||
|
|
||||||
async for websocket in connect(f"{ws_apiurl}/v1/receive/{config.number}"):
|
async def receive_messages(self, websocket) -> Result[Message, str]:
|
||||||
try:
|
return parse_response(Message, await websocket.recv())
|
||||||
print(parse_response(Message, await websocket.recv()))
|
|
||||||
#print(await websocket.recv())
|
|
||||||
except websockets.exceptions.ConnectionClosed:
|
|
||||||
print("Websockets connection closed. Reestablishing connection.")
|
|
||||||
|
|
||||||
def send_message(self, message: SendMessageSimple) -> Result[SendMessageResponse, str]:
|
def send_message(self, message: SendMessageSimple) -> Result[SendMessageResponse, str]:
|
||||||
data = message.model_dump()
|
data = message.model_dump()
|
||||||
@ -219,23 +217,21 @@ class LabCleaningBot:
|
|||||||
message = SendMessageSimple(message=message, recipients=[self.base_group])
|
message = SendMessageSimple(message=message, recipients=[self.base_group])
|
||||||
return self.api.send_message(message)
|
return self.api.send_message(message)
|
||||||
|
|
||||||
|
async def receiver(self, session: Session):
|
||||||
|
async for websocket in self.api.websocket_connect_receive():
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
message = await self.api.receive_messages(websocket)
|
||||||
|
print(message)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
except websockets.exceptions.ConnectionClosed:
|
||||||
|
print("Websockets connection closed. Reestablishing connection.")
|
||||||
|
|
||||||
with open("config.json", "r") as f:
|
async def sync_members_and_tasks(self, session: Session):
|
||||||
config = Config.model_validate(json.load(f))
|
|
||||||
|
|
||||||
api = SignalAPI(config.apiurl, config.number)
|
|
||||||
|
|
||||||
bot = LabCleaningBot(api, "group.bm5KT3NJUW5FdkpRNnR2ZGRFa01oOVZBeUYrVkdnd3NNTzFpNWdsR2pwUT0=")
|
|
||||||
|
|
||||||
engine = create_engine("sqlite:///data.db")
|
|
||||||
SQLModel.metadata.create_all(engine)
|
|
||||||
with Session(engine) as session:
|
|
||||||
while True:
|
while True:
|
||||||
sync_result = bot.sync_members_as_active_users(session)
|
sync_result = self.sync_members_as_active_users(session)
|
||||||
|
|
||||||
bot.api.update_group_members(bot.base_group, ["+4915773232355"])
|
self.api.update_group_members(self.base_group, ["+4915773232355"])
|
||||||
|
|
||||||
if is_err(sync_result):
|
if is_err(sync_result):
|
||||||
print(sync_result.unwrap_err())
|
print(sync_result.unwrap_err())
|
||||||
@ -244,7 +240,7 @@ if __name__ == "__main__":
|
|||||||
reqs = task.create_additional_requests(utc_now(), session)
|
reqs = task.create_additional_requests(utc_now(), session)
|
||||||
|
|
||||||
if is_err(reqs):
|
if is_err(reqs):
|
||||||
res = bot.send_to_base_group("Could not fulfill task: " + task.name)
|
res = self.send_to_base_group("Could not fulfill task: " + task.name)
|
||||||
|
|
||||||
if is_err(res):
|
if is_err(res):
|
||||||
print(res.unwrap_err())
|
print(res.unwrap_err())
|
||||||
@ -257,7 +253,7 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
for request in reqs:
|
for request in reqs:
|
||||||
message = SendMessageSimple(message=task.name, recipients=[request.user.name])
|
message = SendMessageSimple(message=task.name, recipients=[request.user.name])
|
||||||
res = api.send_message(message)
|
res = self.api.send_message(message)
|
||||||
|
|
||||||
if is_ok(res):
|
if is_ok(res):
|
||||||
timestamp = datetime.datetime.fromtimestamp(int(res.unwrap().timestamp)/1000)
|
timestamp = datetime.datetime.fromtimestamp(int(res.unwrap().timestamp)/1000)
|
||||||
@ -267,13 +263,28 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
exit(0)
|
|
||||||
|
|
||||||
res = bot.api.update_group_members("group.TTlKelhpUW1sUVJSU2Z2NDJpdjVWcllMTW93MTBNN2tseEtGaFkzQ1VsZz0=", groupinfo.members)
|
async def main(config: Config, session: Session):
|
||||||
|
api = SignalAPI(config.apiurl, config.number)
|
||||||
|
|
||||||
if is_err(res):
|
bot = LabCleaningBot(api, "group.bm5KT3NJUW5FdkpRNnR2ZGRFa01oOVZBeUYrVkdnd3NNTzFpNWdsR2pwUT0=")
|
||||||
print(res.unwrap_err())
|
|
||||||
continue
|
|
||||||
|
|
||||||
print(res.unwrap())
|
await asyncio.gather(
|
||||||
|
bot.receiver(session),
|
||||||
|
bot.sync_members_and_tasks(session)
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
with open("config.json", "r") as f:
|
||||||
|
config = Config.model_validate(json.load(f))
|
||||||
|
|
||||||
|
engine = create_engine("sqlite:///data.db")
|
||||||
|
SQLModel.metadata.create_all(engine)
|
||||||
|
|
||||||
|
with Session(engine) as session:
|
||||||
|
asyncio.run(main(config, session))
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
Loading…
Reference in New Issue
Block a user