-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnexmo_sms.py
42 lines (30 loc) · 1.09 KB
/
nexmo_sms.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
import os
import vonage
from dotenv import load_dotenv
load_dotenv()
NEXMO_API_KEY = os.getenv('NEXMO_API_KEY')
NEXMO_API_SECRET = os.getenv('NEXMO_API_SECRET')
client = vonage.Client(key=NEXMO_API_KEY, secret=NEXMO_API_SECRET)
sms = vonage.Sms(client)
def send_nexmo_sms(to_phone_number, message_body):
"""
Sends an SMS message using Nexmo (Vonage) service.
Args:
to_phone_number (str): Recipient's phone number.
message_body (str): The SMS message to be sent.
Returns:
dict: Response from Nexmo with status and message details.
"""
response_data = sms.send_message({
'from': 'NEXMO',
'to': to_phone_number,
'text': message_body
})
if response_data["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response_data['messages'][0]['error-text']}")
return response_data
if __name__ == "__main__":
response = send_nexmo_sms('+XXXXXXXXXX', 'Hey, Steve trying to reach you!')
print(response)