Send sms to phone number using GUPSHUP and nodejs

957 Views Asked by At

I need help in sending sms to user using nodejs and Gupshup.io . I'm facing difficulty understanding their documentation.

1

There are 1 best solutions below

0
María Lomeli On

Khurram Shahzad

I work as a chatbot developer for Gupshup, please let me know what you don't understand.

Either way, here I'll share the snipet code to send an sms in node.js with our API.

const { default: axios } = require('axios');
/*
Variables to replace
YOUR_API_KEY => The unique identifier for a Gupshup account.
YOUR_APP_ID=> The unique identifier for an app. An app's appid can be found in the cURL request generated on the dashboard.
*/
const sendMessage = (msg, phone, callbackFunc) => {
    const params = new URLSearchParams()
    params.append('destination', phone);
    params.append('message', msg);
    params.append('source', 'GSDSMS'); //Required only for india

    const config = {
        headers: {
            'apikey': YOUR_API_KEY,
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    }

    axios.post('http://api.gupshup.io/sms/v1/message/:YOUR_APP_ID', params, config)
        .then((result) => {
            console.log('Message sent');
            callbackFunc(result.data);
        })
        .catch((err) => {
            console.log(err);
            callbackFunc(err);
        })
}

module.exports = {
    sendMessage
}