Skip to content
This repository has been archived by the owner on Aug 12, 2022. It is now read-only.

03 Receive and send messages

EL KESRI Othmane edited this page Jan 15, 2019 · 3 revisions

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.

Usage

Start by instantiating either a Client or a Connect object, as shown below:

require 'sapcai'

client = new Sapcai::Client.new('YOUR_TOKEN')
connect = client.connect

# ...is the same as...

connect = Sapcai::Connect.new('YOUR_TOKEN')

Connect

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.

Sinatra
require 'sinatra'
require 'sapcai'

connect = Sapcai::Connect.new('YOUR_TOKEN')

post '/' do
	connect.handle_message(request) do |message|
		# 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)
	end
end

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.

require 'sinatra'
require 'sapcai

client = Sapcai::Client.new('YOUR_TOKEN')

post '/' do
	client.connect.handle_message(request) do |message|
		# 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)
		end
	end
end

Methods

Send messages to a specific conversation

Method Params Return
send_message() messages: Array[Hash], conversation_id: String Object: 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')

Broadcast messages to all bot conversations

Method Params Return
broadcast_message() messages: Array[Hash] Object: 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)

Message

A Message object is what you receive in the Connect#handle_message block.

Attributes

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