Forwarding a deciphered Y/N response to Admin as an SMS message using Twilio.

The previous blog post, with respect to processinga received SMS message used the code:

await handleRecvdTwilioSMS.ProcessMessage(requestBody, from, helper);

… to handle all processing of the received message, apart from a standardise reply that would be handled directly by the controller. This processing involves deciphering the Y/N response into Yes or No as well as forwarding that to an Admin via SMS. Firstly, the Twilio API does not have a concept of on-forwarding received messages. They way that is done is by generating a new message to be sent much like the original request for a Y/N response. One the deciphering outcome is determined the following message is sent via SMS to an Admin account:

string reply = $"Outcome:{outcome} Message:{body} - From:{from}";

Note it is also an interpolated string as discussed 2 posts ago.

The deciphering is an a priori search for Y, N Yes or No etc:

    public enum YesOrNo { yes, no, indeterminate , none};

    YesOrNo HandleYesNo(string msg)
    {
        msg = msg.ToUpper().Trim();

        if (string.IsNullOrEmpty(msg))
            return YesOrNo.indeterminate;
        if (msg[0] == 'N')
            return YesOrNo.no;
        else if (msg[0] == 'Y')
            return YesOrNo.yes;
        else if (msg.Contains("NO"))
            return YesOrNo.no;
        else if (msg.Contains("YES"))
            return YesOrNo.yes;
        return YesOrNo.indeterminate;
    }

An indeterminate response may or may not be of importance depending upon the context.

The ProcessMessage method follows:

    public async Task ProcessMessage(string msg, string from, Helper helper)
    {
        YesOrNo res = HandleYesNo(msg);
        await LogMsg(res, msg, helper);
        switch (res)
        {
            case YesOrNo.indeterminate:
                // Perhaps a reply here to sender.
                await ForwardMessage(res, msg, from, helper);
                break;
            case YesOrNo.yes:
                if (TwillioController.ForwardAllToAdmin)
                    await ForwardMessage(res, msg, from, helper);
                break;
            case YesOrNo.no:
                await ForwardMessage (res, msg, from, helper);
                break;

        }
    }

ForawrdMessage() is similar code that used in 2 posts ago to send the original query to helpers.

Logging

There is an ActivityState property of Activity:

 public enum ActivityState {Unassigned, Assigned, Sent, Yes, No, Indet }

The Lifecycke of this is:

  1. New Activity -> Unassigned
  2. Helper Volunteers -> Assigned
  3. Request for Confirmation sent -> Sent
  4. Yes response received -> Yes
  5. No response received -> No

Indet could be used but currently is ignored.

An Admin user can view the state of each Activity on the Activities on Day page. Also an Admin can directly change that on that page.
Volunteers can not see it. They only see the columns up to Helper.

confirmed

There is also a service LogSMSService that logs sent, received and on-forwarded messages with the database.


Next That is all on this topics for now. Time to get back to IOT


 TopicSubtopic
  Next: > Blazor Helpers App
   
 This Category Links 
Category:Twilio SMS Index:Twilio SMS
  Next: > Blazor Helpers App Twilio
<  Prev:   Blazor Helpers App Twilio