Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Slack Events api #480

Closed
wants to merge 31 commits into from
Closed

Slack Events api #480

wants to merge 31 commits into from

Conversation

jonchurch
Copy link
Contributor

@jonchurch jonchurch commented Nov 8, 2016

Had to resubmit this because I somehow got the other branch into a non-repairable headless state that github support advised I abandon ¯_(ツ)_/¯

Receive Events API events at the webhook setup by createWebhookEndpoints()

EDIT: startTicking() is now called for you when wehbhook endpoints are created
Use controller.startTicking() when running bot to enable conversations

Would like feedback on Bot Identity management without RTM. I'm saving bot identity to team storage when a user completes oauth flow, and looking it up from storage each time an Events API request is received.

closes #438

add events_api boolean, event_ts, authed_users
…ntity is not in team storage use identifyBot
…ts api to play nice together

start ticking when webhooks are setup
@jonchurch jonchurch mentioned this pull request Nov 8, 2016
@sundeepgupta
Copy link
Contributor

sundeepgupta commented Nov 9, 2016

Read the changelog earlier and the latest 0.4.1 release already support the Events API - no?

@jonchurch
Copy link
Contributor Author

jonchurch commented Nov 9, 2016

Nope! It was mostly small fixes in 4.1
Biggest slack change was defaulting to Web API for sending messages, which is a separate issue from receiving events from Events API at a webhook.

Any feedback @sundeepgupta?
I still need to better explain subscribing to events in the docs. For example, a file shared into a channel the bot is in will generate a message event (assuming the bot is subscribed to messaging events) that has a subtype field with a value of file_share. This is representative of the message slack sends into the channel. Botkit will trigger any message with a subtype as an event of that subtype. So now we can listen to file_share events. However, there is a separate permission in the Events API that is fired whenever a file is shared anywhere on a team. That will fire a file_shared(notice the d) event from the Events API, and botkit will fire file_shared when receiving this event. However the file_shared event is generated under a different permission, and contains different information than file_share(which at this point has much more data about that file than file_shared).

I left it this way because that's how Botkit did it before, but this would likely be very confusing unless explained well in the docs.

@sundeepgupta
Copy link
Contributor

@jonchurch I've yet to learn about Events API and haven't looked at your PR, sorry. Thought I'd just check to make sure this wasn't duplicated effort but I obviously mis-understood the changelog.

Copy link
Contributor

@colestrode colestrode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I haven't tested this against the API yet, but I left some comments.

@@ -27,8 +27,8 @@ This is a sample Slack Button application that adds a bot to one or many slack t
/* Uses the slack button feature to offer a real time bot to multiple teams */
var Botkit = require('../lib/Botkit.js');

if (!process.env.clientId || !process.env.clientSecret || !process.env.port) {
console.log('Error: Specify clientId clientSecret and port in environment');
if (!process.env.clientId || !process.env.clientSecret || !process.env.port || !process.env.redirectUri) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is redirectUri required in this example now? It's optional in the Slack OAuth flow.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was actually part of a PR from #458, which I realize now was not necessary.
You are correct, redirectUri is not required here, I misunderstood their issue.
Removing

@@ -287,15 +358,19 @@ function Slackbot(configuration) {

slack_botkit.webserver = express();
slack_botkit.webserver.use(bodyParser.json());
slack_botkit.webserver.use(bodyParser.urlencoded({ extended: true }));
slack_botkit.webserver.use(bodyParser.urlencoded({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These formatting changes make it a bit hard to understand what changes are relevant to this functionality. Would it be possible to exclude them from this PR?


// Save bot identity to team storage for retrieval when handling Events API
slack_botkit.findTeamById(identity.team_id, function(err, team_data) {
if (!team_data.bot.identity) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

team_data will probably not be defined if there is an error. I suggest testing for the error immediately, then exiting early if one exists.

examples/db_slackbutton_bot/
examples/db_slackbutton_incomingwebhook/
examples/db_slackbutton_slashcommand/
examples/db_team_bot/
.DS_Store
*/.DS_Store
.env
.idea
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we need .idea in the gitignore. That seems like it should be in users global .gitignore if they are webstorm users.

@@ -2,10 +2,12 @@ node_modules/
start.sh
start_button.sh
db/
db_slack_events_api/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not put this db under either the examples/ directory?

res.status(200).json({
challenge: req.body.challenge
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of extra spaces in this file before closing braces. Would you mind removing them to be more consistent with the rest of the file?


// Receive messages and trigger events from the Events API
if (req.body.type === 'event_callback') {
// respond to events api with a 200
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be possible to pull all of this logic inside the if into it's own function? this function is getting very long and hard to follow, decomposing it will make it easier to read.

if (err || !team) {
slack_botkit.log.error('Received Events API message, but could not load team');
if (err) {
slack_botkit.log.error(err);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you return here, then you save yourself the if and keeps the indentation flatter.

bot.identifyBot(function(err, identity) {
bot.identity = identity;

if (req.body.event.type === 'message') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logic is duplicated, could we make this a function?


if (team.bot.identity == undefined || team.bot.identity.id == null) {
// API call to slack to identify bot user
bot.identifyBot(function(err, identity) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if there is an error identifying the bot?

@benbrown benbrown mentioned this pull request Nov 18, 2016
@benbrown
Copy link
Contributor

This got merged in to #500 and is now released! Thanks @jonchurch and @colestrode and @sundeepgupta and @peterswimm for your attention to this!!

@benbrown benbrown closed this Nov 21, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for the Slack Events API
5 participants