【问题标题】:[KIK MESSENGER]Configure Bot, JSON give a suggestion[KIK MESSENGER]配置Bot,JSON给个建议
【发布时间】:2017-11-27 12:56:33
【问题描述】:

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

用户:建议 Bot:我们很高兴听到您有建议!请在下方输入您的建议 用户:请添加 sumthin cool 博特:谢谢你的建议! [机器人将建议发送到我的 kik pringlejingle]

我想知道如何使用 JSON 来做到这一点

【问题讨论】:

    标签: json kik


    【解决方案1】:

    “pringlejingle”是聊天机器人吗?您将无法直接向您的用户帐户发送消息,但您可以构建一个聊天机器人来执行此操作。

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

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

    【讨论】:

      【解决方案2】:

      为此,您首先需要获取pringlejingle用户名的唯一chatid。

      为此,请从 pringlejingle 用户名向您的机器人发送消息(比如 Hi)。

      你的机器人会收到一个类似这样的 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 是聊天 ID,对于您的用户名(pringlejingle)显然会有所不同。

      获取这个聊天ID。

      然后使用 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>
              }
          ]
      }
      
      • 在聊天流中,创建一个数据库来存储建议信息(用户名、聊天 ID 等)。
      • 当用户发送建议请求时,将信息添加到数据库中。
      • 收到机器人的消息后,从数据库中检查用户之前是否请求过建议。如果在数据库中找到,则处理建议聊天流。

      这里是一个在 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的消息要发送到bot,以便它可以将pringlejingle的聊天ID存储在数据库中。完成此操作后,机器人可以处理“建议”聊天流。

      【讨论】:

        猜你喜欢
        • 2016-08-09
        • 1970-01-01
        • 2016-08-11
        • 2017-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多