Skip to content

Commit

Permalink
Added Kik Client support
Browse files Browse the repository at this point in the history
  • Loading branch information
keiffster committed Feb 15, 2018
1 parent 92c2065 commit c60b276
Show file tree
Hide file tree
Showing 19 changed files with 456 additions and 2 deletions.
11 changes: 11 additions & 0 deletions bots/servusai/kik/aiml/sraix.aiml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<aiml>

<category>
<pattern>*</pattern>
<template>
<sraix service="PROGRAMY"><star /></sraix>
</template>
</category>

</aiml>
60 changes: 60 additions & 0 deletions bots/servusai/kik/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
bot:
license_keys: $BOT_ROOT/config/license.keys

prompt: ">>>"

initial_question: Hi, how can I help you today?
default_response: Sorry, I don't have an answer for that!
default_response_srai: YEMPTY
empty_string: YEMPTY
exit_response: So long, and thanks for the fish!
exit_response_srai: YEXITRESPONSE

override_properties: true

brain:

# Overrides
overrides:
allow_system_aiml: false
allow_learn_aiml: false
allow_learnf_aiml: false

# Defaults
defaults:
default-get: unknown
default-property: unknown
default-map: unknown

# Nodes
nodes:
pattern_nodes: ../../ybot/config/pattern_nodes.conf
template_nodes: ../../ybot/config/template_nodes.conf

files:
aiml:
files: $BOT_ROOT/aiml
extension: .aiml
directories: true
conversation:
file: /tmp/multibot-conversation.csv
format: csv
delete_on_start: true
properties: $BOT_ROOT/config/properties.txt
preprocessors: $BOT_ROOT/config/preprocessors.conf
postprocessors: $BOT_ROOT/config/postprocessors.conf

services:
PROGRAMY:
classname: programy.services.programy.ProgramyRESTService
method: GET
host: 0.0.0.0
port: 8989
url: /api/v1.0/ask

kik:
bot_name: servusai
webhook: https://4264d3b1.ngrok.io/incoming
host: 127.0.0.1
port: 5000
debug: false
7 changes: 7 additions & 0 deletions bots/servusai/kik/config/postprocessors.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
programy.processors.post.mergechinese.MergeChinesePostProcessor
programy.processors.post.denormalize.DenormalizePostProcessor
programy.processors.post.formatpunctuation.FormatPunctuationProcessor
programy.processors.post.formatnumbers.FormatNumbersPostProcessor
programy.processors.post.multispaces.RemoveMultiSpacePostProcessor
programy.processors.post.removehtml.RemoveHTMLPostProcessor
programy.processors.post.consoleformat.ConsoleFormatPostProcessor
3 changes: 3 additions & 0 deletions bots/servusai/kik/config/preprocessors.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
programy.processors.pre.normalize.NormalizePreProcessor
programy.processors.pre.removepunctuation.RemovePunctuationPreProcessor
programy.processors.pre.splitchinese.SplitChinesePreProcessor
7 changes: 7 additions & 0 deletions bots/servusai/kik/config/properties.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fullname:Y-Bot

birthdate:March 14, 2017

grammar_version:1.0.0
app_version: 1.0.0

3 changes: 3 additions & 0 deletions bots/servusai/kik/exec_ngrok.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash

ngrok http 5000
8 changes: 8 additions & 0 deletions bots/servusai/kik/kik.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh

clear

export PYTHONPATH=../../../src:.

python3 ../../../src/programy/clients/kik_client.py --config ./config.yaml --cformat yaml --logging ../../y-bot/logging.yaml

11 changes: 11 additions & 0 deletions bots/servusai/kik/servusai-kik.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Y-Bot Line Client

[Service]
User=ubuntu
ExecStart=/opt/program-y/bots/servusai/kik/servusai-kik.sh
Restart=always

[Install]
WantedBy=multi-user.target

10 changes: 10 additions & 0 deletions bots/servusai/kik/servusai-kik.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /bin/sh

# This file is for use on Servusai.com AWS Server

export PYTHONPATH=/opt/program-y/src:.

cd /opt/program-y

python3 /opt/program-y/src/programy/clients/kik_client.py --config /opt/program-y/bots/servusai/kik/config.yaml --cformat yaml --logging /opt/program-y/bots/y-bot/logging.yaml

2 changes: 1 addition & 1 deletion bots/servusai/line/servusai-line.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=Y-Bot Line Client

[Service]
User=ubuntu
ExecStart=/opt/program-y/bots/servusai/line/servusai-twilio.sh
ExecStart=/opt/program-y/bots/servusai/line/servusai-kik.sh
Restart=always

[Install]
Expand Down
2 changes: 1 addition & 1 deletion bots/servusai/viber/servusai-viber.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Description=Y-Bot Viber Client

[Service]
User=ubuntu
ExecStart=/opt/program-y/bots/servusai/viber/servusai-twilio.sh
ExecStart=/opt/program-y/bots/servusai/viber/servusai-kik.sh
Restart=always

[Install]
Expand Down
7 changes: 7 additions & 0 deletions bots/y-bot/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,10 @@ line:
host: 127.0.0.1
port: 5000
debug: false

kik:
bot_name: servusai
webhook: https://4264d3b1.ngrok.io/incoming
host: 127.0.0.1
port: 5000
debug: false
9 changes: 9 additions & 0 deletions bots/y-bot/y-bot-kik.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off

CLS

mkdir .\temp

SET PYTHONPATH=..\..\src;.

python ..\..\src\programy\clients\kik_client.py --config .\config.windows.yaml --cformat yaml --logging .\logging.windows.yaml
8 changes: 8 additions & 0 deletions bots/y-bot/y-bot-kik.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#! /bin/sh

clear

export PYTHONPATH=../../src:.

python3 ../../src/programy/clients/kik_client.py --config ./config.yaml --cformat yaml --logging ./logging.yaml

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ slackclient==1.1.2
redis==2.10.6
viberbot==1.0.11
line-bot-sdk==1.5.0
kik==1.5.0

# Uncomment this line on Osx or Linux if you want to use sanic rest client
# Sanic is not supported on Windows !
Expand Down
135 changes: 135 additions & 0 deletions src/programy/clients/kik_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
"""
Copyright (c) 2016-17 Keith Sterling http://www.keithsterling.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

# https://kik.readthedocs.io/en/latest/user.html#installation

import logging

from flask import Flask, request, abort, Response

from kik import KikApi, Configuration
from kik.messages import messages_from_json, TextMessage

from programy.clients.client import BotClient
from programy.config.sections.client.kik_client import KikConfiguration


KIK_CLIENT = None


class KikBotClient(BotClient):

def __init__(self, argument_parser=None):
BotClient.__init__(self, "kik", argument_parser)

self.get_tokens()

self.create_kik_bot()

if logging.getLogger().isEnabledFor(logging.DEBUG):
logging.debug("Kik Client is running....")

def set_environment(self):
self.bot.brain.properties.add_property("env", 'kik')

def get_client_configuration(self):
return KikConfiguration()

def get_tokens(self):
self._bot_api_key = self.bot.license_keys.get_key("KIK_BOT_API_KEY")

def ask_question(self, clientid, question):
response = ""
try:
response = self.bot.ask_question(clientid, question)
except Exception as e:
print(e)
return response

def create_kik_bot(self):
self._kik_bot = KikApi(self.configuration.client_configuration.bot_name, self._bot_api_key)
self._kik_bot.set_configuration(Configuration(webhook=self.configuration.client_configuration.webhook))

def handle_text_message(self, message):
question = message.body
clientid = message.from_user

answer = self.ask_question(clientid, question)

self._kik_bot.send_messages([
TextMessage(
to=message.from_user,
chat_id=message.chat_id,
body=answer
)
])

def handle_unknown_message(self, message):
pass

def handle_message_request(self, request):

messages = messages_from_json(request.json['messages'])

for message in messages:
if isinstance(message, TextMessage):
self.handle_text_message(message)
else:
self.handle_unknown_message(message)

def handle_incoming(self, request):
if not self._kik_bot.verify_signature(request.headers.get('X-Kik-Signature'), request.get_data()):
return Response(status=403)

self.handle_message_request(request)
return Response(status=200)


APP = Flask(__name__)


@APP.route('/incoming', methods=['POST'])
def incoming():
return KIK_CLIENT.handle_incoming(request)


if __name__ == '__main__':

KIK_CLIENT = KikBotClient()

print("Kik Client running on %s:%s" % (KIK_CLIENT.configuration.client_configuration.host,
KIK_CLIENT.configuration.client_configuration.port))


if KIK_CLIENT.configuration.client_configuration.debug is True:
print("Kik Client running in debug mode")

if KIK_CLIENT.configuration.client_configuration.ssl_cert_file is not None and \
KIK_CLIENT.configuration.client_configuration.ssl_key_file is not None:
context = (KIK_CLIENT.configuration.client_configuration.ssl_cert_file,
KIK_CLIENT.configuration.client_configuration.ssl_key_file)

print("Kik Client running in https mode")
APP.run(host=KIK_CLIENT.configuration.client_configuration.host,
port=KIK_CLIENT.configuration.client_configuration.port,
debug=KIK_CLIENT.configuration.client_configuration.debug,
ssl_context=context)
else:
print("Kik Client running in http mode, careful now !")
APP.run(host=KIK_CLIENT.configuration.client_configuration.host,
port=KIK_CLIENT.configuration.client_configuration.port,
debug=KIK_CLIENT.configuration.client_configuration.debug)
Loading

0 comments on commit c60b276

Please sign in to comment.