将位置(lat& long)发送到API AI

时间:2016-10-04 11:32:31

标签: python json facebook

我正在使用API AI制作Facebook僵尸程序。在Facebook聊天机器人分享我的位置后。我有一个像这样的JSON:

{'message': {'attachments': [{'payload': {'coordinates': {'lat': 52.335001190772,'long': 4.8887078680234}},'title': 'Holiday Inn','type': 'location','url': 'https://www.facebook.com/l.php?u=https%3A%2F%2Fwww.bing.com%2Fmaps%2Fdefault.aspx%3Fv%3D2%26pc%3DFACEBK%26mid%3D8100%26where1%3DDe%2BBoelelaan%2B2%252C%2B1083%2BHJ%2BAmsterdam%252C%2BNetherlands%26FORM%3DFBKPL1%26mkt%3Den-US&h=mAQEt4NIX&s=1&enc=AZN97DQxpVq5xpkZqvgi3bMq2OVJNwWBOXOiIOW4FHx1-kgYHxTPKfFwRkUsl0ibr0K5GAquaEltxBMLGvjxmUbCa1AmptlN85rg4jLhDH6K0g'}],

根据那个JSON,我有lat和long值:

payload = message['message']['attachments'][0]['payload']
lat = payload['coordinates']['lat']
long = payload['coordinates']['long']

我想要的是将这些值发送到API AI中的parameters。所以我写了一个方法来发布:

def post_location(self, text, lat, long):
    return self.get(
        params={
            'query': text,
            'parameters': {
                    'latitude': lat,
                    'longitude': long,
            },
            'lang': 'en'
        }
    )

这就是我get的样子:

def get(self, params):
    """
    Get connection with api ai
    :rtype: object
    """
    print(params)

    request = requests.get(
        url=self.url, params=params, headers=self.headers)
    content = request.content.decode('utf-8')
    try:
        return json.loads(content)
    except ValueError:
        print('Invalid JSON')

最后,我调用post_location方法来处理facebook消息并为其提供值,但是当我运行时,只有text(查询)被发送到API AI。

def _post_location_to_api(message, lat, long):
    ai_api = AIApi()
    return ai_api.post_location(message, lat, long)

location = _post_location_to_api(message['message']['attachments'][0]['type'], latitude, longitude)

print(location)给了我这个:

{'id': 'd4374511-86ce-4ccb-b7b3-e813011a0998', 'sessionId': '00000000-0000-0000-0000-000000000000', 'timestamp': '2016-10-04T11:26:11.613Z', 'result': {'action': 'PlaceSearch', 'actionIncomplete': False, 'score': 0.51, 'contexts': [], 'fulfillment': {'speech': ''}, 'parameters': {'latitude': '', 'longitude': ''}, 'source': 'agent', 'resolvedQuery': 'location', 'metadata': {'intentName': 'I want to eat in Amsterdam', 'intentId': '89f515a6-f723-4df5-96b2-9e0f784747c6', 'webhookUsed': 'true', 'warning': 'IMPORTANT: Please use unique session ID for each client. Otherwise you may experience unstable behaviour.'}}, 'status': {'errorDetails': 'Webhook call failed. Status code 404. Error:404 Not Found', 'errorType': 'partial_content', 'code': 206}}

我做错了什么?

0 个答案:

没有答案