Created
January 30, 2020 15:20
-
-
Save AlexLakatos/b718f49a09843c23a7c90dc4a0347126 to your computer and use it in GitHub Desktop.
Azure Send Receive SMS Function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = async function(context, req) { | |
const Nexmo = require("nexmo"); | |
const nexmo = new Nexmo({ | |
apiKey: process.env["NEXMO_API_KEY"], | |
apiSecret: process.env["NEXMO_API_SECRET"] | |
}); | |
const params = Object.assign(req.query, req.body); | |
if (params.text) { | |
var response = []; | |
// transform inbound SMS into emojis | |
for (let i = 0; i < params.text.length; i++) { | |
const emoji = String.fromCodePoint(127715 + params.text.charCodeAt(i)); | |
response.push(emoji); | |
} | |
// send SMS back with emojis | |
nexmo.message.sendSms( | |
params.to, | |
params.msisdn, | |
response.join(""), | |
{ | |
type: "unicode" | |
}, | |
(err, responseData) => { | |
if (err) { | |
context.log(err); | |
} else { | |
if (responseData.messages[0]["status"] === "0") { | |
context.log("Message sent successfully."); | |
} else { | |
context.log( | |
`Message failed with error: ${responseData.messages[0]["error-text"]}` | |
); | |
} | |
} | |
} | |
); | |
} | |
context.res = {}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment