每次将数据写入我的Google Cloud Firestore存储桶时,是否可以获取电子邮件或文本通知?

用户名

我有一个Google Cloud Bucket,Firebase在那里写了我的应用程序数据。我想监视我的数据,并通过文本或电子邮件将新的更新(写入)写入我的Firebase数据库。我目前在Nodejs上设置了Twilio以在Firebase上发送文本,我的代码是:

const functions = require('firebase-functions');
var twilio = require('twilio');
const admin = require('firebase-admin');
admin.initializeApp();
var accountSid = 'account id'; // Account SID from www.twilio.com/console
var authToken = 'account token';   // Auth Token from www.twilio.com/console

var client = new twilio(accountSid, authToken);

exports.useWildcard = functions.firestore
    .document('comments/{commentContent}')
    .onWrite((change, context) => {
      client.messages.create({
    body: context.params.commentContent,
    to: '+15555555555',  // Text this number
    from: '+15555555556' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));
    });

目前,我只想将其构建为注释文档,该文档是通过firefire内部组织的comments/{commentContent}后来,我想扩展到其他树木。但是,我不确定每次写入我的注释树时上述操作是否都会运行。是否需要firebase-admin我上面提到模块?谢谢!

索伯

是的,该onWrite方法不仅会在写入注释树时运行,而且还会因任何文档中的任何更改以及文档的删除而触发。这意味着现在您的代码将对上述任何情况做出相同的响应,这可能会导致问题,尤其是在文档被删除的情况下,因为它会尝试发送不存在的注释,并且可能会一些空异常。

说您有不同的解决方案。如果您只希望函数对新注释做出反应,而不对更新或删除做出反应,则应使用onCreateTrigger而不是onWrite

如果您还想处理评论更新通知,则可以同时使用onCreateonUpdate,但可以通过以下方式发送不同的消息:

exports.useWildcardCreate = functions.firestore
    .document('comments/{commentContent}')
    .onCreate((change, context) => {
      client.messages.create({
    body: context.params.commentContent,
    to: '+15555555555',  // Text this number
    from: '+15555555556' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));
    });

exports.useWildcardUpdate = functions.firestore
    .document('comments/{commentContent}')
    .onUpdate((change, context) => {

        const newComment = change.after.data();
        const previuosComment = change.before.data();

      client.messages.create({
    body: 'The comment ${previuosComment} has been changed to  ${newComment}',
    to: '+15555555555',  // Text this number
    from: '+15555555556' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));
    });

最后,如果您还需要通知何时删除评论,则应使用onWrite方法,但要区分3种不同情况,如下所示:

exports.useWildcard = functions.firestore
    .document('comments/{commentContent}')
    .onWrite((change, context) => {
    var textBody;
    const oldComment = change.before.data();
    const newComment = change.after.data();

    if (change.after.exists == false) { // comment has been deleted
        textBody = 'The comment ${oldComment} has been deleted';
    }
    else if (oldComment != newComment) { // comment has been updated
        textBody = 'The comment ${oldComment} has been changed to ${newComment}';
    }
    else { // if its not an update or a deletion its a new comment
        textBody = newComment;
    }
      client.messages.create({
    body: textBody,
    to: '+15555555555',  // Text this number
    from: '+15555555556' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));
    });

最后require('firebase-admin')是必需的,因为它将允许您从特权环境中与Firebase进行交互。在这里您可以找到Firebase Admin SDK的所有信息

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从Cloud Firestore获取数据

Firebase Functions将数据写入Cloud Firestore数据库时,写入次数是否计数?

是否可以将Firebase Cloud Firestore数据导出到Postgresql?

将CSV导入Google Cloud数据存储区

无法获取Google Cloud Storage存储桶的位置

我可以将礼品卡用于Google Cloud Platform吗?

创建新的Cloud Composer env时,是否可以将存储桶设置为预先存在的存储桶?

从数据存储区将数据迁移到Google Cloud Firestore

使用Cloud Functions将数据写入Firestore

允许Google Cloud Compute Engine实例将文件写入Google存储桶-Python

Google Cloud-将数据从存储桶下载到实例

是否可以将对Google Cloud Storage存储桶中的对象的访问权限授予非Google管理的电子邮件地址?

Firebase:当该电子邮件已经用于Google,Microsoft等帐户时,是否可以添加新的电子邮件/密码帐户?

电子邮件:[Firebase]对您的Cloud Firestore数据库的客户端访问权限将在X天内到期

Cloud Composer将文件写入其他存储桶问题

从Google Cloud Function(Python)将新文件写入Google Cloud Storage存储桶

将数据插入Google Cloud中的bigquery表时出错?

在Google Cloud Shell中,是否可以将存储桶转移到远程服务器?

是否可以在Firebase / Google Cloud Storage存储桶上设置文件夹或存储桶级别的“ cacheControl”设置?

我的Google Cloud Platform存储中存储了未知存储桶

是否可以将Google Cloud Platform中VM的内容备份到特定存储桶?

是否有程序可以在流程完成后向我发送通知电子邮件?

Google Cloud上用于转发电子邮件的Postfix是否需要某种身份验证?

创建存储桶后,是否可以更改Google Cloud Storage存储桶类?

每次写入错误时,我是否可以触发 LogAnalytics 发送包含错误本身的电子邮件?

从 Google Cloud Firestore 快速获取数据

将 Service Worker 中的数据写入 Cloud Firestore

Cloud Firestore - 如何阻止“任何用户都可以读取您的整个数据库”错误电子邮件

无法从 Google Cloud Storage 存储桶中获取 Flutter 的 NetworkImage