Professional Documents
Culture Documents
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
@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"]
suggestions = rcs_chatbot.Suggestions()
suggestions.addReply("reply", "reply")
chatbot.sendMessage(
contact,
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)
Instead of a simple message, you can also use sendMessage to send
an AudioMessage , etc. Show below is how to send
a RichcardCarousel message.
@chatbot.registerEventHandler(maap.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"]
card1 = maap.Richcard()
card1.setTitle("title")
card2 = maap.Richcard()
card2.setTitle("title")
carousel = maap.RichcardCarousel()
carousel.addRichcard(card1)
carousel.addRichcard(card2)
chatbot.sendMessage(
contact,
carousel
Usage
Chatbot Functions
constructor
getMessageStatus(messageId)
updateMessageStatus(messageId, status)
getContactCapabilities(userContact, chatId)
Uploads a file of type fileType to the MaaP content storage until it expires at
date until Either path or url needs to be specified.
Deletes a file with identifier fileId from the MaaP content storage.
getFile(fileId)
Gets info for a file with identifier fileId from the MaaP content storage.
startTyping(recipient)
stopTyping(recipient)
processEvent(json)
The main middleware for your bot's webhook. It parses the message payload,
and fire the appropriate event.
FileMessage Functions
constructor(fileUrl)
setFileName(fileName)
setFileMIMEType(fileMIMEType)
setFileSize(fileSize)
setThumbnailUrl(thumbnailUrl)
Set the thumbnailUrl property.
setThumbnailFileName(thumbnailFileName)
setThumbnailMIMEType(thumbnailMIMEType)
setThumbnailFileSize(thumbnailFileSize)
AudioMessage Functions
setFileName(fileName)
setFileMIMEType(fileMIMEType)
setFileSize(fileSize)
setPlayingLength(playingLength)
GeolocationPushMessage
Property Type Mandatory Description
pos
This are the coordinates in WGS 84 (latitude, longitude) decimal notation. Exa
String Yes
"26.118 1289 - 80.1283921"
label String No This can be used to tag the nature of the location.
Property Type Mandatory Description
timestamp String No This is the time when the location information was pushed.
expiry
This is an absolute date at which time the recipient is no longer permitted to p
String No
the location information.
timeOffset
This is the time zone where the location information was pushed, expressed a
Number No
number of minutes away from UTC.
constructor(pos)
pos - String: This are the coordinates in WGS 84 (latitude, longitude) decimal
notation.
setLabel(label)
label - String: This can be used to tag the nature of the location.
setTimestamp(timestamp)
timestamp - String: This is the time when the location information was
pushed.
setExpiry(expiry)
timeOffset - Number: This is the time zone where the location information
was pushed, expressed as the number of minutes away from UTC.
setRadius(radius)
Richcard Functions
constructor()
setCardOrientation(cardOrientation)
cardOrientation - String:
Either rcs_chatbot.CardOrientation.HORIZONTAL or rcs_chatbot.CardOrie
ntation.VERTICAL .
setImageAlignment(imageAlignment)
imageAlignment - String:
Either rcs_chatbot.ImageAlignment.LEFT or rcs_chatbot.ImageAlignment.
RIGHT .
setMedia(mediaUrl, mediaContentType, mediaFileSize, height, [thumbnailUrl],
[thumbnailContentType], [thumbnailFileSize], [contentDescription])
setTitle(title)
setDescription(description)
setSuggestions(suggestions)
constructor()
setCardWidth(cardWidth)
cardWidth - String:
Either rcs_chatbot.CardWidth.SMALL or rcs_chatbot.CardWidth.MEDIUM .
addRichcard(richcard)
Suggestions Functions
Suggested Actions are grouped into seven different categories supporting a total
of twelve different suggested actions:
urlAction:
openUrl - Opens a web site or app via deep linking.
dialerAction:
dialPhoneNumber - Calls a phone number via the user's dialer app.
dialEnrichedCall - Start an Enriched Call via the user’s dialer app.
dialVideoCall - Start a video call via the user’s dialer app.
mapAction:
showLocation - Show location(s) on a map for given coordinates or
search query.
requestLocationPush - Request for a one-time geo location push.
calendarAction:
createCalendarEvent - Creates a new event on the user's calendar.
composeAction:
composeTextMessage - Compose a draft text message.
composeRecordingMessage - Compose a draft message and start
recording audio or video.
deviceAction:
requestDeviceSpecifics - Request for a one-time share of device
specifics (device model, operating system version, messaging client
identifier and version, and remaining battery charge in minutes).
settingsAction:
disableAnonymization - Ask the user to disable the anonymization
setting.
enableDisplayedNotifications - Ask the user to enable sending displayed
notifications.
Most actions allow fallback URLs in case a user does not have any app of the
required type installed. Chatbot platforms can use the fallback URL to suggest an
appropriate app to the user.
constructor()
addReply(displayText, postbackData)
On-the-wire example:
"reply": {
"displayText": "Yes",
"postback": {
"data": "set_by_chatbot_reply_yes"
}
}
On-the-wire example:
"action": {
"urlAction": {
"openUrl": {
"url": "https://www.google.com"
},
"postback": {
"data": "set_by_chatbot_open_url"
"action": {
"dialerAction": {
"dialPhoneNumber": {
"phoneNumber": "+1650253000"
},
"postback": {
"data": "set_by_chatbot_dial_phone_number"
},
"action": {
"dialerAction": {
"dialEnrichedCall": {
"phoneNumber": "+1650253000",
},
"postback": {
"data": "set_by_chatbot_dial_enriched_call"
},
"action": {
"dialerAction": {
"dialVideoCall": {
"phoneNumber": "+1650253000"
},
"data": "set_by_chatbot_dial_video_call"
addRequestLocationPushMapAction(displayText, postbackData)
On-the-wire example:
"action": {
"mapAction": {
"requestLocationPush": {}
},
"postback": {
"data": "set_by_chatbot_request_location_push"
}
}
On-the-wire example:
"action": {
"mapAction": {
"showLocation": {
"location": {
"latitude": 37.4220041,
"longitude": -122.0862515,
"label": "Googleplex"
},
"fallbackUrl": "https://www.google.com/maps/@37.4219162,-
122.078063,15z"
},
"postback": {
"data": "set_by_chatbot_show_location"
},
"action": {
"mapAction": {
"showLocation": {
"location": {
"query": "restaurants"
},
"fallbackUrl":
"https://www.google.com/maps/search/restaurants"
},
"postback": {
"data": "set_by_chatbot_search_locations"
}
}
On-the-wire example:
"action": {
"calendarAction": {
"createCalendarEvent": {
"startTime": "2017-03-14T00:00:00Z",
"endTime": "2017-03-14T23:59:59Z",
"title": "Meeting",
},
"postback": {
"data": "set_by_chatbot_create_calendar_event"
}
On-the-wire example:
"action": {
"composeAction": {
"composeTextMessage": {
"phoneNumber": "+1650253000",
},
"postback": {
"data": "set_by_chatbot_compose_text_message"
}
}
On-the-wire example:
"action": {
"composeAction": {
"composeRecordingMessage": {
"phoneNumber": "+1650253000",
"type": "VIDEO"
},
"postback": {
"data": "set_by_chatbot_compose_recording_message"
}
addDeviceAction(displayText, postbackData)
On-the-wire example:
"action": {
"deviceAction": {
"requestDeviceSpecifics": {}
},
"postback": {
"data": "set_by_chatbot_request_device_specifics"
On-the-wire example:
"action": {
"settingsAction": {
"disableAnonymization": {}
},
"postback": {
"data": "set_by_chatbot_disable_anonymization"
},
"action": {
"settingsAction": {
"enableDisplayedNotifications": {}
},
"postback": {
"data": "set_by_chatbot_enable_displayed_notifications"
}
}
Events
A different event is triggered for each type of event an RCS chatbot can receive.
Below are all the possible events with an example of the payload they provide.
EventType.MESSAGE
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.MESSAGE)
def messageHandler(event):
print("messageHandler")
Sample payload:
"RCSMessage": {
"msgId": "Xs8CI3tdf",
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567"
},
"event": "message"
EventType.ISTYPING
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.ISTYPING)
def isTypingHandler(event):
print("isTypingHandler")
Sample payload:
"RCSMessage": {
"msgId": "Xs8CI3tdf",
"isTyping": "active",
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567"
},
"event": "isTyping"
EventType.MESSAGESTATUS
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.MESSAGESTATUS)
def messageStatusHandler(event):
print("messageStatusHandler")
Sample payload:
{
"RCSMessage": {
"msgId": "MzJmajlmamVzZGZ8bmk5MHNlbmRmZTAz",
"status": "displayed",
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567"
},
"event": "messageStatus"
EventType.FILESTATUS
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.FILESTATUS)
def fileStatusHandler(event):
print("fileStatusHandler")
Sample payload:
{
"file": {
"fileId": "MzJmajlmamVzZGZ8bmk5MHNlbmRmZTAz",
"fileUrl": "http://www.example.com/files/f.jpg",
"fileSize": 123456,
"status": "ready",
"validity": "2017-10-03T22:31:00.597Z"
},
"event": "fileStatus"
EventType.RESPONSE
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.RESPONSE)
def responseHandler(event):
print("responseHandler")
Sample payload:
{
"RCSMessage": {
"msgId": "MzJmajlmamVzZGZ8bmk5MHNlbmRmZTAz",
"suggestedResponse": {
"response": {
"action": {
"postback": {
"data": "set_by_chatbot_reply_yes"
},
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567"
},
"event": "response"
EventType.ALIAS
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.ALIAS)
def aliasHandler(event):
print("aliasHandler")
Sample payload:
"RCSMessage": {
"msgId": "MzJmajlmamVzZGZ8bmk5MHNlbmRmZTAz",
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567",
"chatId": "93JF93SEIJFE"
},
"event": "alias"
EventType.NEWUSER
Example usage:
@chatbot.registerEventHandler(rcs_chatbot.EventType.NEWUSER)
def newUserHandler(event):
print("newUserHandler")
Sample payload:
"RCSMessage": {
"msgId": "MzJmajlmamVzZGZ8bmk5MHNlbmRmZTAz",
"suggestedResponse": {
"response": {
"reply": {
"postback": {
"data": "new_bot_user_initiation"
},
"timestamp": "2017-09-26T01:33:20.315Z"
},
"messageContact": {
"userContact": "+14251234567"
},
"event": "newUser"
To Do