SENDING BULK SMS USING APPSSCRIPT AND FAST2SMS
Dear all, In following example we are sending sms through FAST2SMS API, so we all need to follow all fast2sms terms of service and condition.
Note:-To send bulk sms using fast2sms api, we have first made a minimum topup of 100 Rs. Then we can make try to send bulk sms.
Now, we move to https://script.google.com/home and create a project and connect it to the fast2sms API to send bulk sms
Numbers can be taken from google spreadsheet if your data on it. It also cab be send by
google automatin flows. In this condition data can be maped by criteria and send if criteria matched in given data. Below see how the spread sheet looks.
Note: In above sheet I have used data as example. You have to create a sheet as above if you want to get data from it else can arry contacts seprated by comma.
- In above sheet Column A contains Id of user.
- Column B contains Name of user.
- Column C contains Age of user.
- Column D contains Village of user.
- Column E contains Contact of user.
- Column F contains Balance of user.
- Column G contains Status of balance of column F.
- Highlited area contains spreadsheet id. Copy this and paste it in code.
- Remane "bulk" with your sheet name.
Copy and paste following code in appsscript code.gs file.
code.gs
//below code launched the index page in apps script project.
function doGet(e) {
return HtmlService
.createHtmlOutputFromFile('index.html')
.setTitle("SEND BULK SMSes | FAST2SMS API | JAVASCRIPT | APPS SCRIPT");
}
//Below code try to send sms to given numbers( numbers seprated by comma).
function sendbulk(obj){
var ss = SpreadsheetApp.openById("PASTE YOUR SHEET ID");
var sheet = ss.getSheetByName("bulk");// RENAME AS YOUR SHEET NAME
var tr = sheet.getDataRange().getValues();
for(var i=0; i<tr.length ; i++){
var vid = tr [ i ] [ 6 ];
if(vid == obj){
var contacts =tr[i][4];
var name = tr[i][1];
var balance = tr[i][5];
var smsb ="Dear "+name+", Your total due balance is"+balance;
var apikey = "Paste FAST2SMS API" ;
var url = "https://www.fast2sms.com/dev/bulkV2?authorization="
+apikey+"&sender_id=TXTIND&message="+smsb+"&route=v3&numbers="+contacts+"";
var options = {
"async": true,
"crossDomain": true,
"method": "GET"
}
}
}
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText())
return data.message;
}
Create a index.html file and paste the following code in it.
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top" />
<h2>SEND BULK SMS | FAST2SMS API | JAVASCRIPT | APPS SCRIPT</h2>
<style>
select,input{
width:15%;
border:1px dashed black;
margin:1px;}
input:hover{
background:yellow;}
</style>
</head>
<body align="center">
<div id="result"></div><br>
Send To:<br>
<select id="selv">
<option>select</option>
<option value="Due">Due</option>
<option value="Advance">Advance</option>
</select><br>
<input type="submit" value="SEND TO All" onclick="sendSMS()"/>
<script>
function sendSMS () {
let selected = document.querySelector('#selv').value;
let updateLocation = document.querySelector('#result');
function onFailure(error){
let warning = `<span style="color:red">${error}</span>`;
updateLocation.innerHTML = warning;
};
function onSuccess (response){
let result ="<span style='color:green'>"+response+"</span>";
updateLocation.innerHTML = result;
};
google.script.run.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
.submitDT(selected);
};
</script>
</body>
</html>
Note: If you run without Fast2SMS API, following result appears.