问题描述
我正在Google Dialogflow中开发聊天机器人以获得Google帮助,我已经遵循了。我已经请求了许可,但是现在我陷入了最后一步(在该文档中交换访问令牌的密钥并发送通知
)。
任何人都可以帮助我做到这一点。我应该从我的Python履行代码发送哪个JSON响应?
最后,解决了问题。 @matthewayne发布的代码有一些错误,例如,请求必须是 POST方法,并且需要更改有效负载和标头中的某些参数,因此它起作用了!在这里,我更改了一些代码并尝试请求和
我引用了
import io
import json
从google.oauth2导入请求
导入service_account
导入google.auth.transport.requests
PATH_TO_SERVICE_ACCOUNT ='path / to / json /服务/帐户'
REQUIRED_SCOPE ='https://www.googleapis.com/auth/actions.fulfillment.conversation'
#通过以下方式获取访问令牌
io.open(PATH_TO_SERVICE_ACCOUNT,'r',encoding ='utf-8')as json_fi:
凭据_信息= json.load(json_fi)
凭据= service_account.Credentials.from_ service_account_info(
凭据_info,范围= [REQUIRED_SCOPE])
request = google.auth.transport.requests.Request()
凭据.refresh(request)
标头= {
'Authorization':'Bearer'+凭据。令牌
}
有效载荷= {
'customPushMessage':{
'userNotification': {
'title':'Notification title',
'text':'Simple Text'
},
'target':{
'userId':' < USER_ID>',
'intent':'< INTENT>',
#需要IETF BCP-47语言代码(即en-US)
'locale':'en-US'
}
}
}
r = requests.request( POST,' https://actions.googleapis.com/v2/conversations:send'、data=json.dumps(有效载荷),headers = headers)
print(str(r.status_code)+':' + r.text)
I'm developing chatbot in google Dialogflow for google assistance, I've followed this Documentation for showing Push notifications. I've asked for the permission but now I'm stuck at the last step(Exchange the key for an access token and send a notification
) in that documentation.
Can anybody please help me to do it. Which JSON response should I send from my Python fulfillment code?
Finally, solved the problem. The code posted by @matthewayne has some mistakes, for example the request must be of the "POST" method and need to change some parameters in payload and header, hence it worked!, here I've changed some code and tried to request and it hit a notification!
I referred 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中通过Dialogflow实现从Google Assistant发送推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!