TIP: Looking for a good code editor? Check out Notepad++ (Windows) or Atom (Mac)
- Copy/paste the code below in your editor
- Save the file in the folder you created in the preparation step as "spark_bot.js"
var Botkit = require('botkit');
var controller = Botkit.sparkbot({
public_address: process.env.public_address,
ciscospark_access_token: process.env.access_token,
secret: process.env.secret,
});
var bot = controller.spawn();
// Start Webserver to process incoming webhooks
controller.setupWebserver(process.env.port||3000,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver, bot);
});
// Process incoming messages
controller.hears('hello','direct_message,direct_mention', function(bot, message) {
bot.reply(message, 'Howdy!');
});
-
MAC: Run this command that sets the environment variables (non-presistent) and starts the script. In the first terminal window type:
access_token=BOT_ACCESS TOKEN public_address=https://xyz.ngrok.io node spark_bot.js
- replacing
BOT_ACCESS_TOKEN
with the bot token from the previous step - replacing
https://xyz.ngrok.io
with the unique NGROK URL created in the previous step
- replacing
-
WINDOWS: Run these commands to set the environment variables (non-persistent) and starts the script. In the first command prompt type:
First populate the environment variables: C:\Documents\Botkit> SET BOT_ACCESS_TOKEN="your_bot_access_token_here" C:\Documents\Botkit> SET PUBLIC_URL="your_unique_ngrok_URL_here" Then execute the bot script: C:\Documents\Botkit> node spark_bot.js
You should now see the code starting with a message similar to this:
NOTE the
Starting webserver on port 3000
andSUCCESSFULLY UPDATED CISCO SPARK WEBHOOKS
What did we do so far?
- Created a Spark Bot account.
- Started NGROK to tunnel webhook traffic to our local computer.
- Created & started the Bot code.
Let's test our Bot!
-
Open your Cisco Spark client
-
Test your bot script by setting up a 1:1 conversation with your bot. Your bot name should be [email protected], example:
[email protected]
-
Send message:
hello
to your bot Your bot should respond withHowdy!
CONGRATULATIONS! You have a working bot. NOW let's make it smarter!