Skip to content

Instantly share code, notes, and snippets.

@absyah
Last active November 11, 2024 07:44
Show Gist options
  • Save absyah/8e612fcaad81ea062d37910b379db768 to your computer and use it in GitHub Desktop.
Save absyah/8e612fcaad81ea062d37910b379db768 to your computer and use it in GitHub Desktop.
[
{
"destination": "destination1",
"events": [
{
"type": "delivery",
"delivery": {
"data": "001f277462529930889fab80ecffdc088390phoneNumber"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": false
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination2",
"events": [
{
"type": "delivery",
"delivery": {
"data": "002f277462529930889fab80ecffdc088390phoneNumber"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": false
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination3",
"events": [
{
"type": "delivery",
"delivery": {
"data": "003f277462529930889fab80ecffdc088390phoneNumber"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": false
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
},
{
"destination": "destination4",
"events": [
{
"type": "delivery",
"delivery": {
"data": "004f277462529930889fab80ecffdc088390phoneNumber"
},
"webhookEventId": "",
"deliveryContext": {
"isRedelivery": false
},
"timestamp": 0,
"source": {
"type": "user",
"userId": "U8189cf6745fc0d808977bdb0b9f22995"
},
"mode": "active"
}
]
}
]
# poetry run python send_events.py
import json
import requests
from requests_aws4auth import AWS4Auth
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor, as_completed
aws_access_key = ""
aws_secret_key = ""
aws_session_token = ""
body_file = "body.json"
url = "[base_url]/pnp-event?eeURL=asd1233e"
with open(body_file, "r") as f:
event_list = json.load(f)
# Function to send a single request
def send_event(event_data, phone_number):
auth = AWS4Auth(aws_access_key, aws_secret_key, "ap-northeast-1", "execute-api", session_token=aws_session_token)
event_copy = json.loads(json.dumps(event_data))
event_copy["events"][0]["delivery"]["data"] = f"{event_data['events'][0]['delivery']['data']}{phone_number}"
try:
response = requests.post(url, json=event_copy, auth=auth)
print(f"Phone number {phone_number} sent with status code: {response.status_code}")
except requests.RequestException as e:
print(f"Error sending phone number {phone_number}: {e}")
with ThreadPoolExecutor(max_workers=10) as executor: # Adjust max_workers as needed
futures = [
executor.submit(send_event, event_data, phone_number)
for event_data in event_list
for phone_number in range(1, 26)
]
for future in as_completed(futures):
future.result() # Trigger exception if occurred
print("All events have been sent in parallel based on body.json content.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment