Getting started with the Bot Connector
Each time a message is posted on one of the channels your bot is connected to, it receives a POST request on the endpoint you’ve set on the platform. To reply, you need to make a post request with your bot’s Request Token available in your bot settings. In this example, we use SDKs to make it simpler. :)
Receive messages and send “Hello world”
1. Copy-paste this snippet in a file.
2. Replace the REQUEST_TOKEN by your bot request token.
3. Install the dependencies by running the following command:
JS: npm install sapcai express body-parser
Python: pip install sapcai flask
Ruby: gem install Sapcai sinatra
PHP: composer require sapcai/sdk-php
var express = require('express')
var bodyParser = require('body-parser')
var sapcai = require('sapcai').default
var connect = new sapcai.connect('YOUR_REQUEST_TOKEN')
var app = express()
/* Server setup */
app.set('port', 5000)
app.use(bodyParser.json())
app.post('/', function(req, res) {
connect.handleMessage(req, res, onMessage)
})
function onMessage (message) {
// Get the content of the message
var content = message.content
// Get the type of the message (text, picture,...)
var type = message.type
// Add a reply, and send it
message.addReply([{ type: 'text', content: 'Hello, world' }])
message.reply()
}
app.listen(app.get('port'), function () { console.log('App is listening on port ' + app.get('port')) })
require 'sinatra'
require 'sapcai'
connect = Sapcai::Connect.new('YOUR_REQUEST_TOKEN')
set :port, 5000
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
from sapcai import Connect
from flask import Flask, request, jsonify
connect = Connect('YOUR_REQUEST_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(port='5000')
<?php
use Sapcai\Client;
// Start Slim server
$app = new \Slim\App();
// Instantiate the Connect Client
$connect = Client::Connect($_ENV["YOUR_REQUEST_TOKEN"]);
//Handle / route
$app->post('/', function ($request, $response) {
$connect->handleMessage($body, 'replyMessage');
});
function replyMessage ($message) {
// Get the content of the message
$text = $message->content;
// Get the type of the message (text, picture,...)
$type = $message->type;
$message->addReply([(object)['type' => 'text', 'content' => 'Hello, world']]);
$message->reply();
}
// Run Slim server
$app->run();