我如何使用 node.js 在 firebase 云 firestore 中添加 sendgrid webhook 事件 Json 响应
how can i add the sendgrid webhook event Json response in a firebase cloud firestore using node.js
我不知道如何实现这个东西,但在此之前,我已经完成了 SendGrid 的一部分,其中创建了任何文档,然后它将电子邮件发送给用户。但是我要问的这一部分我不知道如何 proceed.this 是我这个实现的第一部分,其中如果创建了新记录,那么任何集合都会将电子邮件发送到特定的电子邮件,并且有一个名为 event 的响应对象我想写一个云函数来存储数据。而且我不知道如何启动此功能或继续处理此问题。
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
var serviceAccount1 = require("./key.json");
const newProject = admin.initializeApp({
credential: admin.credential.cert(serviceAccount1),
databaseURL: "xyz"
});
const sgMail = require("@sendgrid/mail");
const sgMailKey = "key";
sgMail.setApiKey(sgMailKey);
exports.sentMail = functions.firestore
.document("/Offices/{officeId}")
.onCreate((documentSnapshot,event) => {
const documentData = documentSnapshot.data()
const officeID = event.params.officeId;
console.log(JSON.stringify(event))
const db = newProject.firestore();
return db.collection("Offices").doc(officeID).get()
.then(doc => {
const data = doc.data();
const msg = {
to: "amarjeetkumars34@gmail.com",
from: "singhamarjeet045@gmail.com",
text: "hello from this side",
templateId: "d-8ecfa59aa9d2434eb8b7d47d58b4f2cf",
substitutionWrappers: ["{{", "}}"],
substitutions: {
name: data.name
}
};
return sgMail.send(msg);
})
.then(() => console.log("payment mail sent success"))
.catch(err => console.log(err));
});
我的问题的预期输出就像一个集合名称 XYZ,其中一个对象有三个字段,如
{email:"xyz@gmail.com",
event:"processed",
timestamp:123555558855},
{email:"xyz@gmail.com",
event:"recieved",
timestamp:123555558855},
{email:"xyz@gmail.com",
event:"open",
timestamp:123555558855}
正如您将在 Sendgrid documentation 中看到的那样:
SendGrid's Event Webhook will notify a URL of your choice via HTTP
POST with information about events that occur as SendGrid processes
your email
要在您的 Firebase 项目中实施 HTTP 端点,您将实施一个 HTTPS Cloud Function,它将由 Sendgrid webhook 通过 HTTPS POST 请求调用。
来自 Sendgrid webhook 的每个调用都将涉及一个特定的 event,您将能够在您的 Cloud Function 中获取事件的值(processed
、delivered
等等...)。
现在,您需要在您的 Cloud Function 中能够 link 一个特定的事件,其中包含之前通过您的 Cloud Function 发送的特定电子邮件。为此你应该使用 custom arguments.
更准确地说,您将向 msg
对象(您传递给 send()
方法)添加一个唯一标识符。经典值是 Firestore 文档 ID,例如 event.params.officeId
,但也可以是您在 Cloud Function 中生成的任何其他唯一 ID。
实施示例
在您发送电子邮件的 Cloud Function 中,将 officeId
传递到 custom_args
对象中,如下所示:
exports.sentMail = functions.firestore
.document("/Offices/{officeId}")
.onCreate((documentSnapshot,event) => {
const documentData = documentSnapshot.data();
const officeId = event.params.officeId;
const msg = {
to: "amarjeetkumars34@gmail.com",
from: "singhamarjeet045@gmail.com",
text: "hello from this side",
templateId: "d-8ecfa59aa9d2434eb8b7d47d58b4f2cf",
substitutionWrappers: ["{{", "}}"],
substitutions: {
name: documentData.name
},
custom_args: {
"officeId": officeId
}
};
return sgMail.send(msg)
.then(() => {
console.log("payment mail sent success"));
return null;
})
.catch(err => {
console.log(err)
return null;
});
});
请注意,您通过documentSnapshot.data()
获取新创建的文档(触发云功能的文档)的数据:您不需要在云功能中查询相同的文档。
然后,创建一个简单的HTTPS Cloud Function,如下:
exports.sendgridWebhook = functions.https.onRequest((req, res) => {
const body = req.body; //body is an array of JavaScript objects
const promises = [];
body.forEach(elem => {
const event = elem.event;
const eventTimestamp = elem.timestamp;
const officeId = elem.officeId;
const updateObj = {};
updateObj[event] = true;
updateObj[event + 'Timestamp'] = eventTimestamp;
promises.push(admin.firestore().collection('Offices').doc(officeId).update(updateObj));
});
return Promise.all(promises)
.then(() => {
return res.status(200).end();
})
})
部署它并获取它的 URL,如终端中所示:它应该像 https://us-central1-<your-project-id>.cloudfunctions.net/sendgridWebhook
.
注意这里我用的是admin.firestore().collection('Offices')...
。您可以使用 const db = newProject.firestore(); ... db.collection('Offices')...
另请注意,Sendgrid webhook 发送的 HTTPS POST 请求的主体包含一个 JavaScript 对象数组,因此我们将使用 Promise.all()
来处理这些不同的对象,即使用 officeId
不同的事件写入 Firestore 文档。
然后您需要在 Sendgrid 平台的“邮件 Settings/Event 通知”部分设置 Webhook,如 doc 中所述,如下所示。
我不知道如何实现这个东西,但在此之前,我已经完成了 SendGrid 的一部分,其中创建了任何文档,然后它将电子邮件发送给用户。但是我要问的这一部分我不知道如何 proceed.this 是我这个实现的第一部分,其中如果创建了新记录,那么任何集合都会将电子邮件发送到特定的电子邮件,并且有一个名为 event 的响应对象我想写一个云函数来存储数据。而且我不知道如何启动此功能或继续处理此问题。
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
var serviceAccount1 = require("./key.json");
const newProject = admin.initializeApp({
credential: admin.credential.cert(serviceAccount1),
databaseURL: "xyz"
});
const sgMail = require("@sendgrid/mail");
const sgMailKey = "key";
sgMail.setApiKey(sgMailKey);
exports.sentMail = functions.firestore
.document("/Offices/{officeId}")
.onCreate((documentSnapshot,event) => {
const documentData = documentSnapshot.data()
const officeID = event.params.officeId;
console.log(JSON.stringify(event))
const db = newProject.firestore();
return db.collection("Offices").doc(officeID).get()
.then(doc => {
const data = doc.data();
const msg = {
to: "amarjeetkumars34@gmail.com",
from: "singhamarjeet045@gmail.com",
text: "hello from this side",
templateId: "d-8ecfa59aa9d2434eb8b7d47d58b4f2cf",
substitutionWrappers: ["{{", "}}"],
substitutions: {
name: data.name
}
};
return sgMail.send(msg);
})
.then(() => console.log("payment mail sent success"))
.catch(err => console.log(err));
});
我的问题的预期输出就像一个集合名称 XYZ,其中一个对象有三个字段,如
{email:"xyz@gmail.com",
event:"processed",
timestamp:123555558855},
{email:"xyz@gmail.com",
event:"recieved",
timestamp:123555558855},
{email:"xyz@gmail.com",
event:"open",
timestamp:123555558855}
正如您将在 Sendgrid documentation 中看到的那样:
SendGrid's Event Webhook will notify a URL of your choice via HTTP POST with information about events that occur as SendGrid processes your email
要在您的 Firebase 项目中实施 HTTP 端点,您将实施一个 HTTPS Cloud Function,它将由 Sendgrid webhook 通过 HTTPS POST 请求调用。
来自 Sendgrid webhook 的每个调用都将涉及一个特定的 event,您将能够在您的 Cloud Function 中获取事件的值(processed
、delivered
等等...)。
现在,您需要在您的 Cloud Function 中能够 link 一个特定的事件,其中包含之前通过您的 Cloud Function 发送的特定电子邮件。为此你应该使用 custom arguments.
更准确地说,您将向 msg
对象(您传递给 send()
方法)添加一个唯一标识符。经典值是 Firestore 文档 ID,例如 event.params.officeId
,但也可以是您在 Cloud Function 中生成的任何其他唯一 ID。
实施示例
在您发送电子邮件的 Cloud Function 中,将 officeId
传递到 custom_args
对象中,如下所示:
exports.sentMail = functions.firestore
.document("/Offices/{officeId}")
.onCreate((documentSnapshot,event) => {
const documentData = documentSnapshot.data();
const officeId = event.params.officeId;
const msg = {
to: "amarjeetkumars34@gmail.com",
from: "singhamarjeet045@gmail.com",
text: "hello from this side",
templateId: "d-8ecfa59aa9d2434eb8b7d47d58b4f2cf",
substitutionWrappers: ["{{", "}}"],
substitutions: {
name: documentData.name
},
custom_args: {
"officeId": officeId
}
};
return sgMail.send(msg)
.then(() => {
console.log("payment mail sent success"));
return null;
})
.catch(err => {
console.log(err)
return null;
});
});
请注意,您通过documentSnapshot.data()
获取新创建的文档(触发云功能的文档)的数据:您不需要在云功能中查询相同的文档。
然后,创建一个简单的HTTPS Cloud Function,如下:
exports.sendgridWebhook = functions.https.onRequest((req, res) => {
const body = req.body; //body is an array of JavaScript objects
const promises = [];
body.forEach(elem => {
const event = elem.event;
const eventTimestamp = elem.timestamp;
const officeId = elem.officeId;
const updateObj = {};
updateObj[event] = true;
updateObj[event + 'Timestamp'] = eventTimestamp;
promises.push(admin.firestore().collection('Offices').doc(officeId).update(updateObj));
});
return Promise.all(promises)
.then(() => {
return res.status(200).end();
})
})
部署它并获取它的 URL,如终端中所示:它应该像 https://us-central1-<your-project-id>.cloudfunctions.net/sendgridWebhook
.
注意这里我用的是admin.firestore().collection('Offices')...
。您可以使用 const db = newProject.firestore(); ... db.collection('Offices')...
另请注意,Sendgrid webhook 发送的 HTTPS POST 请求的主体包含一个 JavaScript 对象数组,因此我们将使用 Promise.all()
来处理这些不同的对象,即使用 officeId
不同的事件写入 Firestore 文档。
然后您需要在 Sendgrid 平台的“邮件 Settings/Event 通知”部分设置 Webhook,如 doc 中所述,如下所示。