[KIK MESSENGER]配置Bot,JSON提出建议

时间:2017-06-23 21:11:27

标签: json kik

我的问题是,我如何添加类似建议的内容,以下是它的工作原理

用户:建议 Bot:我们很高兴听到您的建议!请在下面输入您的建议 用户:Pleaz加上sumthin cool Bot:谢谢你的建议! [Bot将建议发送给我的kik pringlejingle]

我想知道如何使用JSON

2 个答案:

答案 0 :(得分:0)

' pringlejingle'聊天机器人?您无法直接向您的用户帐户发送邮件,但您可以构建一个聊天机器人来执行此操作。

请参阅https://dev.kik.com/以获取API参考。还有一个在github上可用的python样本机器人(在上面的开发文档中链接)。

您也可以联系bots@kik.com获取开发帮助。

答案 1 :(得分:0)

为此,您首先需要获得pringlejingle用户名的独特chatid。

要执行此操作,请从pringlejingle用户名向您的机器人发送消息(例如您好)。

您的机器人会收到类似这样的json对象:

{
    "messages": [
        {
            "chatId": "0ee6d46753bfa6ac2f089149959363f3f59ae62b10cba89cc426490ce38ea92d",
            "id": "0115efde-e54b-43d5-873a-5fef7adc69fd",
            "type": "text",
            "from": "pringlejingle",
            "participants": ["pringlejingle"],
            "body": "Hi",
            "timestamp": 1439576628405,
            "readReceiptRequested": true,
            "mention": null,
            "metadata": null,
            "chatType": "direct",
        }
    ]
}

在上面的json对象中,0ee6d46753bfa6ac2f089149959363f3f59ae62b10cba89cc426490ce38ea92d是chatid,你的用户名(pringlejingle)明显不同。

得到这个chatid。

使用json对象发出两个http post请求:

{
    'messages': [
        {
            'body': 'Thanks for suggesting!', 
            'to': <username of the sender>, 
            'type': 'text', 
            'chatId': <chat id of the sender>
        }
    ]
}

{
    'messages': [
        {
            'body': <suggestion of the sender>, 
            'to': 'pringlejingle', 
            'type': 'text', 
            'chatId': <chat id of pringlejingle>
        }
    ]
}
  • 在聊天流程中,建立一个数据库来存储建议信息(用户名,chatid等)。
  • 当用户发送建议请求时,请将信息添加到数据库中。
  • 当收到机器人的消息时,请从数据库中检查用户之前是否已请求过建议。如果在数据库中找到,请处理建议chatflow。

以下是在Python中使用json对象的示例:

import json
import sys
import requests
import sqlite3

def postdata(username=None,chatid=None,message=None):
    botusername=<USERNAMEOFYOURBOT>
    botapikey=<APIKEYOFYOURBOT>
    r=requests.post(
        'https://api.kik.com/v1/message',
        auth=(botusername,botapikey),
        headers={
            'Content-Type': 'application/json'
        },
        data=json.dumps({
            'messages': [
                {
                    'body': message, 
                    'to': username, 
                    'type': 'text', 
                    'chatId': chatid
                }
            ]
        })
    )
    return r

form=json.loads(sys.stdin.read())
conn=sqlite3.connect('suggestionsdatabase.db')
c=conn.cursor()

# to create tables if tables were not found
if ('suggestions',) not in list(c.execute("SELECT NAME FROM sqlite_master WHERE type='table';")):
    c.execute('CREATE suggestions (username TEXT, chatid TEXT)')
    conn.commit()
if ('pringlejingle-chatid',) not in list(c.execute("SELECT NAME FROM sqlite_master WHERE type='table';")):
    c.execute('CREATE pringlejingle-chatid (chatid TEXT)')
    conn.commit()

# to store the chatid of pringlejingle
if form['messages'][0]['from']=='pringlejingle':
    c.execute('INSERT INTO pringlejingle-chatid VALUES (?)',(form['messages'][0]['chatId'],))
    conn.commit()
    outputmessage='Your chat id is successfully saved in the database.'
    postdata(username=form['messages'][0]['from'],chatid=form['messages'][0]['chatId'],message=outputmessage)

#processing the suggestions chatflow.
checksuggestions=list(c.execute('SELECT * FROM suggestions WHERE username=? AND chatid=?',(form['messages'][0]['from'],form['messages'][0]['chatId'],)))
if len(checksuggestions)!=0:
    postdata(username=form['messages'][0]['from'],chatid=form['messages'][0]['chatId'],message='Thanks for suggesting!')
    pringlejinglechatid=list(c.execute('SELECT * FROM pringlejingle-chatid'))
    formattedsuggestion='Suggestion from '+form['messages'][0]['from']+':\n\n'+form['messages'][0]['body']+'\n-------end of the suggestion-------'
    postdata(username='pringlejingle',chatid=pringlejinglechatid,message=formattedsuggestion)

elif form['messages'][0]['body'].lower()=='suggestions':
    outputmessage="We're happy to hear that you have a suggestion!"
    c.execute('INSERT INTO suggestions VALUES (?,?)',(form['messages'][0]['from'],form['messages'][0]['chatId'],))
    conn.commit()
    postdata(username=form['messages'][0]['from'],chatid=form['messages'][0]['chatId'],message=outputmessage)
else:
    outputmessage='I am not yet programmed for this input.'
    postdata(username=form['messages'][0]['from'],chatid=form['messages'][0]['chatId'],message=outputmessage)

首先,来自pringlejingle的消息将被发送到机器人,以便它可以将pringlejingle的聊天ID存储在数据库中。完成后,机器人可以处理建议&#39; chatflow。