python dialogflow_如何在Python中通过Dialogflow实现从Google Assis...

最后,我解决了这个问题. @matthewayne发布的代码有一些错误,例如它应该是“ POST”请求并更改一些有效负载和标头参数,因此它起作用了!在这里,我更改了一些代码并尝试请求,并命中了通知!

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': '',

'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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值