我正在尝试在我的应用程序中存储短信对话。现在,我可以通过Twilio API成功发送SMS消息。基本上,我有一个预先构建的消息集合,这些消息显示在表格中,每个消息都可以通过单击"文本"按钮发送。这很好用。
我在存储我的 Twilio 号码收到的短信文本列表时遇到问题(即响应我从应用程序发送的文本)。我能够从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:在服务器上正确使用Meteor.wrapAsync,这绝对有助于为我指明正确的方向,但我仍然遇到麻烦。
好的,在仔细研究了Meteor:在服务器上正确使用Meteor.wrapAsync之后,我想出了下面的解决方案,它运行良好。
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 提供的出色答案,该答案很容易(最终)移植到 Twilio 应用程序中。
这是我的解决方案。它与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/