kikinteractive/kik-bot-python-example

Name: kik-bot-python-example

Owner: Kik Interactive

Description: null

Created: 2016-11-24 22:42:03.0

Updated: 2017-12-14 05:32:29.0

Pushed: 2017-11-27 13:21:55.0

Homepage: null

Size: 1905

Language: Python

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Kik Python Bot Example

Coverage Status Hex.pm

An example bot implemented in Python.

It's designed to greet the user, send a suggested response and replies to them with their profile picture.

Remember to replace the BOT_USERNAME_HERE, BOT_API_KEY_HERE and WEBHOOK_HERE fields with your own.

See https://github.com/kikinteractive/kik-python for Kik's Python SDK documentation.

Table Of Contents

Setting Up

Getting ready Create Your Bot

Go over to dev.kik.com.

Scan The Kik Code

…and in Kik, pull down on your conversations list to scan the code.

(the above scan code is only an example, you must go to the link above for a valid scan code)

Scan Tutorial

Pulling down will allow Kik to scan the code and introduce you to Botsworth, the bot maker bot. Follow the prompts to configure your bot.

Botsworth will ask you to give your bot a name which, is the first step. Choose wisely! Your bot should be cleverly and descriptively named - it should provide relatively clear indication as to the function of your bot. Hint: Avoid numbers and special characters in your bot's name.

Once you've chosen your bot's name, Botsworth will ask you to confirm, and will then log you into the Kik Bot Dashboard. You will be prompted to agree to the Kik API Terms of Use.

Next step: Setting up your development environment!

Setting up Python

Make sure you're running at least Python 2.7 or Python 3. We'll be using Python 2.7 today on Linux / MacOS X using virtualenv

Get the sample bot

Clone this repository:

t clone https://github.com/kikinteractive/kik-bot-python-example.git
ing into 'kik-bot-python-example'...
te: Counting objects: 88, done.
te: Total 88 (delta 0), reused 0 (delta 0), pack-reused 88
iving objects: 100% (88/88), 18.40 KiB | 0 bytes/s, done.
lving deltas: 100% (30/30), done.

The example bot is developed as one file (very simply) using Flask. cd to the directory that was created when the repository was cloned.

 kik-bot-python-example
Setup your Virtualenv environment.

Virtualenv is a popular tool to create isolated Python environments. It is used by many python developers to create an environment that has its own installation directories, that does not share libraries with other environments.

rtualenv env
python executable in ./kik-bot-python-example-internal/env/bin/python2.7
 creating executable in ./kik-bot-python-example-internal/env/bin/python
alling setuptools, pip, wheel...done.
urce env/bin/activate
Install dependencies

Install the python dependencies:

) $ pip install -r requirements.dev.txt
cting flake8==2.5.4 (from -r requirements.dev.txt (line 1))
ing cached flake8-2.5.4-py2.py3-none-any.whl
ecting mock==2.0.0 (from -r requirements.dev.txt (line 2))
ing cached mock-2.0.0-py2.py3-none-any.whl
ecting kik==1.2.0 (from -r requirements.dev.txt (line 3))
ecting Flask==0.11 (from -r requirements.dev.txt (line 4))
ing cached Flask-0.11-py2.py3-none-any.whl
ecting pyyaml (from -r requirements.dev.txt (line 5))
ecting pep8 (from -r requirements.dev.txt (line 6))
ing cached pep8-1.7.0-py2.py3-none-any.whl
ecting nose (from -r requirements.dev.txt (line 7))
ing cached nose-1.3.7-py2-none-any.whl
ecting nose-cov (from -r requirements.dev.txt (line 8))
ecting mccabe<0.5,>=0.2.1 (from flake8==2.5.4->-r requirements.dev.txt (line 1))
ing cached mccabe-0.4.0-py2.py3-none-any.whl
ecting pyflakes<1.1,>=0.8.1 (from flake8==2.5.4->-r requirements.dev.txt (line 1))
ing cached pyflakes-1.0.0-py2.py3-none-any.whl
ecting funcsigs>=1; python_version < "3.3" (from mock==2.0.0->-r requirements.dev.txt (line 2))
ing cached funcsigs-1.0.2-py2.py3-none-any.whl
ecting pbr>=0.11 (from mock==2.0.0->-r requirements.dev.txt (line 2))
ing cached pbr-1.10.0-py2.py3-none-any.whl
ecting six>=1.9 (from mock==2.0.0->-r requirements.dev.txt (line 2))
ing cached six-1.10.0-py2.py3-none-any.whl
ecting requests>=2.3.0 (from kik==1.2.0->-r requirements.dev.txt (line 3))
ing cached requests-2.12.4-py2.py3-none-any.whl
ecting click>=2.0 (from Flask==0.11->-r requirements.dev.txt (line 4))
ing cached click-6.7-py2.py3-none-any.whl
ecting Werkzeug>=0.7 (from Flask==0.11->-r requirements.dev.txt (line 4))
ing cached Werkzeug-0.11.15-py2.py3-none-any.whl
ecting Jinja2>=2.4 (from Flask==0.11->-r requirements.dev.txt (line 4))
ing cached Jinja2-2.9.4-py2.py3-none-any.whl
ecting itsdangerous>=0.21 (from Flask==0.11->-r requirements.dev.txt (line 4))
ecting cov-core>=1.6 (from nose-cov->-r requirements.dev.txt (line 8))
ecting MarkupSafe>=0.23 (from Jinja2>=2.4->Flask==0.11->-r requirements.dev.txt (line 4))
ecting coverage>=3.6 (from cov-core>=1.6->nose-cov->-r requirements.dev.txt (line 8))
ing cached coverage-4.3.1-cp27-cp27m-macosx_10_10_x86_64.whl
alling collected packages: pep8, mccabe, pyflakes, flake8, funcsigs, pbr, six, mock, requests, kik, click, Werkzeug, MarkupSafe, Jinja2, itsdangerous, Flask, pyyaml, nose, coverage, cov-core, nose-cov
essfully installed Flask-0.11 Jinja2-2.9.4 MarkupSafe-0.23 Werkzeug-0.11.15 click-6.7 cov-core-1.15.0 coverage-4.3.1 flake8-2.5.4 funcsigs-1.0.2 itsdangerous-0.24 kik-1.2.0 mccabe-0.4.0 mock-2.0.0 nose-1.3.7 nose-cov-1.6 pbr-1.10.0 pep8-1.7.0 pyflakes-1.0.0 pyyaml-3.12 requests-2.12.4 six-1.10.0```
Validate the installation

Validate the installation by running the unit tests. It should produce no errors.

) $ nosetests
...........
------------------------------------------------------------------
15 tests in 0.176s


How Bots Work

Kik bots talk to the Kik infrastructure via HTTP requests: When sending a message, you send a request to us, and for messages to be received by your bot, Kik will make requests to your endpoint. In other words, Kik must be able to call your URL on your web server.

Chat To Bot Flow Diagram

Configuring Your Server

For our bot to work, we need to have an address that's accessible from the internet. Many production Kik bots run in cloud based services such as Heroku, Google App Engine or Amazon Web Services - or in their own data center infrastructure.

However, for development purposes you can use ngrok to provide access to your bot running in your local network. Ngrok is easy to setup and use, and has excellent documentation.

Ngrok is a handy tool and service that allows you tunnel requests from the wide open Internet to your local machine when it's behind a NAT or firewall. It's commonly used to develop web services and webhooks.

If you're using ngrok, launch it now in a new terminal window:

rok http 8080

When it launches, you will see a screen similar to the following:

k by @inconshreveable                                                                                                                                                                 (Ctrl+C to quit)

ion Status                online                                                                                                                                                                      
unt                       A Bot Developer (Plan: Free)                                                                                                                                                 
ion                       2.1.18                                                                                                                                                                      
on                        United States (us)                                                                                                                                                          
Interface                 http://127.0.0.1:4040                                                                                                                                                       
arding                    http://ABCDEFG123.ngrok.io -> localhost:8080                                                                                                                                  
arding                    https://ABCDEFG123.ngrok.io -> localhost:8080                                                                                                                                 

ections                   ttl     opn     rt1     rt5     p50     p90                                                                                                                                 
                          0       0       0.00    0.00    0.00    0.00  

Note the “Forwarding” address (https://ABCDEFG123.ngrok.io), as this will become part of your 'webhook' address.

Configuring Your Bot

To get the bot running, you're going to need your bot's username, and the API key. This is all available from dev.kik.com. It will be similar to the following screenshot:

Bot Configuration Panel

Here you can set the display name and “profile picture” for your bot. You'll need to copy/paste your API key into the bot's source code.

Change:

= KikApi('BOT_USERNAME_HERE', 'BOT_API_KEY_HERE')

so that your bot's username and your API key are passed to Kik API's constructor. For example, if we named our bot ademobot, and according the bot configuration panel our API key is 5a888dcb-4c6e-1973-b15t-308e1854f0ba, then we would change the above line to:

= KikApi('ademobot', '5a888dcb-4c6e-1973-b15t-308e1854f0ba')

Next, we'll need to set the webhook to the URL of your bot's “incoming messages” route on your web server. This is where Kik will send all the messages that users send to your bot. In the example code, the route for incoming messages is /incoming

Locate the following line in bot.py:

set_configuration(Configuration(webhook='WEBHOOK_HERE'))

Kik will send messages to this path upon receipt. So, if your web address is https://www.example.com then you'll set your webhook to https://www.example.com/incoming, as shown below.

set_configuration(Configuration(webhook='https://www.example.com/incoming'))

If you're using ngrok as shown above, you would set the webhook as follows:

set_configuration(Configuration(webhook='https://ABCDEFG123.ngrok.io/incoming'))
Launch Your Bot

Start the bot by running the file as shown below:

thon ./bot.py
unning on http://127.0.0.1:8080/ (Press CTRL+C to quit)
estarting with stat
ebugger is active!
ebugger pin code: 300-736-125
Talking to the bot

With your bot up and running, you'll be able to chat with it in Kik. It should already appear in your message list. Select it to start chatting:

Start Chatting

New users can click on the magnifying glass in their messages list to search for your bot. Once found, they can click on “Start Chatting” to subscribe to your bot and start interacting! Talk to your Bot

Contributing

If you're looking to contribute to this repository, check out the Contributing Guide.

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.

License

This bot is released under the terms of the Apache 2.0 license.

2016 Kik Interactive Inc.

nsed under the Apache License, Version 2.0 (the "License");
may not use this file except in compliance with the License.
may obtain a copy of the License at

://www.apache.org/licenses/LICENSE-2.0

ss required by applicable law or agreed to in writing,
ware distributed under the License is distributed on an
IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
er express or implied. See the License for the specific
uage governing permissions and limitations under the License.

See LICENSE.md for more information.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.