Send RCS with MaaP using Python
Sending RCS with the MaaP API using the Python programming language
The py-rcs-chatbot library can be used to make MaaP requests using Python to send RCS.
Python requirements and resources
Python is a programming language and can be used to quickly and easily add RCS support for programmatically sending and receiving RCS messages. Use it for transactional messaging and notifications between your application and mobiles.
The following are required to send RCS using Python:
Python
Python programming language
Requirements and resources
The following are required to send RCS with MaaP using Python:
py-rcs-chatbot
Python SDK for RCS MaaP chatbot
Code
The following example Python code makes a MaaP call to the API_URL endpoint to send a message in response to a message received from the user.
#!/usr/bin/python
# -*- coding: iso-8859-15 -*-
import flask
import logging
import rcs_chatbot
chatbot = rcs_chatbot.Chatbot(
"API_URL",
"BOT_ID",
"TOKEN",
None,
logging.DEBUG
)
app = flask.Flask(__name__)
@app.route('/', methods=['POST'])
def event():
try:
chatbot.processEvent(flask.request.get_json())
return "ok", 200
except maap.RequestFailed as ex:
print("Request failed: " + str(ex))
return "ok", 200
@chatbot.registerEventHandler(rcs_chatbot.EventType.MESSAGE)
def messageHandler(event):
userContact = None
chatId = None
if "userContact" in event["messageContact"]:
userContact = event["messageContact"]["userContact"]
if "chatId" in event["messageContact"]:
chatId = event["messageContact"]["chatId"]
contact = rcs_chatbot.MessageContact(userContact, chatId)
suggestions = rcs_chatbot.Suggestions()
suggestions.addReply("reply", "reply")
suggestions.addUrlAction("url", "url", "http://example.com")
chatbot.sendMessage(
contact,
"You wrote: " + event["RCSMessage"]["textMessage"],
suggestions
)
@chatbot.registerEventHandler(rcs_chatbot.EventType.ISTYPING)
def isTypingHandler(event):
print("isTypingHandler")
@chatbot.registerEventHandler(rcs_chatbot.EventType.MESSAGESTATUS)
def messageStatusHandler(event):
print("messageStatusHandler")
@chatbot.registerEventHandler(rcs_chatbot.EventType.FILESTATUS)
def fileStatusHandler(event):
print("fileStatusHandler")
@chatbot.registerEventHandler(rcs_chatbot.EventType.RESPONSE)
def responseHandler(event):
print("responseHandler")
@chatbot.registerEventHandler(rcs_chatbot.EventType.ALIAS)
def aliasHandler(event):
print("aliasHandler")
@chatbot.registerEventHandler(rcs_chatbot.EventType.NEWUSER)
def newUserHandler(event):
print("newUserHandler")
if __name__ == '__main__':
app.run(port=5000, debug=False)
Using in production
Whatever the language or API, you can send RCS and receive RCS between applications and mobiles for a wide range of uses.
We provide a wide range of CPaaS services and infrastructure to organisations, including cloud platforms that enable you to run your own SMS gateway.
Get in contact with us to find out more about CPaaS voice, messaging, video and identity from Melrose Labs.
Testing
For testing your application's RCS support when using the MaaP protocol, we recommend starting with the Melrose Labs RCS MaaP Simulator service to simulate RCS messaging to and from mobiles.
Get MaaP credentials for RCS MaaP Simulator
Alternative APIs and languages
Other languages covered in our tutorials that can be used for sending and receiving RCS with MaaP include: Node.js, cURL
Updated almost 2 years ago