首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Python中通过Dialogflow实现从google助手发送推送通知

如何在Python中通过Dialogflow实现从google助手发送推送通知
EN

Stack Overflow用户
提问于 2018-08-13 20:01:45
回答 2查看 940关注 0票数 2

我正在google Dialogflow中开发聊天机器人,以获得谷歌的帮助,我已经遵循了这个Documentation for showing Push notifications。我已经请求了许可,但现在我被困在文档中的最后一步(Exchange the key for an access token and send a notification)。

有人能帮我做这件事吗?我应该从我的Python实现代码发送哪个JSON响应?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-22 15:21:28

最后,解决了这个问题。@matthewayne发布的代码有一些错误,例如请求必须是"POST“方法,并且需要更改有效负载和头部中的一些参数,因此它工作了!这里我更改了一些代码并尝试请求,但它命中了一个通知!

我推荐了this documentation to make it worked!

代码语言:javascript
复制
import io
import json

import requests
from google.oauth2 import service_account
import google.auth.transport.requests

PATH_TO_SERVICE_ACCOUNT = 'path/to/json/service/account'

REQUIRED_SCOPE = 'https://www.googleapis.com/auth/actions.fulfillment.conversation'

# Get access token
with io.open(PATH_TO_SERVICE_ACCOUNT, 'r', encoding='utf-8') as json_fi:
    credentials_info = json.load(json_fi)
credentials = service_account.Credentials.from_service_account_info(
    credentials_info, scopes=[REQUIRED_SCOPE])
request = google.auth.transport.requests.Request()
credentials.refresh(request)

headers = {
    'Authorization': 'Bearer ' + credentials.token
}

payload = {
    'customPushMessage': {
        'userNotification': {
            'title': 'Notification title',
            'text': 'Simple Text'
        },
        'target': {
            'userId': '<USER_ID>',
            'intent': '<INTENT>',
            # Expects a IETF BCP-47 language code (i.e. en-US)
            'locale': 'en-US'
        }
    }
}

r = requests.request("POST", 'https://actions.googleapis.com/v2/conversations:send', data=json.dumps(payload), headers=headers)

print(str(r.status_code) + ': ' + r.text)
票数 3
EN

Stack Overflow用户

发布于 2018-08-21 05:44:47

试试这个:

代码语言:javascript
复制
import io
import json

import requests
from google.oauth2 import service_account
import google.auth.transport.requests

PATH_TO_SERVICE_ACCOUNT = 'path/to/json/service/account'

REQUIRED_SCOPE = 'https://www.googleapis.com/auth/actions.fulfillment.conversation'

# Get access token
with io.open(PATH_TO_SERVICE_ACCOUNT, 'r', encoding='utf-8') as json_fi:
    credentials_info = json.load(json_fi)
credentials = service_account.Credentials.from_service_account_info(
            credentials_info,  scopes=[REQUIRED_SCOPE])
request = google.auth.transport.requests.Request()
credentials.refresh(request)

headers = {
    'Authorization': 'Bearer: ' + credentials.token
}

payload = {'customPushMessage': {
    'userNotification': {
      'title': '<NOTIFICATION_TITLE>',
    },
    'target': {
      'userId': '<USER_ID>',
      'intent': '<INTENT>',
      # Expects a IETF BCP-47 language code (i.e. en-US)
      'locale': '<LOCALE>'
    },
  }
}

r = requests.get('https://actions.googleapis.com/v2/conversations:send', json=payload, headers=headers)

print(str(r.status_code) + ': ' + r.text)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51821919

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档