Inline keyboard callback_query setup in Google Apps Script Telegram bot - javascript

I've managed to figure out how to attach an inline button to message from my Telegram bot. I'm not sure how to make this button actually do something.
I have a simple invoice table in my Google Spreadsheet. I manually connected a Form to it, through which managers can request some financial transactions.
My perfect scenario: on every new form submit bot sends me a message with all the transaction's data. It already works perfectly (I'm using variables from form responses in combination with sendMessage and UrlFetchApp.fetch).
Now, I want to attach a functional button that will delete the particular message it's connected to and then send me a "Transaction completed" message.
My inline keyboard code excerpt:
'{"inline_keyboard":[[{"text":"Del","callback_data":"delete_message"}]]}'
I use most primitive webhook:
function setWebhook() {
var url = telegramUrl + "/setWebhook?url=" + webAppUrl;
var response = UrlFetchApp.fetch(url);
}
And I have a doPost function for just replying to /start command (if some manager got lost in space and time):
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = contents.message.from.id;
var fname = contents.message.from.first_name;
var text = (""+fname +", this bot is for notification only, fill the form located on our website.");
sendMessage(chat_id,text)
}
I've Googled a lot, and tried some solutions, but it seems like my callback listening still isn't working.

Related

Is there any way to mail merge with my own domain name instead Gmail

Here what I want to do is :
Send an email by mail merge by writing a script in App Script. (Success)
I have configured 2 domain email addresses in my Gmail along with my basic Gmail address.
I want to send an email via xyz#domainname.com instead of xyz#gmail.com, the Google Sheets owner.
here what I have done to send an email via Gmail
var first = 0;
var last = 1;
var email = 2;
var emailTemp = HtmlService.createTemplateFromFile("email");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("NAJ");
var data = ws.getRange("A2:C" + ws.getLastRow()).getValues();
data.forEach(function(row){
emailTemp.fn = row[first];
emailTemp.ln = row[last];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(row[email], "Important Test", "Your email does not support HTML.",
{name: "Email App", htmlBody: htmlMessage}
);
});
}
The only thing I need is to change the sending email address.
Any help would be highly appreciated. pardon my bad English
Explanation:
Your goal is to send emails from a specific email account regardless of the email account that is currently executing the script.
Approach 1:
One way to approach that is to use an installable trigger. As per the documentation:
Installable triggers always run under the account of the person who
created them. For example, if you create an installable open trigger,
it runs when your colleague opens the document (if your colleague has
edit access), but it runs as your account. This means that if you
create a trigger to send an email when a document is opened, the email
is always be sent from your account, not necessarily the account that
opened the document.
The idea here is to create an installable onEdit trigger from xyz#domainname.com so whenever the other emails edit a particular cell, the emails will be send.
The limitation of this approach is that we need to make sure that we trigger the code when we really want to and not every time a cell is edited. We can create a checkbox, and upon clicking on the checkbox (it does not matter which account did that) then the script will be executed and the emails will sent by the email account who created the installable trigger.
Approach 2:
Create email aliases. Here is the official documentation on how to do that and also this thread might be useful to create them programmatically. The restriction here is that to add an email alias, you must be a Google Workspace administrator.
Solutions:
Solution 1:
Create a checkbox in cell D1 of the sheet NAJ. The cell is up to you but you need to adjust the script accordingly.
Change the name of the function to add the event object. This will allow us to get edit info such as which cell is edited.
The new function will be:
function myInstallableOnEditFunction(e) {
const arng = e.range;
if(arng.getSheet().getName()=="NAJ" && arng.getA1Notation()=='D1' && arng.getValue()==true){
var first = 0;
var last = 1;
var email = 2;
var emailTemp = HtmlService.createTemplateFromFile("email");
var ws = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("NAJ");
var data = ws.getRange("A2:C" + ws.getLastRow()).getValues();
data.forEach(function(row){
emailTemp.fn = row[first];
emailTemp.ln = row[last];
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(row[email], "Important Test", "Your email does not support HTML.",
{name: "Email App", htmlBody: htmlMessage}
);
});
}
}
Finally, create an installable onEdit trigger function from the xyz#domainname.com account. You can do that either manually from the current project's trigger menu or programmatically.
After that, upon clicking on cell D1 by any account, the emails will be sent by xyz#domainname.com.
Solution 2:
After you have created email aliases (see Approach 2), you can use the getAliases() method, here is a sample script. I haven't tested on my own, but you can explore also this possibility.
// Log the aliases for this Gmail account and send an email as the first one.
var me = Session.getActiveUser().getEmail();
var aliases = GmailApp.getAliases();
Logger.log(aliases);
if (aliases.length > 0) {
GmailApp.sendEmail(me, 'From an alias', 'A message from an alias!', {'from': aliases[0]});
} else {
GmailApp.sendEmail(me, 'No aliases found', 'You have no aliases.');
}

How to allow JavaScript to receive a message sent to a specific user (or self) using Signal R?

I previously asked this question but it was closed for duplication owing to this thread (SignalR - Sending a message to a specific user using (IUserIdProvider) *NEW 2.0.0*) - but this doesn't show the JavaScript as made clear in my title.
I have a WebForm application in ASP.Net that uses SignalR to push live data to the user logged in. The setup works perfectly, but realised I am broadcasting messages to all clients, which whilst it doesn't cause the wrong data to displayed to the logged in user, does cause the JavaScript function to get called for all users when just one has a data push.
I have amended the Hub code to broadcast to a specific user (User) and provided the User ID, and I have also tried Client with a Connection ID. Both fire off fine in the codebehind, but the javascript will not update the front end.
I believe it's because the JavaScript has not been modified to listen for a message sent to the user, but I'm not sure how I need to adapt the code to allow the message to be received.
The 2 tried lines in Hub here:
context.Clients.User(Me.Context.User.Identity.GetUserId()).ReceiveNotification(notifCount)
context.Clients.Client(Me.Context.ConnectionId).ReceiveNotification(notifCount)
JavaScript/jQuery function for the SignalR message here:
$(function () {
var nf = $.connection.notificationHub;
nf.client.receiveNotification = function (notifCount) {
// Update data
}
$.connection.hub.start().done(function () {
nf.server.sendNotifications();
}).fail(function (e) {
alert(e);
});
//$.connection.hub.start();
});
For calling back to the client (or self) you should use:
Clients.Caller.addContosoChatMessageToPage(name, message);
And for calling users you should use:
Clients.Client(Context.ConnectionId).addContosoChatMessageToPage(name, message);
Reference - docs

Lotus notes mail using java script from HTML

I am developing an application to send mail to a group of user using java script from Lotus notes. we are using shared mailbox for a user.
When i trigger this mail script it is sending from my personal mailbox
Is it possible to trigger the mail from the shared mailbox?
<script>
function sendEmail()
{
var notesDatabase;
var notesSessiona;
notesSessiona = new ActiveXObject("Notes.NotesSession");
notesDatabase = notesSessiona.GETDATABASE("staralliancesupport", "");
notesDatabase.OPENMAIL();
var mailItem = notesDatabase.CreateDocument();
mailItem.subject = te.value +" Outage"+" "+text11.value+tex111.value+tex11.value+tex21.value+tex31.value+tex41.value+tex51.value+tex61.value+tex71.value+tex81.value+tex91.value+" "+ text.value+" "+ tex.value+" session down"
mailItem.sendto = "nathan.sabari#tcs.com";
mailItem.copyto = textbox_1.value;
mailItem.BlindCopyTo = "";
mailItem.Body = "Dear All,\n\nNote: This e-mail is sent to Star Alliance member carrier contacts, their business partners and provider help desks.\n\nStar Alliance would like to inform you about the "+ tex.value +" interruption between Star Alliance and-"+" "+te.value+" "+text11.value+tex111.value+tex11.value+tex21.value+tex31.value+tex41.value+tex51.value+tex61.value+tex71.value+tex81.value+tex91.value+" "+ text.value+".\n\nWe are liaising with the airline's service desk to get more information regarding this issue.\n\n--\n\nStar Alliance Support\nEmail support at: Staralliance.support#tcs.com\nTelephone contact No: +1877 292 9784\n"
mailItem.Send (0);
}
</script>
Changing the database will not change the sender. The server always puts the current username in the sender name. You need to write your message directly to the mail.box file instead of using the NotesDocument.Send() method.
See Knut's answer to an earlier question about this. It contains a link to a script by Karl-Henry Martinsson that demonstrates the technique. It's LoutsScript, though, so you're going to have to translate that script to JavaScript.
For your specific problem, the most important lines of code in Karl-Henry's LotusScript code are:
Set mailbox = New NotesDatabase(mailservername,"mail.box")
If mailbox.Isopen = False Then
Print "mail.box on " & mailservername & " could not be opened"
Exit Sub
End If
Set me.maildoc = New NotesDocument(mailbox)
and...
If me.p_principal<>"" Then
Call maildoc.ReplaceItemValue("Principal", me.p_principal)
' If principal is set, we want to fix so mail looks like
' it is coming from that address, need to set these fields
Call maildoc.ReplaceItemValue("From", me.p_principal)
Call maildoc.ReplaceItemValue("Sender", me.p_principal)
Call maildoc.ReplaceItemValue("ReplyTo", me.p_principal)
Call maildoc.ReplaceItemValue("SMTPOriginator", me.p_principal)
End If
where me.p_principal contains the address he wants the message to come from, and...
Call maildoc.Save(True,False) ' Save in mail.box
Note that he doesn't call maildoc.Send() when he wants to control the From address. He just calls maildoc.Save(), and this works because he is saving it in the mail.box file.

FormApp.openById not working when i'm not collaborator

I'm creating a script that reads specific emails and retrieves data from them to create responses on a google form.
It is working as a charm when I have a collaborator access to the form, but it throw me this error message when I'm not...
"No item with the given ID could be found, or you do not have permission to access it"
var logEnvironment = "TEST"; // TEST for Testing environment, PRODUCTION for production
// Get Ids for the Google Form and Response Spreadsheet
if (logEnvironment == "TEST") {
var premediaFormId = <MY TEST FORM ID>
var responseWorkbookId = *******;
} else {
var premediaFormId = <MY PRODUCTION FORM ID>;
var responseWorkbookId = *******;
}
// Google Form
var premediaForm = FormApp.openById(premediaFormId);
var premediaFormItems = premediaForm.getItems();
On the production form, I can't give access to everybody running the script as collaborators.. they should have only regular access to create responses.. but we don't want the users to be entering the responses manually if we already have an email from a system with all the information.
Integrating the system with the form is out of the question.
Is there a way to resolve this access issue....

On page unload strophe.js sends request twice! why?

I'm new to Xmpp server and strophe.js, I implement it into my website for messaging. There is a problem when I am send a message it works fine, but after sending a message if I reload the page immediately then I saw that that message, which I send before reload saved in Xmpp server twice, and when I retrieve message from server obviously it return duplicate message. It is so embarrassing.
Can any one tell me any solutions to stop twice requesting when page load?
I saw a post where one developer told that he face with same problem and contact to strophe team they suggest him to replace the line 2566 of strophe.js file with this code
if (req.sends == 0) {
sendFunc();
}
I have already done it but my problem is still there. :(
Thanks,
biswajitGhosh
You need to create multiple divs/windows, one for each chat. Strophe is meant to be asynchronous so you don't have to reload. Reloading defeats the purpose of strophe. Add a handler to your connection once you attach/connect.
connection.addHandler(onMsg, null, 'message');
In your message handler, you can then determine which conversation the message belongs to and append it to the appropriate div.
var onMsg = function (msg) {
var elems = msg.getElementsByTagName('body');
if (elems.length > 0) {
var type = msg.getAttribute('type');
var from = msg.getAttribute('from');
if (type == "chat") {
//new chat message
}
}
return true;
}

Categories