Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Twilio message callbacks #77

Merged
merged 2 commits into from Oct 5, 2020
Merged

Conversation

benweissmann
Copy link
Collaborator

@benweissmann benweissmann commented Oct 4, 2020

This PR does a couple things:

  • Specify a callback URL when sending a message to Twilio, so we get notified about delivery success/failure of our text messages
  • Handle these callbacks by updating the DB and adding a reaction to the Slack message
  • Store message timestamps from Slack as strings, not numbers. This is important because right now a message TS of 1234.567000 from slack gets stored as 1234.567. But if you try to pass 1234.567 to the slack API when you're trying to do something with that message, Slack will say that's an invalid timestamp -- the timestamps are supposed to be strings with the same number of digits of precision as slack tell you, so dropping the trailing 0's makes it invalid. See this for details: Timestamp type inconsistency slackhq/slack-api-docs#7 (comment)

IMPORTANT: this now requires the bot to have the reactions:write permission. I've added this to the VoteAmerica staging and prod instances, but you'll have to add it to your dev instances to test this out.

Copy link
Collaborator

@fongandrew fongandrew left a comment

Choose a reason for hiding this comment

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

Pulled and tested on my end. I'll let @tomerovadia take a look, but it looks good to me.

Copy link
Owner

@tomerovadia tomerovadia left a comment

Choose a reason for hiding this comment

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

All looks excellent. Added a few comments.

src/slack_api_util.ts Outdated Show resolved Hide resolved
sql/20201004.sql Show resolved Hide resolved
src/db_api_util.ts Outdated Show resolved Hide resolved
src/db_api_util.ts Outdated Show resolved Hide resolved
}

// Update the Postgres DB with the message status and current timestamp
const slackMessageInfo = await logTwilioStatusToDb(messageSid, messageStatus);
Copy link
Owner

Choose a reason for hiding this comment

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

Out of curiosity, why doesn't this need a catch? I see the function itself has a try/finally within it like other DBApiUtil functions, but I think we have try/catches around calls to other DBApiUtil functions elsewhere.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We only need a try/catch if there's something we want to do when there's an error other than just return a 500. In this case, there's nothing to do -- if we can't query the database, we can't get the slack message info, so we can't add a reaction. So we might as well just let the error bubble up and return a 500 -- try/catch is just for when there's some useful fallback behavior or graceful failure mode.

Copy link
Owner

Choose a reason for hiding this comment

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

What about catching so we can log to Sentry?

`
UPDATE messages
SET
twilio_callback_status = $1,
Copy link
Owner

Choose a reason for hiding this comment

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

What are your thoughts on modifying the successfully_sent field as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we can leave successfully_sent as is -- to indicate that we sent the message off to Twilio. I'm not sure there's much use in updating both successfully_sent and the twilio_status when we get a twilio status callback.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok great point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants