Skip to content

Instantly share code, notes, and snippets.

@WebD00D

WebD00D/test-bot.js Secret

Created Nov 13, 2020
Embed
What would you like to do?
Test Bot Example
const { SlackDialog } = require("botbuilder-adapter-slack");
const SLASH_COMMANDS = {
TEST: "/test",
};
module.exports = function (controller) {
/**
* controller.on(slash_command) is Slack's way of listening for any specific
* slash command events a user emits.
**/
controller.on("slash_command", async (bot, message) => {
if (message.command === SLASH_COMMANDS.TEST) {
/**
* If the event emited was /test, than are bot is going to open up
* a dialog box to collect input from the user.
*
* SlackDialog expects a name for the form ("Test"), a callback id ("test-form"),
* and the type of dialog it is ("Submit").
*
* After you set the name, callback, and form type, you can then pass in
* an array of fields. You can read more about the
* various fields you can use here https://api.slack.com/dialogs#text_elements
*/
await bot.replyWithDialog(
message,
new SlackDialog("Test", "test-form", "Submit", [
{
label: "What is your name?", // the question the user will see
name: "name", // the value you can reference on form submission
type: "text", // the type of input
},
])
.notifyOnCancel(true)
.asObject()
);
}
});
/**
* Similar to on slash_command, our bot will also listen for form submissions
* via the dialog_submission event.
*
* In the callback, you'll get a bot and message object.
*
* The bot is the worker here in this case to be able to post messages in Slack, and the message
* object will contain the form responses.
*/
controller.on("dialog_submission", async (bot, message) => {
if (message.callback_id === "test-form") {
/**
* If the form submitted was infact our test-form, then the bot will post
* a formatted message to the #slack-bot-test channel using the name value from our
* dialoge response
*/
await bot.say({
channel: "#slack-bot-test", // make sure you've created this channel and have added your bot.
blocks: [
{
type: "header",
text: {
type: "plain_text",
text: "Bot Submission!",
},
},
{
type: "context",
elements: [
{
type: "plain_text",
text: "Please find submission responses below",
},
],
},
{
type: "section",
text: {
type: "mrkdwn",
text: `User who sumbitted: ${message.name}`,
},
},
],
});
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment