-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtestsms.py
74 lines (53 loc) · 2.22 KB
/
testsms.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# print(f"Error: {e}")
import requests
import uuid
from requests.auth import HTTPBasicAuth
def send_post_request(url, username, password, body):
"""
Sends a POST request with basic authentication.
:param url: The URL to send the POST request to.
:param username: The username for basic authentication.
:param password: The password for basic authentication.
:param body: The body of the POST request.
:return: The response from the server.
"""
auth = HTTPBasicAuth(username, password)
headers = {'Content-Type': 'application/json'}
response = requests.post(url, auth=auth, json=body, headers=headers)
print("done1")
print(response)
return response
def send_sms(api_url, username, password, phone_number, message):
"""
Sends an SMS message using a POST request with basic authentication.
:param api_url: The URL of the SMS gateway API.
:param username: The username for basic authentication.
:param password: The password for basic authentication.
:param phone_number: The recipient's phone number.
:param message: The message to send.
:return: The response from the SMS gateway API.
"""
# Split the long message into chunks of 150 characters
chunks = [message[i:i+150] for i in range(0, len(message), 150)]
# Send up to two chunks
responses = []
for i, chunk in enumerate(chunks[:5]):
response = send_sms(api_url, username, password, phone_number, chunk)
responses.append(response)
body = {
"from": "RMNDR",
"to": phone_number,
"text": message[:150]
,
"reference": str(uuid.uuid4())
}
response = send_post_request(api_url, username, password, body)
return response
# Example usage
api_url = "http://sms.reubenwedson.site/api/sms/v1/text/single"
username = "SAMPLE"
password = "SAMPLE"
message = """
Based on the provided data, here's an analysis of the conditions:
Soil Moisture: The value is 45 which indicates that the soil is moderately moist. This is generally a good level for maize as the ideal soil moisture content is between 40% and 60%.
"""