GithubHelp home page GithubHelp logo

messagemedia / messages-python-sdk Goto Github PK

View Code? Open in Web Editor NEW
6.0 7.0 9.0 187 KB

MessageMedia Python SDK for sending and receiving messages

Home Page: https://developers.messagemedia.com/

License: Apache License 2.0

Python 100.00%
sms messaging messagemedia python sms-api sms-messages sms-gateway sms-client

messages-python-sdk's Introduction

MessageMedia Messages Python SDK

Pull Requests Welcome HitCount PyPI

The MessageMedia Messages API provides a number of endpoints for building powerful two-way messaging applications.

Isometric

Table of Contents

๐Ÿ” Authentication

Authentication is done via API keys. Sign up at https://developers.messagemedia.com/register/ to get your API keys.

Requests are authenticated using HTTP Basic Auth or HMAC. For Basic Auth, your API key will be basicAuthUserName and API secret will be basicAuthPassword. For HMAC, your API key will be hmacAuthUserName and API secret will be hmacAuthPassword. This is demonstrated in the Send an SMS example below.

โ‰๏ธ Errors

Our API returns standard HTTP success or error status codes. For errors, we will also include extra information about what went wrong encoded in the response as JSON. The most common status codes are listed below.

HTTP Status Codes

Code Title Description
400 Invalid Request The request was invalid
401 Unauthorized Your API credentials are invalid
403 Disabled feature Feature not enabled
404 Not Found The resource does not exist
50X Internal Server Error An error occurred with our API

๐Ÿ“ฐ Information

Slack and Mailing List

If you have any questions, comments, or concerns, please join our Slack channel: https://developers.messagemedia.com/collaborate/slack/

Alternatively you can email us at: [email protected]

Bug reports

If you discover a problem with the SDK, we would like to know about it. You can raise an issue or send an email to: [email protected]

Contributing

We welcome your thoughts on how we could best provide you with SDKs that would simplify how you consume our services in your application. You can fork and create pull requests for any features you would like to see or raise an issue. Please be aware that a large share of the files are auto-generated by our backend tool.

โญ Installation

Run the following command to install the SDK via pip:

pip install messagemedia-messages-sdk

๐ŸŽฌ Get Started

It's easy to get started. Simply enter the API Key and secret you obtained from the MessageMedia Developers Portal into the code snippet below and a mobile number you wish to send to.

Send an SMS

Destination (destinationNumber) and source number (sourceNumber) should be in the E.164 format. For example, +61491570156 NOT 0491570156. The code snippet below comprises of only the bare minimum parameters required to send a message. You can view the full list of parameters over here. Alternatively, you can refer this code snippet with all the parameters in use.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.models.send_messages_request import SendMessagesRequest
from message_media_messages.models.message import Message
from message_media_messages.models.format_enum import FormatEnum
from message_media_messages.models.source_number_type_enum import SourceNumberTypeEnum
from message_media_messages.models.status_enum import StatusEnum
from message_media_messages.exceptions.send_messages_400_response_exception import SendMessages400ResponseException
from message_media_messages.exceptions.api_exception import APIException
import dateutil.parser
import jsonpickle

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

messages_controller = client.messages
body = SendMessagesRequest()
body.messages = []

body.messages.append(Message())
body.messages[0].content = 'My first message'
body.messages[0].destination_number = '+61491570156'

try:
    result = messages_controller.send_messages(body)
    print(result)
except SendMessages400ResponseException as e:
    print(e)
except APIException as e:
    print(e)

Send an MMS

Destination (destinationNumber) and source number (sourceNumber) should be in the E.164 format. For example, +61491570156 NOT 0491570156. The code snippet below comprises of only the bare minimum parameters required to send a message. You can view the full list of parameters over here. Alternatively, you can refer this code snippet with all the parameters in use.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.models.send_messages_request import SendMessagesRequest
from message_media_messages.models.message import Message
from message_media_messages.models.format_enum import FormatEnum
from message_media_messages.models.source_number_type_enum import SourceNumberTypeEnum
from message_media_messages.models.status_enum import StatusEnum
from message_media_messages.exceptions.send_messages_400_response_exception import SendMessages400ResponseException
from message_media_messages.exceptions.api_exception import APIException
import dateutil.parser
import jsonpickle

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

messages_controller = client.messages
body = SendMessagesRequest()
body.messages = []

body.messages[0].content = 'My second message'
body.messages[0].destination_number = '+61491570158'
body.messages[0].format = FormatEnum.MMS
body.messages[0].media = ['https://images.pexels.com/photos/1018350/pexels-photo-1018350.jpeg?cs=srgb&dl=architecture-buildings-city-1018350.jpg']
body.messages[0].subject = 'This is an MMS message'

try:
    result = messages_controller.send_messages(body)
    print(result)
except SendMessages400ResponseException as e:
    print(e)
except APIException as e:
    print(e)

Get Status of a Message

You can get a messsage ID from a sent message by looking at the message_id from the response of the above example.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.exceptions.api_exception import APIException

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

messages_controller = client.messages
message_id = '877c19ef-fa2e-4cec-827a-e1df9b5509f7'

try:
    result = messages_controller.get_message_status(message_id)
    print(result)
except APIException as e:
    print(e)

Get replies to a message

You can check for replies that are sent to your messages

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.exceptions.api_exception import APIException

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

replies_controller = client.replies
try:
    result = replies_controller.check_replies()
    print(result)
except APIException as e:
    print(e)

Check Delivery Reports

This endpoint allows you to check for delivery reports to inbound and outbound messages.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.exceptions.api_exception import APIException

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

delivery_reports_controller = client.delivery_reports
try:
    result = delivery_reports_controller.check_delivery_reports()
    print(result)
except APIException as e:
    print(e)

Confirm Delivery Reports

This endpoint allows you to mark delivery reports as confirmed so they're no longer returned by the Check Delivery Reports function.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.models.confirm_delivery_reports_as_received_request import ConfirmDeliveryReportsAsReceivedRequest
from message_media_messages.exceptions.api_exception import APIException

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

delivery_reports_controller = client.delivery_reports
body = ConfirmDeliveryReportsAsReceivedRequest()
body.delivery_report_ids = ['011dcead-6988-4ad6-a1c7-6b6c68ea628d', '3487b3fa-6586-4979-a233-2d1b095c7718', 'ba28e94b-c83d-4759-98e7-ff9c7edb87a1']

try:
    result = delivery_reports_controller.confirm_delivery_reports_as_received(body)
    print(result)
except APIException as e:
    print(e)

Check credits remaining (Prepaid accounts only)

This endpoint allows you to check for credits remaining on your prepaid account.

from message_media_messages.message_media_messages_client import MessageMediaMessagesClient
from message_media_messages.exceptions.api_exception import APIException

auth_user_name = 'API_KEY'
auth_password = 'API_SECRET'
use_hmac_authentication = False

client = MessageMediaMessagesClient(auth_user_name, auth_password, use_hmac_authentication)

messages_controller = client.messages
try:
    result = messages_controller.check_credits_remaining()
    print(result)
except APIException as e:
    print(e)

๐Ÿ“• API Reference Documentation

Check out the full API documentation for more detailed information.

๐Ÿ˜• Need help?

Please contact developer support at [email protected] or check out the developer portal at developers.messagemedia.com

๐Ÿ“ƒ License

Apache License. See the LICENSE file.

messages-python-sdk's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

messages-python-sdk's Issues

AttributeError

While following the instructions listed on the 'Python in under 2 minutes' page, I ran into the following error:
Traceback (most recent call last): File "index.py", line 22, in <module> result = messages_client.create_send_messages(body) AttributeError: 'MessagesController' object has no attribute 'create_send_messages'

Python SDK is not working

Python example with my credentials and my phone number (+61 and so on):

root@ext-somehost-01:~  # ./sms.py
INFO:message_media_messages.controllers.messages_controller:create_send_messages called.
INFO:message_media_messages.controllers.messages_controller:Preparing query URL for create_send_messages.
INFO:message_media_messages.controllers.messages_controller:Preparing headers for create_send_messages.
INFO:message_media_messages.controllers.messages_controller:Preparing and executing request for create_send_messages.
INFO:message_media_messages.controllers.messages_controller:Merging global headers with endpoint headers for create_send_messages.
INFO:message_media_messages.controllers.messages_controller:Wrapping request and response in a context object for create_send_messages.
INFO:message_media_messages.controllers.messages_controller:Validating response for create_send_messages.
ERROR:message_media_messages.controllers.messages_controller:HTTP response not OK.
Traceback (most recent call last):
  File "build/bdist.linux-x86_64/egg/message_media_messages/controllers/messages_controller.py", line 359, in create_send_messages
    self.validate_response(_context)
  File "build/bdist.linux-x86_64/egg/message_media_messages/controllers/base_controller.py", line 120, in validate_response
    raise APIException('HTTP response not OK.', context)
APIException: HTTP response not OK.
Traceback (most recent call last):
  File "./sms.py", line 23, in <module>
    result = messages_client.create_send_messages(body)
  File "build/bdist.linux-x86_64/egg/message_media_messages/controllers/messages_controller.py", line 359, in create_send_messages
  File "build/bdist.linux-x86_64/egg/message_media_messages/controllers/base_controller.py", line 120, in validate_response
message_media_messages.exceptions.api_exception.APIException: HTTP response not OK.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.