python - 如何通过 Python 中的 Dialogflow fulfillment 从 google assistant 发送推送通知

标签 python dialogflow-es actions-on-google

我正在谷歌 Dialogflow 中开发聊天机器人以获得谷歌帮助,我已经关注了这个 Documentation for showing Push notifications .我已请求许可,但现在我停留在该文档的最后一步(将 key 交换为访问 token 并发送通知)。

谁能帮我做一下。我应该从我的 Python 实现代码发送哪个 JSON 响应?

最佳答案

终于解决了这个问题。 @matthewayne 发布的代码有一些错误,例如请求必须是“POST”方法并且需要更改 payload 和 header 中的一些参数,因此它有效!,这里我更改了一些代码并尝试请求和它点击了通知!

我提到了 this documentation to make it worked!

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)

关于python - 如何通过 Python 中的 Dialogflow fulfillment 从 google assistant 发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51821919/

相关文章:

python - django 迁移时出错 : "There is no unique constraint matching given keys for referenced table

python - 使用xpath提取图像

node.js - 调用函数后运行的 if 语句

python - 比较多个 numpy 数组

Python - 字符替换的迭代

android - Google home 发送卡片到 Google Home 应用

actions-on-google - 相当于 Alexa 的 Google Assistant 告诉 [skill]

actions-on-google - 对话 Action 2023 年日落 : Migrating from conversational actions to Smart Home Actions

javascript - 由于未知原因,Dialogflow 代理不会在函数内说话

node.js - : DialogFlow + OAuth2. 0