【问题标题】:api call to sinch as promiseapi调用sinch作为promise
【发布时间】:2016-03-16 04:03:59
【问题描述】:

我已经测试了界面并进行了一些更改,以便在后端与我的sailsjs(版本 11)一起使用。在这种情况下,我使用了一个风帆服务,一切看起来都很好,但我总是回到一个待处理的状态。 ```

    SinchService.js
var sinchAuth = require('sinch-auth');
var request = require('request');

var sinchMessaging = {};
var Promise = require('bluebird');
//sinchMessaging.sendMessage = function (phoneNumber, message) {
//exports.sendMessage = function (phoneNumber, message) {
module.exports = {
sendMessage: function (phoneNumber, message) {
var auth = sinchAuth();
if (!auth) {
    throw new Error("No Authorization was provided");
}
var options = {
    method: 'POST',
    url: "https://messagingApi.sinch.com/v1/sms/" + phoneNumber,
    headers: {
        "Content-Type": "application/json",
        "Authorization": auth
    },
    body: "{\"Message\":\"" + message + "\"}"
    //  body: {"Message" : message }
};

return new Promise(function (resolve, reject) {
    request(options, function (error, response, body) {

        sails.log("Finished with call");

        if (error) {
            sails.log(error);
            throw error;
        }
        else {
            sails.log("Finished with body ", body);//.MessageId

            return resolve(response.body);
        }

    });

})
},


getStatus: function (messageId) {
var auth = sinchAuth();
if (!auth) {
    throw new Error("No Authorization was provided");
}
var options = {
    method: 'GET',
    url: "https://messagingApi.sinch.com/v1/sms/" + messageId,
    headers: {
        "Content-Type": "application/json",
        "Authorization": auth
    }
};
return new Promise(function (resolve, reject) {
    request(options, function (error, response, body) {

        sails.log("Finished with call");

        if (error) {
            sails.log(error);
            throw error;
        }
        else {
            return resolve(response.body);
        }

    });

})

}
};

```

    agendaService.js
var jsonObjS;

SinchService.sendMessage(phoneNumber, message).then(function (results) {
var jsonObj = JSON.parse(results);
console.log('results sendMessage ', jsonObj.messageId);
if (jsonObj.messageId!==undefined){
SinchService.getStatus(jsonObj.messageId).then(function (results_s) {
    jsonObjS = JSON.parse(results_s);
    console.log('results getStatusS ', jsonObjS.status);

SinchService.getStatus(jsonObjS.messageId).then(function (results_s) {
var jsonObjS = JSON.parse(results_s);
console.log('results getStatusS ', jsonObjS.status);
});
});

```

【问题讨论】:

    标签: node.js sails.js sinch


    【解决方案1】:

    Pending 永远是第一个状态,过一段时间再查询看看消息的状态。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多