-
-
Save WebD00D/479a8a22508459a1b4a36e32c5fa0fe5 to your computer and use it in GitHub Desktop.
Test Bot Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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