Skip to content

Commit c60b276

Browse files
committed
Added Kik Client support
1 parent 92c2065 commit c60b276

File tree

19 files changed

+456
-2
lines changed

19 files changed

+456
-2
lines changed

bots/servusai/kik/aiml/sraix.aiml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<aiml>
3+
4+
<category>
5+
<pattern>*</pattern>
6+
<template>
7+
<sraix service="PROGRAMY"><star /></sraix>
8+
</template>
9+
</category>
10+
11+
</aiml>

bots/servusai/kik/config.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
bot:
2+
license_keys: $BOT_ROOT/config/license.keys
3+
4+
prompt: ">>>"
5+
6+
initial_question: Hi, how can I help you today?
7+
default_response: Sorry, I don't have an answer for that!
8+
default_response_srai: YEMPTY
9+
empty_string: YEMPTY
10+
exit_response: So long, and thanks for the fish!
11+
exit_response_srai: YEXITRESPONSE
12+
13+
override_properties: true
14+
15+
brain:
16+
17+
# Overrides
18+
overrides:
19+
allow_system_aiml: false
20+
allow_learn_aiml: false
21+
allow_learnf_aiml: false
22+
23+
# Defaults
24+
defaults:
25+
default-get: unknown
26+
default-property: unknown
27+
default-map: unknown
28+
29+
# Nodes
30+
nodes:
31+
pattern_nodes: ../../ybot/config/pattern_nodes.conf
32+
template_nodes: ../../ybot/config/template_nodes.conf
33+
34+
files:
35+
aiml:
36+
files: $BOT_ROOT/aiml
37+
extension: .aiml
38+
directories: true
39+
conversation:
40+
file: /tmp/multibot-conversation.csv
41+
format: csv
42+
delete_on_start: true
43+
properties: $BOT_ROOT/config/properties.txt
44+
preprocessors: $BOT_ROOT/config/preprocessors.conf
45+
postprocessors: $BOT_ROOT/config/postprocessors.conf
46+
47+
services:
48+
PROGRAMY:
49+
classname: programy.services.programy.ProgramyRESTService
50+
method: GET
51+
host: 0.0.0.0
52+
port: 8989
53+
url: /api/v1.0/ask
54+
55+
kik:
56+
bot_name: servusai
57+
webhook: https://4264d3b1.ngrok.io/incoming
58+
host: 127.0.0.1
59+
port: 5000
60+
debug: false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
programy.processors.post.mergechinese.MergeChinesePostProcessor
2+
programy.processors.post.denormalize.DenormalizePostProcessor
3+
programy.processors.post.formatpunctuation.FormatPunctuationProcessor
4+
programy.processors.post.formatnumbers.FormatNumbersPostProcessor
5+
programy.processors.post.multispaces.RemoveMultiSpacePostProcessor
6+
programy.processors.post.removehtml.RemoveHTMLPostProcessor
7+
programy.processors.post.consoleformat.ConsoleFormatPostProcessor
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
programy.processors.pre.normalize.NormalizePreProcessor
2+
programy.processors.pre.removepunctuation.RemovePunctuationPreProcessor
3+
programy.processors.pre.splitchinese.SplitChinesePreProcessor
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fullname:Y-Bot
2+
3+
birthdate:March 14, 2017
4+
5+
grammar_version:1.0.0
6+
app_version: 1.0.0
7+

bots/servusai/kik/exec_ngrok.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
ngrok http 5000

bots/servusai/kik/kik.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
3+
clear
4+
5+
export PYTHONPATH=../../../src:.
6+
7+
python3 ../../../src/programy/clients/kik_client.py --config ./config.yaml --cformat yaml --logging ../../y-bot/logging.yaml
8+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Y-Bot Line Client
3+
4+
[Service]
5+
User=ubuntu
6+
ExecStart=/opt/program-y/bots/servusai/kik/servusai-kik.sh
7+
Restart=always
8+
9+
[Install]
10+
WantedBy=multi-user.target
11+

bots/servusai/kik/servusai-kik.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#! /bin/sh
2+
3+
# This file is for use on Servusai.com AWS Server
4+
5+
export PYTHONPATH=/opt/program-y/src:.
6+
7+
cd /opt/program-y
8+
9+
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
10+

bots/servusai/line/servusai-line.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=Y-Bot Line Client
33

44
[Service]
55
User=ubuntu
6-
ExecStart=/opt/program-y/bots/servusai/line/servusai-twilio.sh
6+
ExecStart=/opt/program-y/bots/servusai/line/servusai-kik.sh
77
Restart=always
88

99
[Install]

bots/servusai/viber/servusai-viber.service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Description=Y-Bot Viber Client
33

44
[Service]
55
User=ubuntu
6-
ExecStart=/opt/program-y/bots/servusai/viber/servusai-twilio.sh
6+
ExecStart=/opt/program-y/bots/servusai/viber/servusai-kik.sh
77
Restart=always
88

99
[Install]

bots/y-bot/config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,10 @@ line:
253253
host: 127.0.0.1
254254
port: 5000
255255
debug: false
256+
257+
kik:
258+
bot_name: servusai
259+
webhook: https://4264d3b1.ngrok.io/incoming
260+
host: 127.0.0.1
261+
port: 5000
262+
debug: false

bots/y-bot/y-bot-kik.cmd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@echo off
2+
3+
CLS
4+
5+
mkdir .\temp
6+
7+
SET PYTHONPATH=..\..\src;.
8+
9+
python ..\..\src\programy\clients\kik_client.py --config .\config.windows.yaml --cformat yaml --logging .\logging.windows.yaml

bots/y-bot/y-bot-kik.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#! /bin/sh
2+
3+
clear
4+
5+
export PYTHONPATH=../../src:.
6+
7+
python3 ../../src/programy/clients/kik_client.py --config ./config.yaml --cformat yaml --logging ./logging.yaml
8+

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ slackclient==1.1.2
1818
redis==2.10.6
1919
viberbot==1.0.11
2020
line-bot-sdk==1.5.0
21+
kik==1.5.0
2122

2223
# Uncomment this line on Osx or Linux if you want to use sanic rest client
2324
# Sanic is not supported on Windows !

src/programy/clients/kik_client.py

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
"""
2+
Copyright (c) 2016-17 Keith Sterling http://www.keithsterling.com
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
6+
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
7+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
9+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
Software.
11+
12+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
13+
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
15+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
"""
17+
18+
# https://kik.readthedocs.io/en/latest/user.html#installation
19+
20+
import logging
21+
22+
from flask import Flask, request, abort, Response
23+
24+
from kik import KikApi, Configuration
25+
from kik.messages import messages_from_json, TextMessage
26+
27+
from programy.clients.client import BotClient
28+
from programy.config.sections.client.kik_client import KikConfiguration
29+
30+
31+
KIK_CLIENT = None
32+
33+
34+
class KikBotClient(BotClient):
35+
36+
def __init__(self, argument_parser=None):
37+
BotClient.__init__(self, "kik", argument_parser)
38+
39+
self.get_tokens()
40+
41+
self.create_kik_bot()
42+
43+
if logging.getLogger().isEnabledFor(logging.DEBUG):
44+
logging.debug("Kik Client is running....")
45+
46+
def set_environment(self):
47+
self.bot.brain.properties.add_property("env", 'kik')
48+
49+
def get_client_configuration(self):
50+
return KikConfiguration()
51+
52+
def get_tokens(self):
53+
self._bot_api_key = self.bot.license_keys.get_key("KIK_BOT_API_KEY")
54+
55+
def ask_question(self, clientid, question):
56+
response = ""
57+
try:
58+
response = self.bot.ask_question(clientid, question)
59+
except Exception as e:
60+
print(e)
61+
return response
62+
63+
def create_kik_bot(self):
64+
self._kik_bot = KikApi(self.configuration.client_configuration.bot_name, self._bot_api_key)
65+
self._kik_bot.set_configuration(Configuration(webhook=self.configuration.client_configuration.webhook))
66+
67+
def handle_text_message(self, message):
68+
question = message.body
69+
clientid = message.from_user
70+
71+
answer = self.ask_question(clientid, question)
72+
73+
self._kik_bot.send_messages([
74+
TextMessage(
75+
to=message.from_user,
76+
chat_id=message.chat_id,
77+
body=answer
78+
)
79+
])
80+
81+
def handle_unknown_message(self, message):
82+
pass
83+
84+
def handle_message_request(self, request):
85+
86+
messages = messages_from_json(request.json['messages'])
87+
88+
for message in messages:
89+
if isinstance(message, TextMessage):
90+
self.handle_text_message(message)
91+
else:
92+
self.handle_unknown_message(message)
93+
94+
def handle_incoming(self, request):
95+
if not self._kik_bot.verify_signature(request.headers.get('X-Kik-Signature'), request.get_data()):
96+
return Response(status=403)
97+
98+
self.handle_message_request(request)
99+
return Response(status=200)
100+
101+
102+
APP = Flask(__name__)
103+
104+
105+
@APP.route('/incoming', methods=['POST'])
106+
def incoming():
107+
return KIK_CLIENT.handle_incoming(request)
108+
109+
110+
if __name__ == '__main__':
111+
112+
KIK_CLIENT = KikBotClient()
113+
114+
print("Kik Client running on %s:%s" % (KIK_CLIENT.configuration.client_configuration.host,
115+
KIK_CLIENT.configuration.client_configuration.port))
116+
117+
118+
if KIK_CLIENT.configuration.client_configuration.debug is True:
119+
print("Kik Client running in debug mode")
120+
121+
if KIK_CLIENT.configuration.client_configuration.ssl_cert_file is not None and \
122+
KIK_CLIENT.configuration.client_configuration.ssl_key_file is not None:
123+
context = (KIK_CLIENT.configuration.client_configuration.ssl_cert_file,
124+
KIK_CLIENT.configuration.client_configuration.ssl_key_file)
125+
126+
print("Kik Client running in https mode")
127+
APP.run(host=KIK_CLIENT.configuration.client_configuration.host,
128+
port=KIK_CLIENT.configuration.client_configuration.port,
129+
debug=KIK_CLIENT.configuration.client_configuration.debug,
130+
ssl_context=context)
131+
else:
132+
print("Kik Client running in http mode, careful now !")
133+
APP.run(host=KIK_CLIENT.configuration.client_configuration.host,
134+
port=KIK_CLIENT.configuration.client_configuration.port,
135+
debug=KIK_CLIENT.configuration.client_configuration.debug)

0 commit comments

Comments
 (0)