我的问题是,我如何添加一些建议,这是它的工作原理
用户:建议博特:我们很高兴听到您有建议!请在下面输入您的建议用户:pleaz添加sumthin酷机器人:感谢您的建议![bot将建议发送给我的kik pringlejingle]
id想知道如何使用JSON
是'pringlejingle'聊天机器人吗?您将无法将消息直接发送到您的用户帐户,但是您可以构建聊天机器人来执行此操作。
请查看https://dev.kik.com/以获取API参考。GitHub上还有一个Python样品机器人(上述开发文档中的链接(。
您也可以联系bots@kik.com寻求开发帮助。
为此,您首先需要获得pringlejingle用户名的唯一chatid。
要执行此操作,请将消息(例如 HI (从PringleJingle用户名发送给您的bot。
您的机器人会收到一个类似的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帖子请求:
{
'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']+':nn'+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存储在数据库中。完成此操作后,机器人可以处理"建议"聊天流。