【问题标题】:How to send push notification from google assistant through Dialogflow fulfilment in Python如何通过 Python 中的 Dialogflow 实现从谷歌助手发送推送通知
【发布时间】:2026-01-20 09:10:02
【问题描述】:

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

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

【问题讨论】:

  • 您是否停留在“交换密钥以获取访问令牌并发送通知”步骤?
  • 是的,我被困在Exchange the key for an access token and send a notification step

标签: python dialogflow-es actions-on-google


【解决方案1】:

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

我推荐了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)

【讨论】:

    【解决方案2】:

    试试这个:

    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)
    

    【讨论】:

    • 我试过这个解决方案,但它给了Error: 400Your client has issued a malformed or illegal request.我已将我的机器人从 v1 更新到 v2
    • 我的有效载荷目标 json 格式类似于 'target': { 'userId': 'ABwppHEtlrhbdLhrSplziZ3mT8pFMkhbo4j6XFYT89GFrqwtV93B2EJr7BDOonpre0eoYhzmuM1bWMygbJIAmhUBZxc-', 'intent': 'tell_latest_tip', 'locale': 'en-US' } 如果需要任何更改或我犯了任何错误,请告诉我。我不确定localeintent 的名字。他们正确吗?
    • 嘿,谢谢您的代码不起作用,但它为我提供了一种找到解决方案的方法,并且在您的代码中进行了一些更改,推送通知功能可以正常工作!谢谢,伙计!
    最近更新 更多