【问题标题】:Meteor: Get a list of SMS texts from Twilio and insert them into mongoDBMeteor:从 Twilio 获取 SMS 文本列表并将它们插入到 mongoDB
【发布时间】:2016-04-17 07:03:32
【问题描述】:

我正在尝试将 SMS 文本消息对话存储在我的应用中。现在,我可以通过 Twilio API 成功发送 SMS 消息。基本上,我有一组预先构建的消息,它们显示在一个表格中,每条消息都可以通过单击“Text it”按钮来发送。这很好用。

我在存储我的 Twilio 号码收到的 SMS 文本列表时遇到了问题(即响应我从应用程序发送的文本)。我能够从 Twilio 获取文本列表。

这是我拥有的提取文本列表的 Meteor 方法代码:

Meteor.methods({
  getTexts: function() {

    // Twilio Credentials
    var accountSid = 'someTwilioSid';
    var authToken = 'someAuthToken';
    var twilio = Twilio(accountSid, authToken);

    twilio.messages.list({}, function (err, data) {
      var texts = [];
      data.messages.forEach(function (message) {
        var text = {
          to: message.to,
          from: message.from,
          body: message.body
        };
        texts.push(text);
       //Texts.insert(text); // Uncommenting this causes a Meteor.bindEnvironment error
       console.log(text);
      });
    });
});

返回例如这个 JSON:

I20150314-15:12:08.823(-5)? { to: '+11234567891',
I20150314-15:12:08.823(-5)?   from: '+11234567890',
I20150314-15:12:08.823(-5)?   body: 'Hello, welcome to Twilio from the command line!' }
I20150314-15:12:08.823(-5)? { to: '+11234567891',
I20150314-15:12:08.823(-5)?   from: '+11234567890',
I20150314-15:12:08.823(-5)?   body: 'Hello, welcome to Twilio!' }

看起来不错,除非我取消注释 Texts.insert(text),我得到:

> Error: Meteor code must always run within a Fiber. Try wrapping callbacks that you pass to non-Meteor libraries with Meteor.bindEnvironment.

这是我尝试过的 getTexts 的替代版本,当我取消注释 Texts.insert(text) 行时,这两个版本都会让我出现 bindEnvironment 错误:

使用Meteor.wrapAsync

Meteor.wrapAsync(twilio.messages.list({}, function (err, data) {
  var texts = [];
  data.messages.forEach(function (message) {
    var text = {
      to: message.to,
      from: message.from,
      body: message.body
    };
    texts.push(text);
   //Texts.insert(text);
   console.log(text);
  });
}));

使用Meteor.bindEnvironemnt

   Meteor.bindEnvironment(twilio.messages.list({}, function (err, data) {
      var texts = [];
      data.messages.forEach(function (message) {
        var text = {
          to: message.to,
          from: message.from,
          body: message.body
        };
        texts.push(text);
       //Texts.insert(text);
       console.log(text);
      });
    }));

我已阅读 Meteor: Proper use of Meteor.wrapAsync on server,这绝对有助于为我指明正确的方向,但我仍然遇到问题。

【问题讨论】:

    标签: javascript mongodb meteor twilio


    【解决方案1】:

    好的,在仔细研究Meteor: Proper use of Meteor.wrapAsync on server之后,我想出了下面的解决方案,效果很好。

    Meteor.methods({
      getTexts: function() {
        // Twilio Credentials
        var accountSid = 'someTwilioSid';
        var authToken = 'someAuthToken';
        var twilio = Twilio(accountSid, authToken);
    
        var twilioMessagesListSync = Meteor.wrapAsync(twilio.messages.list, twilio.messages);
    
        var result = twilioMessagesListSync(
          function (err, data) {
            var texts = [];
            data.messages.forEach(function (message) {
              var text = {
                to: message.to,
                from: message.from,
                body: message.body,
                dateSent: message.date_sent,
                status: message.status,
                direction: message.direction
              };
              texts.push(text);
              Texts.insert(text);
            })
          }
        );
      }
    });
    

    希望这对其他人有所帮助。感谢 saimeunt 出色的 answer,它非常容易(最终)移植到 Twilio 应用程序。

    【讨论】:

      【解决方案2】:

      这是我的解决方案。它与 benvenker 非常相似,但包含额外的代码行以防止重复数据填充我的本地 mongo 数据库。

      SMS = new Mongo.Collection('sms');
      
      // Configure the Twilio client
      var twilioClient = new Twilio({
        from: Meteor.settings.TWILIO.FROM,
        sid: Meteor.settings.TWILIO.SID,
        token: Meteor.settings.TWILIO.TOKEN
      });
      var getTwilioMessages = Meteor.wrapAsync(twilioClient.client.messages.list, twilioClient.client.messages);
      
      function updateMessages() {
        getTwilioMessages(function(err, data) {
          if (err) {
            console.warn("There was an error getting data from twilio", err);
            return
          }
          data.messages.forEach(function(message) {
            if (SMS.find({
              sid: message.sid
            }).count() > 0) {
              return;
            }
            SMS.insert(message);
          });
        });
      }
      
      updateMessages();
      Meteor.setInterval(updateMessages, 60000);
      

      如果没有以下非常有用的博文,我无法弄清楚这一点:

      http://blog.jakegaylor.com/2015/11/26/hello-twilio-meteor-here/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-26
        • 1970-01-01
        • 1970-01-01
        • 2017-05-24
        • 1970-01-01
        • 1970-01-01
        • 2021-11-23
        相关资源
        最近更新 更多