Advertisement
kwabenasapong

SMS v3 API using python3

May 30th, 2023
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | Source Code | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. '''
  4. This is a test script to send a message using the MTN API
  5. AppName = bulkSMSSender
  6. '''
  7.  
  8. import requests
  9.  
  10. # get access token for MTN API
  11. import requests
  12.  
  13. url = "https://api.mtn.com/v1/oauth/access_token?grant_type=client_credentials"
  14. payload = "client_id=OtYI2bLjnInCpyW1K6keohVZRKKIsz1f&client_secret=Hd3bmuAjAm44Ddmx"
  15. headers = {
  16.     'Content-Type': "application/x-www-form-urlencoded"
  17.     }
  18.  
  19. token_response = requests.request("POST", url, data=payload, headers=headers)
  20.  
  21. # get access token using get() method
  22. token = token_response.json().get('access_token')
  23.  
  24. print(token_response.text)
  25.  
  26. print('token: ', token)
  27. print('---------------------------------')
  28.  
  29. # send message using the access token
  30. url = "https://api.mtn.com/v3/sms/messages/sms/outbound"
  31.  
  32. payload = {
  33.     "senderAddress": "VALCO",
  34.     "receiverAddress": ["233548257283", "233202419977"],
  35.     "message": "Test message from VALCO",
  36.     "clientCorrelatorId": "test123",
  37.     "keyword": "string",
  38.     "serviceCode": "1000",
  39.     "requestDeliveryReceipt": False
  40. }
  41. headers = {
  42.     "Content-Type": "application/json",
  43.     "Authorization": "Bearer " + token
  44. }
  45. print(headers)
  46.  
  47. try:
  48.     response = requests.request("POST", url, json=payload, headers=headers)
  49.     print(response.status_code)
  50.     message = response.json()
  51.     print(response.text)
  52. except:
  53.     print("Message not sent")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement