每次将数据写入我的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条评论
登录后参与评论

相关文章

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

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

来自分类Dev

Google Cloud Run /挂载Google存储桶

来自分类Dev

将检查点存储到 Google Cloud 存储桶时出错

来自分类Dev

Google Cloud Firestore触发器未检测到写入数据库

来自分类Dev

我的Google Cloud Storage存储桶中的文件在哪里?

来自分类Dev

我可以从Google Cloud Storage项目存储桶中删除容器映像吗?

来自分类Dev

每当将0 kb文件上传到s3时,是否可以通过SNS或SQS发送电子邮件通知?

来自分类Dev

是否可以授予服务器读/写权限,但不能授予对Google Cloud Storage存储桶的删除权限?

来自分类Dev

是否可以授予服务器读/写权限,但不能授予对Google Cloud Storage存储桶的删除权限?

来自分类Dev

在将行插入Google表格时获取电子邮件

来自分类Dev

是否可以获取 Firebase Cloud Firestore 数据库中可用集合的文档 ID?

来自分类Dev

使用MailKit c#时,是否可以将电子邮件另存为文件?

来自分类Dev

使用MailKit c#时,是否可以将电子邮件另存为文件?

来自分类Dev

Google Cloud Firestore Functions 是否会在其他 Cloud Functions 的 Firestore 交互时触发?

来自分类Dev

如何将Facebook登录名和电子邮件注册都添加到我的Google Cloud Endpoints App(Java)?

来自分类Dev

我如何才能断开长文本的行?从Cloud Firestore获取长文本数据后

来自分类Dev

每次我从 Firebase Cloud Messaging 发送通知时,我的应用都崩溃了

Related 相关文章

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

    Google Cloud Run /挂载Google存储桶

  15. 15

    将检查点存储到 Google Cloud 存储桶时出错

  16. 16

    Google Cloud Firestore触发器未检测到写入数据库

  17. 17

    我的Google Cloud Storage存储桶中的文件在哪里?

  18. 18

    我可以从Google Cloud Storage项目存储桶中删除容器映像吗?

  19. 19

    每当将0 kb文件上传到s3时,是否可以通过SNS或SQS发送电子邮件通知?

  20. 20

    是否可以授予服务器读/写权限,但不能授予对Google Cloud Storage存储桶的删除权限?

  21. 21

    是否可以授予服务器读/写权限,但不能授予对Google Cloud Storage存储桶的删除权限?

  22. 22

    在将行插入Google表格时获取电子邮件

  23. 23

    是否可以获取 Firebase Cloud Firestore 数据库中可用集合的文档 ID?

  24. 24

    使用MailKit c#时,是否可以将电子邮件另存为文件?

  25. 25

    使用MailKit c#时,是否可以将电子邮件另存为文件?

  26. 26

    Google Cloud Firestore Functions 是否会在其他 Cloud Functions 的 Firestore 交互时触发?

  27. 27

    如何将Facebook登录名和电子邮件注册都添加到我的Google Cloud Endpoints App(Java)?

  28. 28

    我如何才能断开长文本的行?从Cloud Firestore获取长文本数据后

  29. 29

    每次我从 Firebase Cloud Messaging 发送通知时,我的应用都崩溃了

热门标签

归档