Start building your own chatbot now >
Note: This article is deprecated. It uses Recast.AI API version 1.

In this tutorial we are going to see, step by step, how to integrate Kik and Recast.AI in a Nodejs application.

At the end of this tutorial, you will be able to use Kik for every bot you ever want to build. This is a simple tutorial, so even if you don’t have an extensive knowledge of NodeJS, you can do this.

First of all, what is Kik?

For those of who don’t know, Kik is a messaging app, much like Facebook Messenger or Whatsapp. The company has a solid base of 240 millions register users. You can browse the Kik botshop to find inspiration for your bot!

1) Create your Kik account

  •  Take out your phone and download the Kik app. It does not have a desktop version. 
Tutorial: screenshot of play store kik page

Just follow the steps on the kik app in order to create your account

  • You’ll find here the code you need to have access rights to your bot
  • Then go to Settings > your Kik code and use the picture icon to scan the code of your browser.

Tutorial: screenshot setting of kik application

  • Go on the page Configuration and copy your API Key:

Tutorial: screenshotkik configuration

 

Now that your Kik account is created, we have one last thing to do before jumping into the code, and that’s creating your Recast.AI account. Recast.AI is a Natural Language Processing platform allowing you to create truly adaptable bots. If you’re already using another Natural Language Processing platform, you can skip to the third part directly. 

2) Create your  Recast.AI account:

  • Sign up on the platform and log in.
  • Click on ‘NEW BOT’ to create your bot.

Recast.AI account

To allow your bot to understand you, you need to create (or fork) your first ‘intent’. An intent In each intent, you’ll input different expression the user could use daily.

  • Fork your first intent: here, we’re forking the intent “greeting’’. This intent will take care of all the different ways to say hello. 

Create Intent

Forck Intent

 

  • Chose the one you like and click on the Fork button.

 

Intent page

Even if one example can be enough, the more examples your provide, the better the classification will be. And easier will it be for your bot to understand the sentence.

  • Try your first intent:

On the right side, use the console to see how your bot reacts to a sentence.

Console

  • Configure your bot:

Here, you find everything you need to manage the bot. In this tutorial, you’re mostly interested in the ‘Request access token’ , as we need it for the next part!

Setting tab

3) Jumping into the code:

The entire tutorial code is on this repo. Feel free to have a look, and go over our SDKs as well here, they can help. 

Now let’s start the real stuff.

  • First of all, you need to get ngrok on your computer. Ngrok helps you creating tunnels from your phone to your localhost.
  • Go on this link and download the appropriate version: https://ngrok.com/download
  • Launch ngrok on your terminal
./ngrok http 8080

I am listening on 8080 because it’s the config of our server in bot.js, feel free to change that if needed.

Tutorial: Ngrok screenshot Leave your server running and keep this uri in mind. You will need it for the rest of the tutorial. Be careful, as each time you restart the server, the address changes.

  • Install the dependencies:
mkdir mybot && cd mybot
npm init
npm install --save recastai
npm install --save @kikinteractive/kik
  •  Configure your tokens and ids:

I like to separate all my login details in a config.js file, but you can do as you wish.

  • Create a config.js file
touch config.js
  • config.js:
    let config = {}
    config.recast = {}
    config.recast.request_token = 'RECAST-TOKEN'
    config.recast.language = 'en' // to chose your language 'fr' or 'en' config.kik = {} config.kik.username = 'KIK-USERNAME' //Username of the bot config.kik.apiKey = 'KIK-APIKEY' //find it on the Configuration page on 'https://dev.kik.com' config.kik.baseUrl = 'NGROK-URL' //This is you ngrok url you got when you lunch your server module.exports = config
  • Create a bot.js file (it will be the main file of your app)
touch bot.js
  • bot.js:
 import { Client } from 'recastai'
import Kik from '@kikinteractive/kik'
import http from 'http'
import config from '../config.js'


const client = new Client(config.recast.request_token, config.recast.language)

const bot = new Kik({
 username: config.kik.username,
 apiKey: config.kik.apiKey,
 baseUrl: config.kik.baseUrl,
})

  • client.textConverse(message.body, { conversation_token: message.chatid }) To use this method you need to pass the user’s input and a unique conversation token. This token can be the message.chatid of the messenger chat. This token will create for each users a specific conversation with your bot.
  • res.reply() To get the first reply of your bot.
  • res.replies To get an array of all your replies.
  • res.action Get the object action. When an action is complete you will have the action.done = true and you will be able to trigger a specific behavior.

 

bot.onTextMessage[1]message) => {
  client.textConverse(message.body, { conversationToken: message.chatid }).then((res) => {
    const reply = res.reply()               /* To get the first reply of your bot. */
    const replies = res.replies             /* An array of all your replies */
    const action = res.action               /* Get the object action. You can use 'action.done' to trigger a specification action when it's at true. */

    if (!reply) {
      message.reply('i dont\'t get it :(')
    } else {
      if (action && action.done === true) {
        console.log('action is done')
        // Use external services: use res.memory('knowledge') if you got a knowledge from this action
      }
      replies.forEach(rep => message.reply(rep
    }
  }).catch((err) => {
    console.log(err)
  })
})

 Result of this code
Tutorial: Result of the code

Great! You now know how to integrate a Recast.AI bot on Kik. 🙂

Feel free to ask any question in the comment section or join us on our Slack Community ! You’ll find the rest of our bot tutorials here

References   [ + ]

1. message) => { client.textConverse(message.body, { conversationToken: message.chatid }).then((res) => { const reply = res.reply() /* To get the first reply of your bot. */ const replies = res.replies /* An array of all your replies */ const action = res.action /* Get the object action. You can use 'action.done' to trigger a specification action when it's at true. */ if (!reply) { message.reply('i dont\'t get it :(') } else { if (action && action.done === true) { console.log('action is done') // Use external services: use res.memory('knowledge') if you got a knowledge from this action } replies.forEach(rep => message.reply(rep

Want to build your own conversational bot? Get started with Recast.AI !

Subscribe to our newsletter


There are currently no comments.