Jay Patel
Jay Patel

Reputation: 2451

How to send push notification from google assistant through Dialogflow fulfilment in Python

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?

Upvotes: 1

Views: 1270

Answers (2)

Jay Patel
Jay Patel

Reputation: 2451

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)

Upvotes: 2

mattcarrollcode
mattcarrollcode

Reputation: 3469

Try this:

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)

Upvotes: 1

Related Questions