Get Google Business Notifications from push Pub/Sub - google-cloud-platform

I'm trying to be notified when a new review is added on my Google Business Profile.
According to the documentation, I have setup the notification but I got nothing when a new review is added.
First of all, I have created a Pub/Sub Topic projects/my-project/topics/business-profile-notifications.
Then, I have created a Push subscription projects/my-project/subscriptions/business-profile-notifications-push attached to the previous created Topic. I have also defined an endpoint: https://my-endpoint/webhook. This endpoint is listening POST requests
Finally, I have added the service account mybusiness-api-pubsub#system.gserviceaccount.com into IAM with Pub/Sub admin role.
On the code side, I'm using NPM googleapis client in a TypeScript Node.js server.
I'm updating the account settings to setup the notifications:
const { data }: GaxiosResponse<mybusinessnotifications_v1.Schema$NotificationSetting> = await google.mybusinessnotifications({
version: 'v1',
auth,
}).accounts.updateNotificationSetting({
name: `accounts/${params.accountID}/notificationSetting`,
updateMask: 'notification_types',
requestBody: {
name: `accounts/${params.accountID}/notificationSetting`,
pubsubTopic: 'projects/my-project/topics/business-profile-notifications',
notificationTypes: [
'NEW_REVIEW',
'UPDATED_REVIEW',
],
},
});
At this point, nothing happens when a new review is added.
When I'm sending a POST request on my endpoint via curl command curl -X POST -H "Content-Type: application/json" -i "https://my-endpoint/webhook", the request is successfully catched.
In the other hand, when I'm getting notifications settings from the configured account, I have the notifications types but not any subscribed topic:
const { data }: GaxiosResponse<mybusinessnotifications_v1.Schema$NotificationSetting> = await google.mybusinessnotifications({
version: 'v1',
auth,
}).accounts.getNotificationSetting({
name: `accounts/${accountID}/notificationSetting`,
fields: 'pubsubTopic,notificationTypes',
});
Response:
{
"notificationTypes": [
"NEW_REVIEW",
"UPDATED_REVIEW"
]
}
What I forgot to do ?

I resolved the issue by myself 😁
In the documentation of updateNotificationSetting method, is it stated that "The only editable field is notificationSetting" about the updateMask field. But it's wrong. I had to add pubsubTopic as value.
Finally, the parameter values of this method are:
const opts = {
name: `accounts/${params.accountID}/notificationSetting`,
updateMask: 'notificationTypes,pubsubTopic',
requestBody: {
name: `accounts/${params.accountID}/notificationSetting`,
pubsubTopic: params.pubsubTopic,
notificationTypes: params.notificationTypes,
},
};
const { data }: GaxiosResponse<mybusinessnotifications_v1.Schema$NotificationSetting> = await google.mybusinessnotifications({
version: 'v1',
auth,
}).accounts.updateNotificationSetting(opts);

Related

Webhook JSON return from Cloud Run Endpoint not showing in Dialogflow CX

I developed a chatbot solution with Dialogflow CX, where a question is made by the user, and Dialogflow uses an unauthenticated webhook (temporary) to call a Cloud Run endpoint. Cloud Run runs a Python code with a Flask application that jasonifies the output of the algorithm.
Locally I can call the Cloud Run endpoint successfully (200) with the following script:
import requests
url='https://container-xxx-acc12345.run.app/predict'
r = requests.post(url, json={"text": "which is the deadline provided by the law?"})
print(r.json())
Which returns me:
{'prediction': 'Second year after initial deadline'}
Then when I go back to Dialogflow, it successfully calls the Cloud Run Endpoint (200 above), but I do not get a valid text response in the chat:
Cloud Run allows unauthenticated calls and the webhook is well configured. The webhook was added at the parameters session of the Ask Question page, whose tag is prediction.
As configured at the event handlers, the chatbot does not show any webhook error, but the result of the prediction does not show up in the chat.
Cloud Logging data:
httpRequest: {
latency: "0.010078946s"
protocol: "HTTP/1.1"
remoteIp: "22.222.222.164"
requestMethod: "POST"
requestSize: "2327"
requestUrl: "https://container-xxx-1234abc.run.app/predict"
responseSize: "959"
serverIp: "222.222.34.33"
status: 200
userAgent: "Google-Dialogflow"
}
This is the return of Flask application:
return jsonify({
"fulfillment_response": {
"messages": [{
"text":
predict(data)
}]
}
})
Any ideas on how to solve this issue are welcome.
I solved the problem. My return from Flask app in Cloud Run is fixed to:
return jsonify({
"fulfillment_response": {
"messages": [{
"text": {
"text": [
predict(data)
]
}
}]
}
})
I also found out you can define the body request payload also.

gmail users.watch API 400 Bad Request using domain wide delegation

Getting 400 Bad Request when calling users.watch Gmail API from GCP Cloud Function
I'm trying to automate the call to watch() on a users GSuite email account. I have already given domain-wide access to the default service account of the Cloud Function by following the steps outlined in the link below
https://developers.google.com/admin-sdk/reports/v1/guids/delegation
I have authorized the following scopes:
profile,email, https://mail.google.com/,https://www.googleapis.com/auth/gmail.metadata, https://www.googleapis.com/auth/gmail.modify, https://www.googleapis.com/auth/gmail.readonly
Deployed Cloud Function code:
exports.watchEmail = async () => {
console.info('authenticating');
const auth = await google.auth.getClient({
scopes: ['profile',
'email',
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.metadata',
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/gmail.readonly'],
});
console.info('<-- authenticated');
console.info('watch on email', 'no-reply#telico.ca');
const api = google.gmail({ version: 'v1', auth });
const response = await api.users.watch({
userId: '<USER_EMAIL>',
requestBody: {
topicName: 'projects/<PROJECT_ID>/topics/processEmail',
labelIds: ["INBOX"]
}
});
console.info('<-- watch on file', JSON.stringify(response));
};
When executing the CloudFunction, I am seeing Error: Bad Request at Gaxios.request printed in the logs.

NodeMailer Gmail API in AWS Lambda not working

I'm using nodemailer with gmail API to send mails.
Following code :-
var nodemailer = require('nodemailer');
var transporter = await nodemailer.createTransport({
service: 'Gmail',
auth: {
user: 'xx#usr.com',
pass: 'xxx'
}
});
console.log("Starting");
await transporter.sendMail({
from: 'xx#google.com',
to: 'xx#google.com',
subject: 'Hello !',
text: "Hello"
}, function(data, info){
});
Code is working perfectly on local & sending mails.
But, when used inside lambda nothing is happening. Function getting executed successfully.
The error would probably be in the (1) code, (2) IAM permissions (for SES, see Nodemailer SES doc, Connecting to the Amazon SES SMTP Endpoint and SES FAQ) or
(3) networking (if lambda runs from private VPC and networking isn't fully configured).
Or maybe its working fine (see #TheProgrammer's comment in this question)
To view and understand the error, try the following:
Adding logging to the error callback of sendMail method (see this):
transporter.sendMail({
from: 'sender#example.com',
to: 'recipient#example.com',
subject: 'Message',
text: 'I hope this message gets delivered!'
}, (err, info) => {
console.log(info.envelope);
console.log(info.messageId);
console.log(err);
});
Viewing the error in CloudWatch logs (accessed directly from View logs in CloudWatch, docs)

Client authentication failed in Postman request for Amazon Alexa Smart Home Skill LWA

I am referring to Amazon documentation for the purpose of Customer Authentication. Currently, I am using LWA.
Steps I followed:
I enabled the Send Alexa Events Permission from the Alexa developer Console in Build > Permission page.
I took the grant code from the request in the cloudwatch logs which was sent when I logged in using Alexa companion app.
Example:-
{
"directive": {
"header": {
"messageId": "Example",
"name": "AcceptGrant",
"namespace": "Alexa.Authorization",
"payloadVersion": "3"
},
"payload": {
"grant": {
"code": "Example2",
"type": "OAuth2.AuthorizationCode"
},
"grantee": {
"token": "Example3",
"type": "BearerToken"
}
}
}
}
Permission Page under build on Alexa Developer console gave me client-Id and client-secret Which I used for making the post request to https://api.amazon.com/auth/o2/token.
Example:-
POST /auth/o2/token HTTP/l.l
Host: api.amazon.com
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
grant_type=authorization_code&code=&client_id=&client_secret=
I passed the code,client_id, and client_secret in the above example and made the post request to this URL https://api.amazon.com/auth/o2/token
I tried using x-www-form-urlencoded;charset=UTF-8 and also JSON for the Content-Type.
I followed the step given in the above documentation and I am stuck on the error ( 401 Unauthorized ):
{
"error_description": "The request has an invalid grant parameter : code",
"error": "invalid_grant"
}
I tried implementing it using Python code and Postman both. Ending up with the Same above error scenario.
Here is a sample code to help you and others who are looking to send events to alexa gateway.
const AWS = require('aws-sdk');
AWS.config.update({region: 'eu-west-1'});
// Create the DynamoDB service object
const ddb = new AWS.DynamoDB({ apiVersion: 'latest' });
const doc = new AWS.DynamoDB.DocumentClient({
convertEmptyValues: true,
service: ddb
});
// Using 'request' for http POST and GET request.
// https://www.npmjs.com/package/requests
// npm install --save requests
const r = require('request');
//Handle Authorization. Call this method from your lambda handler whenever you get Alexa.Authorization message. You will get this message only when you select permission to
//send events in your Smart Home Skill.
//Access to Event gateway allows you to enable Proactive Device Discovery and
//Proactive State Reporting in your skill
//More information on Alexa.Authorization can be found on https://developer.amazon.com/docs/device-apis/alexa-authorization.html
function handleAuthorization(request, context, user) {
//Even when you are using your own authentication, the url below will still
//point to amazon OAuth token url. The token you obtain here has to be stored
//separately for this user. Whenever sending an event to alexa event gateway you will
//require this token.
//URL below is for EU server. Look at following documentation link to identify correct url
//for your system.
//https://developer.amazon.com/docs/smarthome/send-events-to-the-alexa-event-gateway.html
var url = "https://api.amazon.com/auth/o2/token";
var body = {
grant_type : 'authorization_code',
code : request.directive.payload.grant.code,
client_id : 'your client id from permissions page on developer portal where you enable alexa events. This is id different than one you specify in account linking settings',
client_secret : 'client secret from permissions page'
}
//https://developer.amazon.com/docs/smarthome/authenticate-a-customer-permissions.html
r.post({
url: url,
form : body
}, function(error, response, b){
if (error) { return console.log(error); }
var body = JSON.parse(b);
var params = {
TableName: 'Devices',
Item: {
'id' : user,
'auth_token' : body.access_token,
'refresh_token' : body.refresh_token
}
}
log("DEBUG:", "Authorization Body", JSON.stringify(body));
log("DEBUG:", "Authorization Response", JSON.stringify(response));
log("DEBUG:", "Database Params", JSON.stringify(params));
// Call DynamoDB to add the item to the table
var putObjectPromise = doc.put(params).promise();
//Store auth_token and refresh_token in database. We will need these
//while sending events to event gateway.
//Send a success response.
putObjectPromise.then(function(data) {
var response = {
event: {
header: {
messageId: request.directive.header.messageId,
namespace: "Alexa.Authorization",
name: "AcceptGrant.Response",
payloadVersion: "3"
},
"payload": {
}
}
};
context.succeed(response);
}).catch(function(err) {
//TODO - Add a Authorization error response JSON here.
console.log(err);
});
});
}

How can i confirm the subscription request HTTP from amazon SNS

I have been searching all over the web and nothing gives a clear answer to confirm the subscription request from amazon SNS. I already send the subscription from the amazon console to my website, but what's next? I am using amazon EC2 as my server with PHP.
Before you even configure the HTTP/HTTPS endpoint subscription through AWS management console, you need to make sure that the HTTP or HTTPS endpoint of your PHP web site has the capability to handle the HTTP POST requests that Amazon SNS generates. There are several types of SNS messages: SubscriptionConfirmation, Notification and UnsubscribeConfirmation. Your PHP code needs to get the header x-amz-sns-message-type from request and process it based on the message type. For SubscriptionConfirmation message, your PHP application needs to process the POST message body, which is a JSON document. In order to subscribe the topic, your PHP code needs to visit the "SubscriberURL" specified in the JSON body. Optionally, you should verify the signature to make sure the authenticity of message before subscribing the topic.
You can find more details on AWS documentation: http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html
Here is an express application (Node.js) which confirms the SNS subscription:
const express = require('express')
const request = require('request')
// parse urlencoded request bodies into req.body
const bodyParser = require('body-parser')
const app = express()
const port = 8080
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.post('/', (req, res) => {
let body = ''
req.on('data', (chunk) => {
body += chunk.toString()
})
req.on('end', () => {
let payload = JSON.parse(body)
if (payload.Type === 'SubscriptionConfirmation') {
const promise = new Promise((resolve, reject) => {
const url = payload.SubscribeURL
request(url, (error, response) => {
if (!error && response.statusCode == 200) {
console.log('Yess! We have accepted the confirmation from AWS')
return resolve()
} else {
return reject()
}
})
})
promise.then(() => {
res.end("ok")
})
}
})
})
app.listen(port, () => console.log('Example app listening on port ' + port + '!'))
To use it one needs to install required packages:
yarn add express request body-parser
Once you confirm the subscription AWS will send a POST request to the server with the following content:
{
"Type": "SubscriptionConfirmation",
"MessageId": "XXXXXXXX-1ee3-4de3-9c69-XXXXXXXXXXXX",
"Token": "SECRET_TOKEN",
"TopicArn": "arn:aws:sns:us-west-2:XXXXXXXXXXXX:ses-test",
"Message": "You have chosen to subscribe to the topic arn:aws:sns:us-west-2:XXXXXXXXXXXX:ses-test. To confirm the subscription, visit the SubscribeURL included in this message.",
"SubscribeURL": "https://sns.us-west-2.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:us-west-2:XXXXXXXXXXXX:ses-test&Token=SECRET_TOKEN",
"Timestamp": "2018-11-21T19:48:08.170Z",
"SignatureVersion": "1",
"Signature": "SECRET",
"SigningCertURL": "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.pem"
}
The payload contains SubscribeURL which is requested by the server.
The end point you have specified will get data from AWS SNS endpoint verification service, The same end point will be used to verify the end point and to get notifications from aws,
Simply dump the input sent by AWS SNS into one text file like,
$json_write_to_text = json_decode(file_get_contents("php://input"));
You will find all data sent by AWS SNS, but just find SubscriptionUrl (which will be specific for endpoint having valid token), Open this in browser you will have SubscriptionConfirmation status. That's it
Enjoy.
Spring cloud SNS subscription with annotation
spring cloud AWS has support for auto confirmation of subscriber, you just need to put this annotation "#NotificationSubscriptionMapping"
#Controller
#RequestMapping("/topicName")
public class NotificationTestController {
#NotificationSubscriptionMapping
public void handleSubscriptionMessage(NotificationStatus status) throws IOException {
//We subscribe to start receive the message
status.confirmSubscription();
}
#NotificationMessageMapping
public void handleNotificationMessage(#NotificationSubject String subject, #NotificationMessage String message) {
// ...
}
#NotificationUnsubscribeConfirmationMapping
public void handleUnsubscribeMessage(NotificationStatus status) {
//e.g. the client has been unsubscribed and we want to "re-subscribe"
status.confirmSubscription();
}
}
http://cloud.spring.io/spring-cloud-aws/spring-cloud-aws.html#_sns_support
I solved this using NodeJS backend. Lets say you have an API like this in HapiJS (Well it doesnt matter you can have another tech)
{
method: 'POST',
path: '/hello',
handler: ( request, reply ) => {
reply( Hello.print(request.payload) );
},
config: {
tags: ['api']
}
}
Just pass the payload you receive, on to your business logic.
In the business logic process it like this
'use strict';
const request = require('request');
exports.print = (payload) => {
payload = JSON.parse(payload);
if(payload.Type === 'SubscriptionConfirmation'){
return new Promise((resolve, reject) => {
const url = payload.SubscribeURL;
request(url, (error, response) => {
if (!error && response.statusCode == 200) {
console.log('Yess! We have accepted the confirmation from AWS');
return resolve();
}
else
return reject();
});
});
}
I am using request module from NPM to automatically accept such requests.
Another way would be to print the contents of payload and then click on the URL given in payload.SubscribeURL.
Once AWS accepts it you check the confirmation on the Subscriptions page where Subscription ARN would be changed from Pending Confirmation to a complex name-cum-SHA having your Topic name.