-
Notifications
You must be signed in to change notification settings - Fork 20
03 Receive and send messages
You can use the Bot Connector API to connect your bot to multiple channels, such as Messenger, Slack or Kik.
Once you've connected your bot to channels on the platform, you can use this SDK to receive and send messages.
Start by instantiating either a Client or a Connect object, as shown below:
import sapcai
client = new sapcai.Client('YOUR_TOKEN')
connect = client.connect
# ...is the same as...
connect = sapcai.Connect('YOUR_TOKEN')
You can now connect your bot, access all incoming messages and send replies accordingly.
You can find more details on the message format your bot can send here.
from sapcai import Connect
from flask import Flask, request, jsonify
connect = Connect('YOUR_TOKEN')
def bot(request):
message = connect.parse_message(request)
# Get the content of the message
content = message.content
# Get the type of the message (text, picture,...)
type = message.type
# Add a reply, and send it
replies = [{ 'type': 'text', 'content': 'Hello, world' }]
connect.send_message(replies, message.conversation_id)
return jsonify(status=200)
app = Flask(__name__)
@app.route('/', methods=['POST'])
def root():
return bot(request)
app.run()
Connect with SAP Conversational AI
This is a small example using both the Connect and the Request APIs to receive messages from a channel, send the content to SAP Conversational AI to get back the bot reply, and send it back to the channel.
import sapcai
from flask import Flask, request, jsonify
client = sapcai.Client('YOUR_TOKEN')
def bot(request):
message = client.connect.parse_message(request)
# Get the content of the message
content = message.content
# Get the type of the message (text, picture,...)
type = message.type
# Get the sender_id, which we'll use as a conversation token.
conversation_token = message.sender_id
# If it's a text message...
if type == 'text':
# ...make a request to SAP Conversational AI to get the bot reply...
response = client.request.converse_text(content, conversation_token)
# ...extract the reply...
reply = response.reply
# ...and send it back to the channel
connect.send_message([{ 'type': 'text', 'content': reply}], message.conversation_id)
return jsonify(status=200)
app = Flask(__name__)
@app.route('/', methods=['POST'])
def root():
return bot(request)
app.run()
Method | Params | Return |
---|---|---|
send_message() | messages: Array[Dict], conversation_id: String | Dict: the API response |
This methods allow you to push a message to a specific conversation.
messages = [
{
'type': 'text',
'content': 'Roger that',
}
]
connect.send_message(messages, 'A_CONVERSATION_ID')
Method | Params | Return |
---|---|---|
broadcast_message() | messages: Array[Dict] | Dict: the API response |
This method allow you to push a message to all the conversations of your bot.
messages = [
{
'type': 'text',
'content': 'Roger that',
}
]
connect.broadcast_message(messages)
A Message object is what you receive when calling the Connect#parse_message method.
An instance of the Message class provides each of the following attributes:
Attributes | Type |
---|---|
conversation_Id | String: the id of the conversation this message belongs to |
content | String: the content of the message |
type | String: the type of the message |
chat_id | String: the native ID of the chat |
sender_id | String: the native ID of the message's sender |
attachment | Object: the raw content of the message |