forked from frodobots-org/earth-rovers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtm_client.py
37 lines (30 loc) · 1.2 KB
/
rtm_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import requests
import json
class RtmClient:
def __init__(self, auth_response_data):
self.app_id = auth_response_data.get("APP_ID")
self.channel = auth_response_data.get("CHANNEL_NAME")
self.token = auth_response_data.get("RTM_TOKEN")
self.uid = str(auth_response_data.get("USERID"))
def send_message(self, message: dict):
# Convert the message dictionary to a JSON string
message_json = json.dumps(message, separators=(',', ':'))
url = f"https://api.agora.io/dev/v2/project/{self.app_id}/rtm/users/{self.uid}/peer_messages"
headers = {
"x-agora-uid": self.uid,
"x-agora-token": self.token
}
payload = {
"destination": self.channel.replace('sdk_', '', 1),
"enable_offline_messaging": False,
"enable_historical_messaging": False,
"payload": message_json
}
response = requests.post(url, headers=headers, json=payload)
print(response)
print(response.status_code)
print(response.json())
if response.status_code == 200:
print("Message sent successfully")
else:
print(response.json())