Skip to main content

Business plan experiments

How to “SMS” in POWR

Feel free to update this file if needed...

Idea

The main idea of this document is to clearly understand the SMS writing process, and document it, so that future interns or colleagues will easily understand.

Prerequisites

Before coding, you should be familiar with:

  • Twilio in POWR project.

    powr/lib/sms/twilio.rb

  • How Workers/Jobs/Schedulers work

FYI:

  • Twilio is a ruby gem that allows us to use Twilio’s API or connect to Twilio.
  • Read a little about Twilio.

2021 ticket #11175

I have an idea about how to create an SMS reply in the project according to the ticket.

**_Scenario A+B:_**
  • Twilio settings, set up number, account [Done]
  • Test an SMS, does it send to the Zendesk [Done]
  • Write a Webhook to read replies and reply back to them, Google Analytics included in this function I think. [Done]
  • In app/models/app.rb, ‘before_save’write down the function/method that will activate Job/Worker/Scheduler, which will wait (be delayed for 1 hour). [Done]
  • In the same function use Twilio Module’s send_sms method. [Done]
  • Same function will read the SMS, or Webhook will read it instead. [Done]

Twilio Set Up

  1. Go into Develop → Messaging → Try it out

  2. After setting up the Messaging service you are able to send an SMS. Also, in the previous section you got your Authentication Token.

  3. Actually when I set up the Phone number to send the SMS, I took it from Step Strong, she said that this number is connected to the zendesk of POWR. All messages to this number goes to Zendesk.

  4. If you set up everything correctly, you may try to send sms in the Try it out section.

  5. In the right side of the “Send SMS” section you may see the code in different languages, our choice is Ruby. We can just copy this and use it in the .rb file to send an SMS, pretty easy. (Be careful, when you are sending an SMS, initially Twilio gives 15$ for trial, if you can’t send SMS, check the trial money)

  6. If you want to read _replies _go to the Messaging → [Your Messaging service][Your sending number] → Messaging → A Messages Comes In. In this input section you may:

    1. If it is Webhook → You can just put full URL of the Function
    2. I haven’t tested others, if in the future you will, pls complete this list :)

    More you can find here.

Set up Twilio in localhost/staging/prod

  1. Actually you don’t have to install Twilio integration at all, because we have already implemented module/helper methods to make our life easier. (You can read code in twilio.rb file)

  2. When you successfully created the Twilio account, you will automatically get the

    1. TWILIO_ACCOUNT_SID:
    2. TWILIO_AUTH_TOKEN:
    3. TWILIO_PHONE_NO:
    4. MESSAGING_SERVICE_SID:

    You need these things to connect Twilio with your localhost, staging or production. But in reality, in staging and in production you already have them, and you don’t have to change, so just use Twilio in staging and prod.

  3. I recommend you save them into application.yml.

[]https://lh3.googleusercontent.com/fBKTEC7dSE125CF_zEiLSJWRD7P57UIgccT6mTfX8WjkNHOswIgMIznCs1c8VrCtGSMv4y2tJ1UUFOMhzK-fhn91RB3VzWbkc-Z3iiOd-iAcXUerB2EN1Pqkf60Yzgwc64mb7tOr=s1600 4. Now you are ready to send an SMS implementing some SMS stuff kinda workers/jobs/schedules.

How to send your first SMS?

As I said, in our project sms sending is not complicated (if you are not rewriting the twilio.rb file ∠( ᐛ 」∠)_)

How to send SMS in Rails Console?

Prerequisites: 
  • Existing User

  • Phone number of this User’s also should be filled

  • The opt_in_sms field is not false (The _opt_in_sms _means that User didn’t uncheck the field Opt Into Text Messaging in the user's settings privacy page).

    Example:

I took the number from the US POWR team, because Twilio can’t send SMS to Kazakhstan (T_T). And the result you will see in Zendesk, if you ask the number from the US Team, probably they will give you the Zendesk number, so here:


Result:

How to send SMS with Worker/Scheduler/Job

Prerequisites:

  • Existing User
  • Phone number of this User’s also should be filled
  • The opt_in_sms field is not false (The _opt_in_sms _means that User didn’t uncheck the field Opt Into Text Messaging in the user's settings privacy page).
  1. First of all create a worker in the “workers” folder in POWR project. (example: some_kind_of_worker.rb)
  2. You should fill the worker as here (Note it is not the full convention of the worker because it depends on the situation and what you need to complete the ticket):
  3. The algorithm for sending the SMS is the same as running the console command to send an SMS.
  4. You can run worker manually (1) and with console (2)
    1. To run manually you just need to perform the steps that triggers the worker.
    2. To run in console you should:
      1. Uncomment the require 'sidekiq/testing/inline' in development.rb.
      2. In console write: YourWorkerClassName.perform_async()
    3. Note that you should exit and open the console again, if you changed the worker and the changes will be applied.

How to get a reply for the SMS?

To get any reply you need a Webhook and Ngrok.

If you want to test in Google Voice, which is the best choice, you should cherry-pick your commit to the staging. When you finished deployment to the staging you should:

  1. Login to Google Voice, you should login via [email protected], because it is the common number. Don’t forget to use last past and zendesk, it is the only way to verify your device and successfully log in.
  2. Assign number from Google Voice to your account in powr.io
  3. Try to trigger your webhook or the code that will receive the request from Twilio.

Things that may help you

  • You can’t send an SMS to yourself. From Twilio number to Twilio number.
  • You can’t send an SMS to Kazakhstan.
  • You can’t send SMS to the undefined number if you are using the Trial version. You should verify the number in the Phone Numbers → Manage → Verified Caller IDs. If you are using the trial version, you will probably be able to add only one number.
  • If you are interested in the code that I’ve written, it is here.