“如何在Google Assistant操作中通过userID获取用户名?”

时间:2019-03-27 08:42:43

标签: actions-on-google

我已将聊天机器人连接到Google Assistant操作。他们只提供了userID,如何使用此userID获取用户名?

2 个答案:

答案 0 :(得分:0)

通过权限document here,您可以在不知道用户ID的情况下获取用户名。您可以查看this sample code

或者您可以使用account linking功能。

提示!对于userID,您可以签出此doc

对于Python:

尚无用于使用Python开发Google动作的官方库,但是, 您可以在possibleIntent数组中添加权限意图。因此,您的Action SDK JSON将为

{
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "PLACEHOLDER"
              }
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.PERMISSION",
          "inputValueData": {
            "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
            "optContext": "To address you by name and know your location",
            "permissions": [
              "NAME",
              "DEVICE_PRECISE_LOCATION"
            ]
          }
        }
      ]
    }
  ],
  "conversationToken": "{\"data\":{}}",
  "userStorage": "{\"data\":{}}"
}

答案 1 :(得分:0)

{`"actions": [
  {
    "description": "Default Welcome Intent",
    "name": "MAIN",
    "fulfillment": {
      "conversationName": "welcome"
    },
    "intent": {
      "name": "actions.intent.MAIN",
      "trigger": {
        "queryPatterns":["talk to Mr Bot"]
      }
    }
  },
  {
    "description": "Rasa Intent",
    "name": "TEXT",
    "fulfillment": {
      "conversationName": "rasa_intent"
    },
    "intent": {
      "name": "actions.intent.TEXT",
      "trigger": {
        "queryPatterns":[]
      }
    }
  }],
"conversations": {
  "welcome": {
    "name": "welcome",
    "url": "https://ac752bb0.ngrok.io/webhooks/google_home/webhook",
    "fulfillmentApiVersion": 2
},
  "rasa_intent": {
    "name": "rasa_intent",
    "url": "https://ac752bb0.ngrok.io/webhooks/google_home/webhook",
    "fulfillmentApiVersion": 2
}

}}

这是我的action.json,

GoogleConnector(InputChannel)类:

@classmethod
def name(cls):
    return "google_home"

#def __init__(self):
#    self.out_channel = CustomOutput(url, access_token)

def blueprint(self, on_new_message):

    google_webhook = Blueprint('google_webhook', __name__)

    @google_webhook.route("/", methods=['GET'])
    def health():
        return jsonify({"status": "ok"})

    @google_webhook.route("/webhook", methods=['POST'])
    def receive():
        payload = json.loads(request.data)      
        sender_id = payload['user']['userId']
        intent = payload['inputs'][0]['intent']             
        text = payload['inputs'][0]['rawInputs'][0]['query']        
        if intent == 'actions.intent.MAIN': 
            message = "<speak>Hello! <break time=\"1\"/> Welcome to the Rasa-powered Google Assistant skill. You can start by saying hi."            
        else:
            out = CollectingOutputChannel()         
            on_new_message(UserMessage(text, out, sender_id))
            responses = [m["text"] for m in out.messages]
            message = responses[0]  
        r = json.dumps(
            {
              "conversationToken": "{\"state\":null,\"data\":{}}",
              "expectUserResponse": 'true',
              "expectedInputs": [
                {
                  "inputPrompt": {
                   "initialPrompts": [
                    {
                      "ssml": message
                    }
                  ]
                 },
                "possibleIntents": [
                {
                  "intent": "actions.intent.TEXT"
                }
               ]
              }
             ]
            })
        return r                

    return google_webhook

这是我的Google连接器python代码, 如何修改此帐户登录信息