I'm new to this and trying to figure out the basics. I want to use google apps script to receive requests from slack and send back information from a spreadsheet.
I am stuck at this step. EventsAPI - URL Verification. My approach was to have a doPost() function that returns the challenge. After I was able to confirm slack could connect to the app I would be able to build it out to send the required information from the spreadsheet.
function doPost(e){
return ContentService.createTextOutput(JSON.parse(e.postData.contents).challenge);
}
I expect the challenge to be successfully returned and the URL verified.
The error I receive in Slack is Request URL Your URL didn't respond with the value of the challenge parameter.
Related
I'm currently using Apps Script to build a webpage. Is there a way to get the parameters in the URL without using doGet(e)? i.e. Can I use a getParams() function to retrieve the URL?
Answer
No, it is not possible
How Web-Apps work
When a user visits an app or a program sends the app an HTTP GET request, Apps Script runs the function doGet(e). When a program sends the app an HTTP POST request, Apps Script runs doPost(e) instead. In both cases, the e argument represents an event parameter that can contain information about any request parameters.
Ask for a feature request
Google has a tool called Issue Tracker that tracks bugs and feature requests during product development. You can open a new feature request there.
I'm attempting to make a post request from an Airtable script to Google Apps Script. I have a doPost() function set up in the Google Apps Script file, but the post request needs to be authenticated to run.
I believe I need to pass an OAuth token in the header of my request, but I'm unsure how to get this token in the first place. I've found this doc here about web apps in Google Apps Script but no luck finding how to generate an OAuth token from an external service.
I may be completely off the rails with my thinking, so if there's an easier way to make a post request and authenticate it from an external service, I'm all ears.
EDIT:
I don't really have any code at the moment. I'm using Postman to send the calls to Google to test. In Apps Script I have the following inside just to test.
doPost(e) { Logger.log("POST REQUEST") }
When I make the call from Postman, the function isn't triggered. In short, I know I need an authorization in the post request, but I don't know how or where to get it.
I am trying to respond to incoming sms messages with Apps Script through Twilio. I have done all the steps exactly as in the video below.
https://youtu.be/j0wjM1Ds3lc
I currently have a doGet function to handle incoming sms messages. In my Twilio number settings under messaging I have set the incoming messages with a GET webhook to the published apps script url.
When I send a sms message to my Twilio number, I can see the message was received in the Twilio logs but nothing happens in the apps script.
The apps script is published as 'anyone even anonymous' can execute. In the apps script logs I can see that the script is not being executed.
I have sms capabilities on Twilio number.
Any help would be greatly appreciated. I apologize if this is something simple I missed.
function doGet(e) {
var incoming = e.paramater.Body;
var translated = LanguageApp.translate(incoming, "en", "es")
var output = ContentService.createTextOutput(translated).setMimeType(ContentService.MimeType.TEXT);
return output
}
I expect to receive the translated message sent back to my number but currently the apps script is not being executed at all by Twilio.
Change
var incoming = e.paramater.Body;
to
var incoming = e.parameter.Body;
Also, make sure your Webhook is set to HTTP GET.
Trying to receive an SMS at my Twilio number and send a POST request to a Google Apps Script app URL as a result of the received SMS.
I have this doPost() message:
function doPost(request) {
return ContentService.createTextOutput("User says: "+JSON.stringify(request));
}
Some text should be spit out containing the request data.
My doPost() method never gets called. I can't tell if the POST request is actually being sent by Twilio. I see in the Twilio number message log that my SMS is received by Twilio. But after that I can't tell. I have the Twilio number configured for webhook - HTTP POST, and the published URL of my Google Apps Script project. If I change that to HTTP GET my doGet() method DOES get called. I need to doPost() method called, though. any suggestions? TIA.
How about the following confirmation?
Confirmation points :
Redeploy Web Apps as a new version again.
When the script is updated, Web Apps is required to be redeployed as a new version for reflecting the update.
Confirm setting for Web Apps.
"Execute the app as:" is "Me".
"Who has access to the app:" is "Anyone, even anonymous".
Retrieve a log of request using Stackdriver.
The sample script is as follows. Please copy and paste it. And redeploy Web Apps.
Request POST.
On script editor, click View -> Stackdriver Logging
By this, when POST request is received, you can see the log.
Sample script :
function doPost(request) {
console.log(JSON.stringify(request)); // Here
return ContentService.createTextOutput("User says: "+JSON.stringify(request));
}
By above confirmation, the reason of your problem may be found. But if this was not useful for you, I'm sorry.
I decided to just use doGet(). See my response to the previous comment.
TL;DR - I would like to setup a Slack slash command that works on Google Apps Script. How do you recommend I do so?
Context:
I currently use google.com/script with Sheets to trigger a webhook call to Slack when someone fills out a Google Form. Here it is for reference. My question is an additional feature I'd like to add that won't overlap with this code.
I want to listen in Google Apps for a Slack slash command, so I've been looking through tons of examples of callbacks. I don't understand the dance necessary to get this going. Here are the variables:
I've setup the Slack slash command, which asks for a URL and method type (POST or GET), then gives me a token for verification of the outgoing payload.
I've setup OAuth 2.0 client IDs on Google APIs, which provides a client ID and secret token.
The Google Apps tutorial here offers https://script.google.com/macros/d/{SCRIPT ID}/usercallback, but if I'm reading it correctly in this guide it would require user authorization
I've reviewed other questions that mention callback (this and this) but haven't found anything relevant.
Guidance toward the next steps of just being able to configure a callback URL and understand how it will authentication and ultimately consume the API call from Slack in Google would be awesome.
You are on the right track, i'm fiddling around with this since couple of weeks so maybe this helps:
Google App Script
Publish > Deploy as Web App > Version: New / Execute the App as : Me / Who has access: Anyone Even Anonymous
You'll get a url for that web app something like https://script.google.com/macros/s/xxx-ver-long-number-xxx/exec
Copy that
Slack Integrations
Create new Slash Command
Method POST / URL: Paste The url from your web app
Copy the Secure Token, you need it in the Google App Script
Set all the other settings as you like.
Don't forget to save
Back to Google App Script
Copy paste this basic stuff
function doPost(request) {
//// SET SECURITY TOKEN (FROM SLACK COMMAND)
var your_token = "YOUR_SLASH_COMMAND_TOKEN";
var output;
//// GET PARAMETES FROM SLACK POST REQUEST
var params = request.parameters;
//// ... and store them into variables
var cmd_token = params.token; // or params.token[0] idk
//// CHECK RECEIVED TOKEN AGAINST YOUR SAVED TOKEN
if (your_token == cmd_token) {
output = {"text":"SUCCESS"};
} else {
output = {"text":"INVALID TOKEN"};
}
//// SEND RESPONSE BACK TO SLACK
return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
}
Save script and again publish the script as web app (you have to do this everytime you change something, always choose "new" as version
Additional Info
This is the stuff you get from every slash command. In the example above I only use token ...
token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678
Works perfect for me, hope it helps :) Good luck !