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 webhook handler #693

Merged
merged 14 commits into from
Apr 1, 2021
Merged

Add Twilio webhook handler #693

merged 14 commits into from
Apr 1, 2021

Conversation

fmterrorf
Copy link
Contributor

@fmterrorf fmterrorf commented Mar 27, 2021

Description

  • Added Twilio webhook handler

Issue

Fixes #675

Screenshot

Here's a screenshot of a new conversation
twilio-test
I used ngrok to test this locally

Checklist

  • Everything passes when running mix test
  • Ran mix format
  • No frontend compilation warnings


test "returns 200 when message is successfuly created",
%{authed_conn: authed_conn} do
with_mocks([
Copy link
Contributor Author

@fmterrorf fmterrorf Mar 27, 2021

Choose a reason for hiding this comment

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

I've decided to use mocks when testing in controller so that I don't have to insert actual data to the db. It seems like this makes sense because it separates the concerns between the controller vs context. The context should do all the business logic and the controller is responsible the web layer. Lmk what y'all think

@fmterrorf
Copy link
Contributor Author

cc @reichert621

lib/chat_api/twilio.ex Outdated Show resolved Hide resolved
@a8t
Copy link
Contributor

a8t commented Mar 28, 2021

I noticed we changed some of the same files. I'm fine to handle conflicts since my PR is newer!

@a8t
Copy link
Contributor

a8t commented Mar 30, 2021

Rebase troubles? 😛

@fmterrorf
Copy link
Contributor Author

Rebase troubles? stuck_out_tongue

Haha :D yeah i rebased to master 🤦 TIL about git reflog

@a8t
Copy link
Contributor

a8t commented Mar 30, 2021

I did the exact same thing the other day but I caught it before force-pushing so I only had to hard reset to origin

@fmterrorf
Copy link
Contributor Author

Nice. I'm so used to merge commits 😄 Trying to get a hold of the rebase workflow

Copy link
Collaborator

@reichert621 reichert621 left a comment

Choose a reason for hiding this comment

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

nice @fmterrorf 🎉 thanks for writing all these tests too :)

just a couple comments, but looks great so far!

now = DateTime.utc_now()
sms_source = "sms"

case Customers.list_customers(account_id, %{phone: phone}) do
Copy link
Collaborator

Choose a reason for hiding this comment

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

might be nice to set up a Customers.find_or_create_by_phone method to abstract over some of this logic -- what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If if I create a Customers.find_or_create_by_phone, It will always return a customer.

The current code right contains this logic

if the customer is not present
    creates a customer + conversation
    return {customer, conversation}
else 
  find conversation
    if no conversation found
        create one
   else
        return {customer, conversation}

If the customer is not present it creates the customer + conversation right away, without having to do a find by conversation

Copy link
Collaborator

Choose a reason for hiding this comment

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

hmm so wouldn't it still work if Customers.find_or_create_by_phone just indicated whether the returned customer was found or created?

maybe something like...

case Customers.find_or_create_by_phone(account, phone) do
  {:ok, :found, customer} -> 
    case Conversations.find_latest_conversation_by_customer(customer.id) do
      nil -> Conversations.create_conversation(...)
      conversation -> {:ok, conversation}
    end

  {:ok, :created, customer} -> 
    Conversations.create_conversation(...)

  error -> 
    error
end

does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah that makes sense!

lib/chat_api_web/controllers/twilio_controller.ex Outdated Show resolved Hide resolved
Copy link
Collaborator

@reichert621 reichert621 left a comment

Choose a reason for hiding this comment

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

looks good for now! 👍 gonna merge and start playing around with it 😄

@reichert621 reichert621 merged commit 3d84567 into papercups-io:twilio-integration-v1 Apr 1, 2021
reichert621 pushed a commit that referenced this pull request Apr 1, 2021
* Added logic

* Added tests for context

* Added controller tests

* Pattern match param

* Removed whitespace

* Add inet6 back

* Add typespec

* Wrap test with describe

* Moved find_or_create_customer_and_conversation to Conversations context

* Mix format

* Cleanup typespec

* Log if twilio account is not found

* Log warn message if twilio account is not found

* Fix twilio controller test
@fmterrorf fmterrorf deleted the twilio-webhook branch April 1, 2021 17:03
reichert621 pushed a commit that referenced this pull request Apr 1, 2021
* Added logic

* Added tests for context

* Added controller tests

* Pattern match param

* Removed whitespace

* Add inet6 back

* Add typespec

* Wrap test with describe

* Moved find_or_create_customer_and_conversation to Conversations context

* Mix format

* Cleanup typespec

* Log if twilio account is not found

* Log warn message if twilio account is not found

* Fix twilio controller test
reichert621 added a commit that referenced this pull request Apr 2, 2021
* Set up Twilio authorization flow (#671)

* Set up boilerplate for Twilio client

* Add migration for twilio authorizations

* Set up boilerplate UI for twilio authorization flow

* Improve error handling

* Add todos, fix warning

* Hide todo comment

* Send SMS notification via twilio (#695)

* Send SMS notification via twilio

* Log error  if twilio sms fails

* Switch error to info

* Remove unused from test

* Add Twilio webhook handler (#693)

* Added logic

* Added tests for context

* Added controller tests

* Pattern match param

* Removed whitespace

* Add inet6 back

* Add typespec

* Wrap test with describe

* Moved find_or_create_customer_and_conversation to Conversations context

* Mix format

* Cleanup typespec

* Log if twilio account is not found

* Log warn message if twilio account is not found

* Fix twilio controller test

* Fix tests

* Improve logging a bit

Co-authored-by: Andy Tran <[email protected]>
Co-authored-by: Daven <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants