updated waku send message to take an array of recipients w/ burnettk

This commit is contained in:
jasquat 2023-01-24 16:18:10 -05:00
parent aa11ea1eab
commit 7a4752498d
1 changed files with 34 additions and 23 deletions

View File

@ -29,32 +29,43 @@ class SendMessage:
message: str
message_type: str
recipient: str
recipient: list[str]
def execute(self, config, task_data):
"""Execute."""
url = f'{current_app.config["WAKU_BASE_URL"]}'
headers = {"Accept": "application/json", "Content-type": "application/json"}
request_body = {
"jsonrpc": "2.0",
"method": self.message_type,
"params": [{"id": self.recipient, "message": self.message}],
"id": 1,
}
responses = []
all_calls_returned_200 = True
for rec in self.recipient:
url = f'{current_app.config["WAKU_BASE_URL"]}'
headers = {"Accept": "application/json", "Content-type": "application/json"}
request_body = {
"jsonrpc": "2.0",
"method": self.message_type,
"params": [{"id": rec, "message": self.message}],
"id": 1,
}
status_code = 0
try:
raw_response = requests.post(url, json.dumps(request_body), headers=headers)
status_code = raw_response.status_code
parsed_response = json.loads(raw_response.text)
response = json.dumps(parsed_response)
except Exception as ex:
response = json.dumps({"error": str(ex)})
status_code = 500
status_code = 0
try:
raw_response = requests.post(url, json.dumps(request_body), headers=headers)
status_code = raw_response.status_code
if status_code != 200:
all_calls_returned_200 = False
parsed_response = json.loads(raw_response.text)
response = parsed_response
except Exception as ex:
response = {"error": str(ex)}
status_code = 500
all_calls_returned_200 = False
return {
"response": response,
"node_returned_200": True,
"status": status_code,
responses.append({
"response": response,
"node_returned_200": True,
"status": status_code,
})
return ({
"response": json.dumps(responses),
"node_returned_200": all_calls_returned_200,
"status": 200,
"mimetype": "application/json",
}
})