Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenFaaS Google Form Response Handler
// Put your OpenFaaS gateway public URL here
var baseURL = "http://02626413.ngrok.io";
var functionURL = "/function/signup-form";
function onSubmit(entry) {
var response = entry.response.getItemResponses();
var answers = {};
for (var j = 0; j < response.length; j++) {
var itemResponse = response[j];
answers[itemResponse.getItem().getTitle()] = itemResponse.getResponse();
}
// Build request
var options = {
method: "post",
payload: JSON.stringify(answers)
};
// Send to OpenFaaS
UrlFetchApp.fetch(baseURL + functionURL, options);
};
using Newtonsoft.Json;
namespace Function
{
public class FormResponse
{
[JsonProperty("Email")]
public string Email { get; set; }
[JsonProperty("First Name")]
public string FirstName { get; set; }
[JsonProperty("Last Name")]
public string LastName { get; set; }
[JsonProperty("Company")]
public string Company { get; set; }
[JsonProperty("Location")]
public string Location { get; set; }
[JsonProperty("I'm joining to")]
public string JoinReason { get; set; }
}
}
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using SlackAPI;
namespace Function
{
public class FunctionHandler
{
public string Handle(string input) {
FormResponse formResponse = JsonConvert.DeserializeObject<FormResponse>(input);
var message = FormatMessage(formResponse);
var slackSent = SendToSlack(message);
var fnResponse = $"Response recorded from {formResponse.Email}. Slack posted? {slackSent}";
return fnResponse;
}
private string FormatMessage(FormResponse response)
{
var msg = new StringBuilder();
msg.Append($"{response.FirstName} {response.LastName} would like to join the Slack group!" );
if (!string.IsNullOrWhiteSpace(response.Company))
{
msg.Append($"\nThey work at {response.Company}.");
}
if (!string.IsNullOrWhiteSpace(response.Location))
{
msg.Append($"\nThey say they're from {response.Location}");
}
msg.Append($"\nThe reason they'd like to join is to {response.JoinReason}");
msg.Append($"\nSend them an invite link to {response.Email}");
return msg.ToString();
}
private bool SendToSlack(string message)
{
var token = GetSecret("slack-token");
var client = new SlackTaskClient(token);
var channel = "signup";
var response = client.PostMessageAsync(channel, message);
return response.Result.ok;
}
private string GetSecret(string name)
{
try
{
using (StreamReader sr = new StreamReader("/var/openfaas/secrets/slack-token"))
{
String line = sr.ReadToEnd();
return line;
}
}
catch (IOException e)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
return string.Empty;
}
}
}
}
provider:
name: faas
gateway: http://127.0.0.1:8080
functions:
signup-form:
lang: csharp
handler: ./signup-form
image: signup-form:latest
secrets:
- slack-token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.