如何使用nodejs以区域语言发送短信?

时间:2017-03-22 05:03:21

标签: javascript node.js

我正在使用textlocal.in api发送批量短信,当我用区域语言键入消息时显示错误

{"errors":[{"code":204,"message":"Invalid message content"}],"status":"failure"}

有没有办法发送区域语言请帮帮我?

var http = require('http');
var urlencode = require('urlencode'); 

var msg = 'ಅನುವಾದಿಸಿ';
var toNumber = '9731750371';
var username = 'dinesh@cantern.in';
var hash = '171f2176f6a24f1d219028011d5bff7b9eac1a3ff91873de62af429a736da26'; // The hash key could be found under Help->All Documentation->Your hash key. Alternatively you can use your Textlocal password in plain text.
var sender = 'txtlcl';
var data = 'username=' + username + '&hash=' + hash + '&sender=' + sender + '&numbers=' + toNumber + '&message=' + msg;
var options = {
  host: 'api.textlocal.in', path: '/send?' + data
};
callback = function (response) {
  var str = '';//another chunk of data has been recieved, so append it to `str`
  response.on('data', function (chunk) {
    str += chunk;
  });//the whole response has been recieved, so we just print it out here
  response.on('end', function () {
    console.log(str);
  });
}//console.log('hello js'))
http.request(options, callback).end();

1 个答案:

答案 0 :(得分:1)

首先安装

npm install i18n-nodejs --save

下载语言环境语言json文件并使用此代码

var config = {
"lang": "ar",
"langFile": "./../../locale.json"//relative path to index.js file  
}
var i18n = require('i18n-nodejs')(config.lang, config.langFile);

其中" ar"是指阿拉伯语

并将字符集设置为UTF-8,如下所示

response.header("Content-Type", "html/text; charset=utf-8");