Skip to content

Instantly share code, notes, and snippets.

@areski
Created November 28, 2016 22:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save areski/7433e7d146d4637f03d6fee62a288207 to your computer and use it in GitHub Desktop.
Save areski/7433e7d146d4637f03d6fee62a288207 to your computer and use it in GitHub Desktop.
Send SMS via MessageMedia API
import hmac
import hashlib
import requests
from datetime import datetime
utc_str_date = datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT")
secret = 'SECRET'
api_key = "API KEY"
url = 'https://rest.messagemedia.com/v1/messages'
# content = <HTTP-Verb> + "\n" + <Content-Type> + "\n" + <Date> + "\n" + <RequestPath> + "\n" + <RequestBody>
content = "Hello my world!!!"
destination_number = "+34650112233"
request_body = """
{
"messages": [
{
"content" : "Helloworld!",
"destination_number" : "+34650112233"
}
]
}
"""
content = "POST" + "\n" + "application/json" + "\n" + utc_str_date + "\n" + '/v1/messages' + "\n" + request_body
signing = hmac.new(secret, content, hashlib.sha1)
# Authorization: MM-1 <API Key> <HMAC>
headers = {'Authorization': "MM-1 " + api_key + " " + signing.digest()}
# response = requests.get(url, headers=headers)
response = requests.post(url, headers=headers, data={'content': 'Helloworld!', 'destination_number': '+34650112233'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment