The following features may not be available to all Transposit Developer Platform users. Please contact Transposit support to get access to these features.
In this short guide, you'll build a custom Slack command that helps anyone in your workspace list their day's events from their own Google calendar.
You'll need:
calendar_bot
, and do not select a template, as shown below.({ http_event }) => {
return {
status_code: 200,
headers: { "Content-Type": "application/json" },
body: { "greeting": "Hello World" }
};
}
/calendar
./calendar
. You should see the following in Slack, as defined in your webhook logic, shown above:transposit/slack
.While Transposit makes it easy to authenticate with your Slack account, in order to act as your bot, we need to manually input the credentials of the bot. Otherwise the application will execute as you.
https://accounts.transposit.com/oauth/v2/handle-redirect
as a redirect URL.If done correctly, it should authenticate the Transposit application as the bot user, instead of you. When you post a message (if you have given your bot chat:write:bot
permissions) you should see the message posted as the bot.
Create a new JavaScript file named 'get_slack_message', with the content below.
({ details }) => {
triggerId = details.trigger_id;
userName = details.user_name;
modal = {
type: "modal",
title: {
type: "plain_text",
text: "Post Calendar Events",
},
submit: {
type: "plain_text",
text: "Create",
},
close: {
type: "plain_text",
text: "Cancel",
},
blocks: [
{
type: "input",
element: {
type: "plain_text_input",
placeholder: {
type: "plain_text",
text: "Event highlights for " + userName,
},
},
label: {
type: "plain_text",
text: "Description:",
},
},
{
type: "section",
text: {
type: "mrkdwn",
text: 'placeholder for events'
}
},
],
};
slackParams = {};
slackParams["view"] = JSON.stringify(modal);
slackParams["trigger_id"] = triggerId;
openModalResp = api.run("slack.open_modal_view", slackParams)[0];
return {
status_code: 200,
body: null,
};
};
Change your webhook to connect to the above, as shown below.
({ http_event: { parsed_body } }) => {
switch (parsed_body.command) {
case "/calendar":
api.run("this.get_slack_message", { details: parsed_body });
break;
}
return {
status_code: 200,
body: null,
};
};
When you now run your /calendar
command in Slack, you should now see the below.
Your modal is correctly shown in Slack, now you need to add your Google Calendar events to it.
transposit/google_calendar
.get_calendar_events
operation as the code template.get_slack_message
operation, add the below above the main body of code to retrieve data from your Google calendar. const parameters = {};
parameters.calendarId = 'primary';
events = api.run('google_calendar.get_calendar_events', parameters, {limit: 10})
.map(e => e.summary)
.join("\n");
console.log(events);
get_day_start_end
.moment.js
library to get the day's start and end time (replace America/Los_Angeles
with your timezone if desired):params => {
let moment = require("moment-timezone-with-data.js");
let today = moment().tz("America/Los_Angeles");
return {
start: today.startOf("day").format(),
end: today.endOf("day").format()
};
}
[
{
"start": "2019-08-20T00:00:00-07:00",
"end": "2019-08-20T23:59:59-07:00"
}
]
google_calendar
to allow users to connect their Google Calendar to their slack account.get_calendar_events
and replace the SQL with the following to transparently join together the start and end time from the get_day_start_end
operation with the Google Calendar call.SELECT summary FROM google_calendar.get_calendar_events as E
JOIN this.get_day_start_end AS T
ON E.timeMin=T.start
AND E.timeMax=T.end
AND E.singleEvents=true
WHERE E.calendarId='primary'
LIMIT 100
get_slack_message
operation so it returns calendar events for the day.({ user }) => {
let events = api
.run("this.get_calendar_events")
.map(e => e.summary)
.join("\n");
return {
// Blocks get displayed in the actual message.
// You can play with block kit here: https://api.slack.com/tools/block-kit-builder
blocks: [
{
type: "section",
text: {
type: "mrkdwn",
text: events === "" ? "You have no events today" : events
}
}
],
// The text content gets displayed in the notification
text: "A message from Transposit!"
};
}
/calendar
. You should now see a list of your day's events.There are a lot of ways to extend this application. Here are some things you can try:
/calendar
since you installed it in the workspace). Don't they deserve to know what they have on the calendar from the comfort of their Slack command line? They can sign up with their Slack credentials at the same url: https://calendar-bot-XXXXX.transposit.ioget_day_start_end
operation so that you display events from today and tomorrow.select * from google_calendar.get_calendar_events ...
and see what comes back.)get_slack_message
.There's a lot you can do with Transposit’s powerful relational engine; imagine connecting APIs from JIRA, AWS, GitHub, Airtable and more.
Check out other documentation to learn more, including: