Kik bot API for Node.js
npm install @kikinteractive/kik[![NPM Version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url]
Use this library to communicate with the Kik API to develop a bot for [Kik Messenger][kik-url]. Got to [dev.kik.com][dev-kik-url] to learn more and start building a bot.
- Install with [npm install @kikinteractive/kik][npm-url]
This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code. Please report unacceptable behavior to bots@kik.com.
If you're looking to contribute to the kik package, check out the Contributing Guide.
Here are other resources for using Kik node:
- [stackoverflow.com][stackoverflow-url] is a great place to get answers about building a Kik chat bot.
- Go to [dev.kik.com][dev-kik-url] to get started building a bot, scan the code at dev.kik.com and talk to Botsworth.
The Kik bot library is released under the terms of the MIT license. See License for more information or see https://opensource.org/licenses/MIT.
Creating a basic Kik bot is simple:
1. Import @kikinteractive/kik
2. Create a bot with the username and API key you got from https://dev.kik.com/
3. Configure your bot as described in the [documentation][dev-config-kik-url]
4. Add the bot as middleware to your server with bot.incoming()
5. Start your web server
You can use any node-style web server to host your Kik bot. Add handlers to your bot by calling bot.onTextMessage(...) and get notified whenever a user messages you. Take action on the messages as they come in and call message.reply(...) to respond to the message within the chat the message was sent from.
Check out the full API documentation for more advanced uses.
``javascript
'use strict';
let util = require('util');
let http = require('http');
let Bot = require('@kikinteractive/kik');
// Configure the bot API endpoint, details for your bot
let bot = new Bot({
username: 'echo.bot',
apiKey: '7b939d69-e840-4d22-aab8-4188c2198f8a',
baseUrl: 'https://kik-echobot.ngrok.io/'
});
bot.updateBotConfiguration();
bot.onTextMessage((message) => {
message.reply(message.body);
});
// Set up your server and start listening
let server = http
.createServer(bot.incoming())
.listen(process.env.PORT || 8080);
`
You can send a targeted message to a user once they have subscribed to your bot. If you want to send someone a message, just call bot.send(...) with their username. You don't need to specify a chat ID here since you are sending it directly to the user, not within a specific chat.
`javascript
// To one user (a.username)
bot.send(Bot.Message.text('Hey, nice to meet you!'), 'a.username');
// You can use a shorthand for text messages to keep things a bit cleaner
bot.send('Getting started is super easy!', 'a.username');
`
If you want to send a photo to a user you can send a picture message. The API will download the image you supply and pass it along. You have to set the attribution name and icon for the message so the knows where the content came from even if it's forwarded later.
`javascript`
bot.send(Bot.Message.picture('http://i.imgur.com/oalyVlU.jpg')
.setAttributionName('Imgur')
.setAttributionIcon('http://s.imgur.com/images/favicon-96x96.png'),
'a.username');
Whenever a user subscribes to your bot, your bot will receive a start-chatting message. This message gives you the chance to say hello to the user and let them know what your bot is about.
You might want to greet your new user by name. You can use the bot.getUserProfile(...) method to request information about users who have subscribed to your bot.
`javascriptHey ${user.firstName}!
bot.onStartChattingMessage((message) => {
bot.getUserProfile(message.from)
.then((user) => {
message.reply();`
});
});
Separating different states into multiple message handlers can keep your bot logic under control. If you call next from within your handler, you allow the next handler in the chain to run, otherwise, handling of the incoming message will end with the current handler.
`javascript
bot.onTextMessage((message, next) => {
const userState = getUserState(message.from);
if (!userState.inIntroState) {
// Send the user the intro state
// ...
return;
}
// Allow the next handler take over
next();
});
bot.onTextMessage((message) => {
searchFor(message.body)
.then((result) => {
message.reply(result);
});
});
`
You can specify a static keyboard for your bot when a user starts mentioning it in a conversation:
`javascript`
let bot = new Bot({
username: 'echo.bot',
apiKey: '7b939d69-e840-4d22-aab8-4188c2198f8a',
baseUrl: 'https://kik-echobot.ngrok.io/',
staticKeyboard: new Bot.ResponseKeyboard(['Option 1', 'Option 2'])
});
[travis-image]: https://travis-ci.org/kikinteractive/kik-node.svg?branch=master
[travis-url]: https://travis-ci.org/kikinteractive/kik-node
[dev-kik-url]: https://dev.kik.com/
[dev-config-kik-url]: https://dev.kik.com/#/docs/messaging#configuration
[kik-url]: https://kik.com/
[coveralls-image]: https://coveralls.io/repos/github/kikinteractive/kik-node/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/kikinteractive/kik-node?branch=master
[npm-image]: https://img.shields.io/npm/v/@kikinteractive/kik.svg?style=flat-square
[npm-url]: https://www.npmjs.org/package/@kikinteractive/kik
[stackoverflow-url]: http://stackoverflow.com/questions/tagged/kik
. The middleware will automatically decode the request, and call the appropriate on functions based on the content type. Additional middleware can be used by calling the .use(handler) method.Kind: global class
See: https://bots.kik.com
* Bot
* new Bot()
* .use(handler)
* .updateBotConfiguration()
* [.onTextMessage([text], handler)](#Bot+onTextMessage)
* .onLinkMessage(handler)
* .onPictureMessage(handler)
* .onVideoMessage(handler)
* .onStartChattingMessage(handler)
* .onScanDataMessage(handler)
* .onFriendPickerMessage(handler)
* .onStickerMessage(handler)
* .onIsTypingMessage(handler)
* .onDeliveryReceiptMessage(handler)
* .onReadReceiptMessage(handler)
* .getKikCodeUrl() ⇒ promise.<string>
* .getUserProfile() ⇒ promise.<UserProfile>
* .broadcast(messages, recipients)
* [.send(messages, recipient, [chatId])](#Bot+send)
* .incoming()
* .outgoing(handler)
$3
| Param | Type | Description |
| --- | --- | --- |
| options.username | string | |
| options.apiKey | string | |
| [options.baseUrl] | string | |
| [options.incomingPath] | string | Set true to enable polling or set options |
| [options.manuallySendReadReceipts] | boolean | |
| [options.receiveReadReceipts] | boolean | |
| [options.receiveDeliveryReceipts] | boolean | |
| [options.receiveIsTyping] | boolean | |
| [options.skipSignatureCheck] | boolean | Verify the authenticity of inbound requests. For testing only, do not disable for a bot in production. |
| [options.staticKeyboard] | ResponseKeyboard | Static keyboard for your bot |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot $3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| [text] | string | regexp |
| handler | MessageHandlerCallback |
Example
`js
bot.onTextMessage((incoming, next) => {
// reply handles the message and stops other handlers
// from being called for this message
incoming.reply(Hi I'm ${bot.username});
});`
Example
`js
bot.onTextMessage((incoming, next) => {
if (incoming.body !== 'Hi') {
// we only handle welcoming, let someone else deal with this
// message
return next();
} // say hello...
});
`
Example
`js
bot.onTextMessage(/^hi|hello|bonjour$/i, (incoming, next) => {
// say hello...
});
`
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
$3
Creates a Kik Code with the intended options and returns the
URL of the Kik Code image. If the options specify a data Kik Code
this will hit the Kik Code service and store that data for you.Kind: instance method of Bot
| Param | Type | Description |
| --- | --- | --- |
| [options.data] | string | object | The data to be sent back to this bot after the user scans |
| [options.width] | number | Width of the Kik code in the PNG image |
| [options.height] | number | Height of the Kik code in the PNG image |
| [options.size] | number | Helper for the width and height in the PNG image |
| [options.color] | number | The color which the user will see after scanning. See {KikCode.Colors} |
$3
Kind: instance method of Bot $3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| messages | array |
| recipients | array |
$3
Kind: instance method of Bot | Param | Type |
| --- | --- |
| messages | array |
| recipient | string |
| [chatId] | string |
$3
Handles the incoming requests for messages
configuration.Kind: instance method of Bot
$3
Adds a post processing handler for all outgoing messages.
Messages passed to this handler will be JSON objects.Kind: instance method of Bot
| Param | Type |
| --- | --- |
| handler | MessageHandlerCallback |
Example
`js
bot.outgoing((outgoing, next) => {
console.log('Outgoing message:', outgoing);
next();
});
`IncomingMessage
Object that allows you to send a response to user messages or ignore them.Kind: global class
* IncomingMessage
* .reply(messages) ⇒ promise.<object>
* .markRead() ⇒ promise.<object>
* .startTyping() ⇒ promise.<object>
* .stopTyping() ⇒ promise.<object>
* .ignore()
$3
Kind: instance method of IncomingMessage | Param | Type |
| --- | --- |
| messages | Message | array.<Message> |
$3
Kind: instance method of IncomingMessage
$3
Kind: instance method of IncomingMessage
$3
Kind: instance method of IncomingMessage
$3
Kind: instance method of IncomingMessage UserProfile
See https://dev.kik.com/#/docs/messaging#user-profilesKind: global class
* UserProfile
* .displayName ⇒ string
* .username ⇒ string
* .firstName ⇒ string
* .lastName ⇒ string
* .profilePicUrl ⇒ string
* .profilePicLastModified ⇒ number
* .timezone ⇒ string
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile
$3
Kind: instance property of UserProfile Message
Object that stores a specific message that can be sent to/received from a user. The static methods of Message are factories that generate a specific kind of message.Kind: global class
* Message
* _static_
* .text() ⇒ Message
* .link() ⇒ Message
* .picture() ⇒ Message
* .video() ⇒ Message
* .isTyping() ⇒ Message
* .readReceipt() ⇒ Message
* .fromJSON(json) ⇒ Message
* _instance_
* .from ⇒ string
* .id ⇒ string
* .chatId ⇒ string
* .messageIds ⇒ array
* .readReceiptRequested ⇒ boolean
* .stickerPackId ⇒ string
* .scanData ⇒ string
* .stickerUrl ⇒ string
* .timestamp ⇒ number
* .type ⇒ string
* .kikJsData ⇒ object
* .picUrl ⇒ string
* .noForward ⇒ boolean
* .isTyping ⇒ boolean
* .body ⇒ string
* .text ⇒ string
* .title ⇒ string
* .url ⇒ string
* .videoUrl ⇒ string
* .delay ⇒ number
* .typeTime ⇒ number
* .attributionName ⇒ string
* .attributionIcon ⇒ string
* .loop ⇒ boolean
* .muted ⇒ boolean
* .autoplay ⇒ boolean
* .noSave ⇒ boolean
* .participants ⇒ array
* .chatType ⇒ string
* .mention ⇒ string
* .picked ⇒ array
* .metadata ⇒ object
* .isInPublicChat() ⇒ boolean
* .isInPrivateChat() ⇒ boolean
* .isInDirectChat() ⇒ boolean
* .isTextMessage() ⇒ boolean
* .isLinkMessage() ⇒ boolean
* .isPictureMessage() ⇒ boolean
* .isVideoMessage() ⇒ boolean
* .isStartChattingMessage() ⇒ boolean
* .isScanDataMessage() ⇒ boolean
* .isFriendPickerMessage() ⇒ boolean
* .isStickerMessage() ⇒ boolean
* .isIsTypingMessage() ⇒ boolean
* .isDeliveryReceiptMessage() ⇒ boolean
* .isReadReceiptMessage() ⇒ boolean
* .isMention() ⇒ boolean
* .toJSON() ⇒ object
* .addTextResponse(text) ⇒ Message
* [.addResponseKeyboard(suggestions, [isHidden], [user])](#Message+addResponseKeyboard) ⇒ Message
* .setKikJsData(kikJsData) ⇒ Message
* .setPicUrl(picUrl) ⇒ Message
* .setNoForward(noForward) ⇒ Message
* .setIsTyping(isTyping) ⇒ Message
* .setMessageIds(messageIds) ⇒ Message
* .setBody(body) ⇒ Message
* .setText(text) ⇒ Message
* .setTitle(title) ⇒ Message
* .setUrl(url) ⇒ Message
* .setVideoUrl(videoUrl) ⇒ Message
* .setDelay(delay) ⇒ Message
* .setTypeTime(typeTime) ⇒ Message
* .setAttributionName(attributionName) ⇒ Message
* .setAttributionIcon(attributionIcon) ⇒ Message
* .setLoop(loop) ⇒ Message
* .setMuted(muted) ⇒ Message
* .setAutoplay(autoplay) ⇒ Message
* .setNoSave(noSave) ⇒ Message
* .setMention(noSave) ⇒ Message
$3
See https://dev.kik.com/#/docs/messaging#receiving-messagesKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#receiving-messagesKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#receiving-messagesKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#receiptsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#receiptsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#stickerKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#kik-codes-apiKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#stickerKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#common-fieldsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#message-formatsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#is-typingKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#textKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#common-fieldsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#textKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#attributionKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#attributionKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#participantsKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#all-received-chat-messages-excluding-receipts (will be undefined for receipt messages)Kind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#mentionKind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#friend-pickerKind: instance property of Message
$3
Kind: instance property of Message
$3
See https://dev.kik.com/#/docs/messaging#textKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#all-received-chat-messages-excluding-receiptsKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#all-received-chat-messages-excluding-receiptsKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#all-received-chat-messages-excluding-receiptsKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#pictureKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#start-chattingKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#scan-dataKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#friend-pickerKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#stickerKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#is-typingKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#receiptsKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#receiptsKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#mentionsKind: instance method of Message
$3
Constructs a JSON payload ready to be sent to the
bot messaging APIKind: instance method of Message
$3
See https://dev.kik.com/#/docs/messaging#keyboardsKind: instance method of Message
| Param | Type |
| --- | --- |
| text | string |
$3
See https://dev.kik.com/#/docs/messaging#keyboardsKind: instance method of Message
| Param | Type |
| --- | --- |
| suggestions | array |
| [isHidden] | boolean |
| [user] | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| kikJsData | object |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| picUrl | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| noForward | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| isTyping | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| messageIds | array |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| body | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| text | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| title | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| url | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| videoUrl | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| delay | number |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| typeTime | number |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| attributionName | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| attributionIcon | string |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| loop | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| muted | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| autoplay | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| noSave | boolean |
$3
Kind: instance method of Message | Param | Type |
| --- | --- |
| mention | string |
$3
See https://dev.kik.com/#/docs/messaging#textKind: static method of Message
$3
See https://dev.kik.com/#/docs/messaging#linkKind: static method of Message
$3
See https://dev.kik.com/#/docs/messaging#pictureKind: static method of Message
$3
See https://dev.kik.com/#/docs/messaging#videoKind: static method of Message
$3
See https://dev.kik.com/#/docs/messaging#is-typingKind: static method of Message
$3
See https://dev.kik.com/#/docs/messaging#receiptsKind: static method of Message
$3
Constructs a new {Message} object from a JSON-encoded payload
See https://dev.kik.com/#/docsKind: static method of Message
| Param | Type |
| --- | --- |
| json | object |
ResponseKeyboard
* Bot.ResponseKeyboard
* new Bot.ResponseKeyboard(responses, hidden, to)
* .addResponse(resonse)
$3
| Param | Type |
| --- | --- |
| [responses] | array |
| [hidden] | boolean |
| [to] | string |
Example
`js
let keyboard = new Bot.ResponseKeyboard(['Option 1', 'Option 2']);
`Example
`js
let keyboard = new Bot.ResponseKeyboard(['Option 1', 'Option 2'], true, 'kikteam');
`$3
Kind: instance method of ResponseKeyboard | Param | Type |
| --- | --- |
| response | string | object |
Example
`js
let keyboard = new Bot.ResponseKeyboard();
keyboard.addResponse(Bot.Response.friendPicker('Pick a friend'));
keyboard.addResponse('Option 1');
keyboard.addResponse('Option 2');
``Kind: global class
Kind: static enum property of KikCode
Properties
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| KikBlue | number | 0 | #42B4E6 |
| Turquoise | number | 1 | #42DFD8 |
| Mint | number | 2 | #24D7A7 |
| Forest | number | 3 | #25912B |
| KikGreen | number | 4 | #87D300 |
| Sunshine | number | 5 | #F8CB1C |
| OrangeCreamsicle | number | 6 | #FC971B |
| BloodOrange | number | 7 | #F9703A |
| CandyAppleRed | number | 8 | #F7373C |
| Salmon | number | 9 | #F88585 |
| Coral | number | 10 | #F767C3 |
| Cranberry | number | 11 | #940D65 |
| Lavender | number | 12 | #CB94FF |
| RoyalPurple | number | 13 | #8737F8 |
| Marine | number | 14 | #353CD4 |
| Steel | number | 15 | #5D7687 |
Kind: global class
Kind: static method of Response
| Param | Type |
| --- | --- |
| body | string |
Kind: static method of Response
| Param | Type | Description |
| --- | --- | --- |
| [body] | string | |
| [min] | int | |
| [max] | int| |
| [preselected] | array | array of strings |
Kind: static method of Response
| Param | Type |
| --- | --- |
| picUrl | string |
| metadata | object |