gpt4 book ai didi

node.js - 作为 promise 对 sinch 进行 api 调用

转载 作者:太空宇宙 更新时间:2023-11-04 02:15:33 24 4
gpt4 key购买 nike

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

    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 - 作为 promise 对 sinch 进行 api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35949289/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com