this tutorial to make MPWA autoreply using google sheet plus apps script.
1. make new sheet file using this format (1st row) Keyword, Type, Message, File
you can visit:
https://docs.google.com/spreadsheets/d/1svHPPF3hFyaPfkRxzN1wscR5A5bS21MzcjrH3NO51us/edit?usp=sharing
2. then go to Extensions - Apps Script
3. delete all data on there. copy and paste code bellow. and edit with your own data.
function doPost(e) {
var data = e.postData.contents;
var json = JSON.parse(data);
var number = json['from'];
var senderMessage = json['message'];
var file = json['bufferImage'];
var pesan = senderMessage.toLowerCase();
var newMessage = pesan.split(" ");
var searchreply = autoreply(newMessage[0]);
var type = searchreply[1];
var messagetosend = searchreply[2];
var file = searchreply[3];
if(type == "file"){
sendWaFile(number, messagetosend, file);
} else if(type == "button") {
sendWaButton(number, messagetosend, file);
} else if(type == "list") {
sendWaList(number, messagetosend, file);
} else {
sendWaText(number, messagetosend);
}
}
function autoreply(message){
var sheetUrl = "https://docs.google.com/spreadsheets/d/1svHPPF3hFyaPfkRxzN1wscR5A5bS2jsdhjsdhuheheh/edit?usp=sharing"; //ganti dengan URL sheet kamu
var sheetfile = SpreadsheetApp.openByUrl(sheetUrl);
var sheet = sheetfile.getSheetByName("autoreply"); //sesuaikan dengan nama sheet yg d buat
var data = sheet.getDataRange().getValues()
var filterData = data.filter(function(keyword){
if(keyword[0] == message){
return keyword
}
})
if(filterData.length>0){
return filterData[0]
}
}
function sendWaText(number, messagetosend) {
var APIKey = "Hcu8aDaGnxI6Utf9kWjQsjlpX5taFy" //ganti dengan API
var sender = "628xxxxxxxxxx" // ganti nomer HP
var url = "https://websitekamu/send-message"; //ganti website
var msgtext = {
'api_key': APIKey,
'sender': sender,
"number": number,
'message': messagetosend,
};
var response = UrlFetchApp.fetch(url, {
"method": "POST",
"contentType": "application/json",
"payload" : JSON.stringify(msgtext),
});
Logger.log(response.getContentText());
}
//untuk kirim file
function sendWaFile(number, messagetosend, file){
var APIKey = "Hcu8aDaGnxI6Utf9kWjQsjlpX5taFy" //ganti api
var sender = "628000000000" // ganti nomer HP
var url = "https://websitekamu/send-media"; //ganti website
var msggmbr = {
"api_key": APIKey,
"sender": sender,
"number": number,
"media_type" : "image",
"caption": messagetosend,
"url": file
}
var response = UrlFetchApp.fetch(url, {
"method": "POST",
"contentType": "application/json",
"payload" : JSON.stringify(msggmbr),
});
Logger.log(response.getContentText());
}
//fungsi button tidak bisa
//http://yourhost.com/send-button?sender=nomersender&api_key=yourapikey&number=nomertujuan&url=&footer=optional&message=Halo,ini pesan button&button=button 1,button 2,button 3
function sendWaButton(number, messagetosend, file){
//var APIKey = "blablabla"
//var sender = "blablabla"
var url = "https://alamatwebsite/send-button";
var msgbut = {
"sender" : sender,
"api_key" : APIKey,
"number" : number,
"url" : null,
"footer" : "optional",
"message" : messagetosend,
"button" : file
}
var response = UrlFetchApp.fetch(url, {
"method": "POST",
"contentType": "application/json",
"payload" : JSON.stringify(msgbut),
});
Logger.log(response.getContentText());
}
4. in left menus, choose Triggers
5. Click Add Trigger button in right bottom corner.
6. make setting like picture bellow. click SAVE.
7. Click DEPLOY in right top corner.
8. Select type: WEB APP. and make setting like picture. Click DEPLOY to finish it.
9. copy url and paste in your MPWA webhook.
@Enno The Explorer @JangE thanks to @bloserda who teach me about this script.