Skip to content

Instantly share code, notes, and snippets.

@woodwardtw
Created May 12, 2021 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save woodwardtw/e53ab78b252218481bf27337638424ea to your computer and use it in GitHub Desktop.
Save woodwardtw/e53ab78b252218481bf27337638424ea to your computer and use it in GitHub Desktop.
const { App } = require('@slack/bolt');
const WPAPI = require( 'wpapi' );//add in http://wp-api.org/node-wpapi
// Initializes your app with your bot token and signing secret
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET
});
app.command('/courseupdate', async ({ command, ack, body, client }) => {
// Acknowledge the command request
await ack();
try {
// Call views.open with the built-in client
const result = await client.views.open({
// Pass a valid trigger_id within 3 seconds of receiving it
trigger_id: body.trigger_id,
// View payload
view: {
type: 'modal',
// View identifier
callback_id: 'course-submit',
title: {
type: 'plain_text',
text: 'Course Update'
},
blocks: [{
type: "input",
block_id: 'course_selection',
element: {
type: "static_select",
placeholder: {
type: "plain_text",
text: "Select your course",
emoji: true
},
options: [
{
text: {
type: "plain_text",
text: "ICC Global Leadership",
emoji: true
},
value: "icc-global-leadership"//9
},
{
text: {
type: "plain_text",
text: "Japanese Ethnography",
emoji: true
},
value: "japanese-ethnography-as-a-window-to-japanese-language-and-society"//10
},
{
text: {
type: "plain_text",
text: "Japanese Introduction to Japanese Pedagogy",
emoji: true
},
value: "japanese-introduction-to-japanese-pedagogy"//11
},
{
text: {
type: "plain_text",
text: "Program Management",
emoji: true
},
value: "program-management"//12
},
{
text: {
type: "plain_text",
text: "Software Internationalization and Localization",
emoji: true
},
value: "software-internationalization-and-localization"//13
}
],
action_id: "course_input"
},
label: {
type: "plain_text",
text: "Course selection",
emoji: true
}
},
{
type: 'input',
block_id: 'good_stuff',
optional: true,
label: {
type: 'plain_text',
text: 'What is going well this week?'
},
element: {
type: 'plain_text_input',
action_id: 'good_input',
multiline: true,
}
},{
type: 'input',
block_id: 'flag_stuff',
optional: true,
label: {
type: 'plain_text',
text: 'Yellow or red flags this week?'
},
element: {
type: 'plain_text_input',
action_id: 'flag_input',
multiline: true
}
},{
type: 'input',
block_id: 'other_stuff',
optional: true,
label: {
type: 'plain_text',
text: 'Anything else to share?'
},
element: {
type: 'plain_text_input',
action_id: 'other_input',
multiline: true
}
}
],
submit: {
type: 'plain_text',
text: 'Submit',
}
}
});
//console.log(JSON.stringify(result));
}
catch (error) {
console.error(error);
}
});
//update modal
app.view('course-submit', async ({ ack, body, view, client }) => {
// Acknowledge the action
await ack();
console.log(JSON.stringify(body));//see the data getting passed
let msg = '';
const user = body['user']['id'];
const results = await createWpUpdate(body['user']['username'],body);
if (results){
msg = 'Your update was successful.'
} else {
msg = 'I am a failure but I do not know why.'
}
//message the user
try {
await client.chat.postMessage({
channel: user,
text: msg
});
}
catch (error){
console.error(error);
}
});
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log('⚡️ Bolt app is running!');
})();
function createWpUpdate(slackuser, body){
const values = body.view.state.values;
let good = values.good_stuff.good_input.value;
let flag = values.flag_stuff.flag_input.value;
let other = values.other_stuff.other_input.value;
let course = values.course_selection.course_input.selected_option.value;//easy to miss selected_option
let courseId = courseTagToID(course);
var wp = new WPAPI({
endpoint: 'http://experiments.middcreate.net/slack/wp-json',
// This assumes you are using basic auth, as described further below
username: process.env.WP_USER,
password: process.env.WP_PASS
});
var user = process.env.WP_USER;
//establish route for custom post type update ***************** from https://github.com/WP-API/node-wpapi/issues/275
const namespace = 'wp/v2';
const updateRoute = '/update/';
wp.update = wp.registerRoute(namespace, updateRoute);
//date
const currentDate = new Date();
const currentDayOfMonth = currentDate.getDate();
const currentMonth = currentDate.getMonth(); // Be careful! January is 0, not 1
const currentYear = currentDate.getFullYear();
const dateString = currentDayOfMonth + "-" + (currentMonth + 1) + "-" + currentYear;
wp.update().create({ //leave as post instead of update if just regular post
// "title" and "content" are the only required properties
title: slackuser + ' via slackbot on ' + dateString,
content: 'nothing to see here',
post_type: 'update',
// },
meta: {
going_well: good,//tied to defining them on the WP side w register_meta
flags: flag,
else: other
},
tags: [ courseId ],
// Post will be created as a draft by default if a specific "status"
// is not specified
status: 'publish'
}).then(function( response ) {
// "response" will hold all properties of your newly-created post,
// including the unique `id` the post was assigned on creation
//console.log( response.id );
})
return true;
}
function courseTagToID(course){
let courses = {
'icc-global-leadership': 9,
'japanese-ethnography-as-a-window-to-japanese-language-and-society': 10,
'japanese-introduction-to-japanese-pedagogy': 11,
'program-management': 12,
'software-internationalization-and-localization': 13,
};
if (courses[course]){
return courses[course];//remember the issue with hyphens in json
} else {
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment