I have a HTML form (which is filled in and the data from it goes to google tables and telegram chat). Now I added a button to upload files(<input type="file").
I need a person who uses this HTML form can upload a file from the phone to the form and send it.
The file should only be sent to telegram. The form already sends other data, but I need to attach the file too :)
The function responsible for sending to Telegram chat:
function sendtoTelegram(formData) {
//Send data to Telegram chat
var formattedValues = [
formData.date,
"",
formData.accountTo,
"",
formData.accountFrom,
formData.amount,
formData.note,
"",
formData.object
];
var nakakoiChet = formattedValues[2];
var sKakogoChet = formattedValues[4];
var message = "Date: " + formattedValues[0] + "\n" +
"To which account: " + nakakoiChet + "\n" +
"From which account:" + sKakogoCheta + "\n" +
"Amount: " + formattedValues[5] + "\n" +
"Note: " + formattedValues[6] + "\n" +
"Object: " + formattedValues[8] + "\n" +
"Username: " + Session.getActiveUser().getEmail();
function getId(text) {
var regex = /id-(\d+)/;
var match = regex.exec(text);
if (match) {
return "-" + match[1];
}
return null;
}
var chatIds = [getId(nakakoiChet), getId(sKakogoCheta)].filter(Boolean);
if (chatIds.length === 0) {
chatIds.push("-1001000000"); // if no id came out, all message collect on this chat
}
var sendText = encodeURIComponent(message);
var opts = { "muteHttpExceptions": true };
for (var i = 0; i < chatIds.length; i++) {
var url = apiUrl + "/sendmessage?parse_mode=HTML&chat_id=" + chatIds[i] + "&text=" + sendText;
UrlFetchApp.fetch(url, opts).getContentText();
}
}```
My html code:
<button type="button" id="btn" class="btn btn-outline-success btn-color" onclick="sendDataWithDelay()">Send to telegram and table</button>
<input type="file" id="myFileInput">
<input type="button" value="Upload and send" onclick="uploadAndSend()">
<script>
function sendDataWithDelay() { // Sending data to Telegram and sending data to a table
var date = document.getElementById('date3').value;
var theBillselect = document.getElementById("selectbill3");
var theBilloption = theBillselect.options[theBillselect.selectedIndex].textContent;
var theBillselect1 = document.getElementById("selectbill4");
var theBilloption1 = theBillselect1.options[theBillselect1.selectedIndex].textContent;
var theSum = document.getElementById('theSum4').value;
var note = document.getElementById('note3').value;
var theobjectselect2 = document.getElementById("selectobject1");
var theobjectoption2 = theobjectselect2.options[theobjectselect2.selectedIndex].textContent;
var formData = {
date: date,
accountTo: theBilloption,
accountFrom: theBilloption1,
amount: theSum,
note: note,
object: theobjectoption2
};
google.script.run.sendtoTelegram(formData);
sendData();
}
</script>
I tried to get the files through doPost, but nothing worked and the file send button didn't work.
I expect that the file will be sent with the data in the function sendtoTelegram and it is desirable that in html after attaching the file sending data form and file was through one button My HTML form