A very quick article to remind me how to send a text message using Twilio API in Zoho Deluge.
Why?
The use-case here is that we want local sense dialing where specific numbers are used both for outbound and inbound but local to the customer and we want to remind our customer that they have an appointment in 1 hour with us.
How?
The code is actually pretty straightforward but you will need to follow these steps to get the credentials:
- Login to your Twilio console
- On the dashboard, note the ACCOUNT SID and AUTH TOKEN
- In the side menu, expand Phone Numbers > Manage > Active numbers
- Note the number you want to use (they have friendly numbers but I use the full number in the first column)
Sending a Text/SMS message
// // using the account SID you obtained in the steps above v_Account_SID = "ABCDEFabcdef0123456789ABCDEFabcdef"; // // set this to the authtoken value obtained earlier v_Auth_Token = "1234567890aaaabbbbccccddddeeeeff"; // // set this to your Twilio number (dialing/texting from) v_OurTwilioNumber = "+15017122661"; // // set this to your customer's number (dialing/texting to) v_CustomerNumber = "+15558675310"; // // the message we want to send v_Message = "This is a test text"; // // build up the endpoint (see https://www.twilio.com/docs/usage/requests-to-twilio#http-methods for latest endpoint) v_Endpoint = "https://" + v_Account_SID + ":" + v_Auth_Token + "@api.twilio.com/2010-04-01/Accounts/" + v_Account_SID + "/Messages.json"; // // build up the JSON request m_Data = Map(); m_Data.put("Body",v_Message); m_Data.put("From",v_OurTwilioNumber); m_Data.put("To",v_CustomerNumber); // // JSON request r_TwilioResponse = invokeurl [ url :v_Endpoint type :POST parameters:m_Data ]; // // display to user/developer the response return r_TwilioResponse;
- //
- // using the account SID you obtained in the steps above
- v_Account_SID = "ABCDEFabcdef0123456789ABCDEFabcdef";
- //
- // set this to the authtoken value obtained earlier
- v_Auth_Token = "1234567890aaaabbbbccccddddeeeeff";
- //
- // set this to your Twilio number (dialing/texting from)
- v_OurTwilioNumber = "+15017122661";
- //
- // set this to your customer's number (dialing/texting to)
- v_CustomerNumber = "+15558675310";
- //
- // the message we want to send
- v_Message = "This is a test text";
- //
- // build up the endpoint (see https://www.twilio.com/docs/usage/requests-to-twilio#http-methods for latest endpoint)
- v_Endpoint = "https://" + v_Account_SID + ":" + v_Auth_Token + "@api.twilio.com/2010-04-01/Accounts/" + v_Account_SID + "/Messages.json";
- //
- // build up the JSON request
- m_Data = Map();
- m_Data.put("Body",v_Message);
- m_Data.put("From",v_OurTwilioNumber);
- m_Data.put("To",v_CustomerNumber);
- //
- // JSON request
- r_TwilioResponse = invokeUrl
- [
- url :v_Endpoint
- type :POST
- parameters:m_Data
- ];
- //
- // display to user/developer the response
- return r_TwilioResponse;
Adapting it to trigger off a scheduled workflow of a call/meeting
- Login to ZohoCRM > Setup > Automation > Workflow Rules > Create Rule
- Module is "Calls"
- Rule Name can be whatever you want, I'm calling mine "Calls - Send SMS Reminder 1 Hr Before"
- When is "On a Date/Time" based on "Call Start Time" with execution date "On" the date and execution time "1 hour before".
- Condition 1: rule is applied to all calls
- Action is the function below with the parameter "p_CallID" being the Calls - Call ID value.
// r_CallDetails = zoho.crm.getRecordById("Calls",p_CallID); // // using the account SID you obtained in the steps above v_Account_SID = "ABCDEFabcdef0123456789ABCDEFabcdef"; // // set this to the authtoken value obtained earlier v_Auth_Token = "1234567890aaaabbbbccccddddeeeeff"; // // get lead details v_Type = "Leads"; if(!isnull(r_CallDetails.get("$se_module"))) { v_Type = r_CallDetails.get("$se_module"); } // set your default twilio number v_TwilioLocalNumber = "+12345678910"; v_CustomerNumber = ""; v_CustomerFirstName = ""; if(v_Type == "Leads") { if(!isnull(r_CallDetails.get("What_Id"))) { v_LeadID = r_CallDetails.get("What_Id").get("id"); r_LeadDetails = zoho.crm.getRecordById("Leads",v_LeadID); if(!isnull(r_LeadDetails.get("Twilio_Local_Number"))) { v_TwilioLocalNumber = r_LeadDetails.get("Twilio_Local_Number"); } if(!isnull(r_LeadDetails.get("Mobile"))) { v_CustomerNumber = r_LeadDetails.get("Mobile"); } else if(!isnull(r_LeadDetails.get("Phone"))) { v_CustomerNumber = r_LeadDetails.get("Phone"); } if(!isnull(r_LeadDetails.get("First_Name"))) { v_CustomerFirstName = r_LeadDetails.get("First_Name"); } } } // // set this to your Twilio number (dialing/texting from) v_OurTwilioNumber = v_TwilioLocalNumber; // // send reminder if we have the customer's number if(v_CustomerNumber!="") { // // the message we want to send v_Message = "Hi "+v_CustomerFirstName+"! This is just to remind you that we will be phoning you in an hour for your initial consultation with <My_Company_Name>. To reschedule, just reply to this message or phone us on " + v_TwilioLocalNumber; // // build up the endpoint (see https://www.twilio.com/docs/usage/requests-to-twilio#http-methods for latest endpoint) v_Endpoint = "https://" + v_Account_SID + ":" + v_Auth_Token + "@api.twilio.com/2010-04-01/Accounts/" + v_Account_SID + "/Messages.json"; // // build up the JSON request m_Data = Map(); m_Data.put("Body",v_Message); m_Data.put("From",v_OurTwilioNumber); m_Data.put("To",v_CustomerNumber); info m_Data; // // JSON request r_TwilioResponse = invokeurl [ url :v_Endpoint type :POST parameters:m_Data ]; }
- //
- r_CallDetails = zoho.crm.getRecordById("Calls",p_CallID);
- //
- // using the account SID you obtained in the steps above
- v_Account_SID = "ABCDEFabcdef0123456789ABCDEFabcdef";
- //
- // set this to the authtoken value obtained earlier
- v_Auth_Token = "1234567890aaaabbbbccccddddeeeeff";
- //
- // get lead details
- v_Type = "Leads";
- if(!isnull(r_CallDetails.get("$se_module")))
- {
- v_Type = r_CallDetails.get("$se_module");
- }
- // set your default twilio number
- v_TwilioLocalNumber = "+12345678910";
- v_CustomerNumber = "";
- v_CustomerFirstName = "";
- if(v_Type == "Leads")
- {
- if(!isnull(r_CallDetails.get("What_Id")))
- {
- v_LeadID = r_CallDetails.get("What_Id").get("id");
- r_LeadDetails = zoho.crm.getRecordById("Leads",v_LeadID);
- if(!isnull(r_LeadDetails.get("Twilio_Local_Number")))
- {
- v_TwilioLocalNumber = r_LeadDetails.get("Twilio_Local_Number");
- }
- if(!isnull(r_LeadDetails.get("Mobile")))
- {
- v_CustomerNumber = r_LeadDetails.get("Mobile");
- }
- else if(!isnull(r_LeadDetails.get("Phone")))
- {
- v_CustomerNumber = r_LeadDetails.get("Phone");
- }
- if(!isnull(r_LeadDetails.get("First_Name")))
- {
- v_CustomerFirstName = r_LeadDetails.get("First_Name");
- }
- }
- }
- //
- // set this to your Twilio number (dialing/texting from)
- v_OurTwilioNumber = v_TwilioLocalNumber;
- //
- // send reminder if we have the customer's number
- if(v_CustomerNumber!="")
- {
- //
- // the message we want to send
- v_Message = "Hi "+v_CustomerFirstName+"! This is just to remind you that we will be phoning you in an hour for your initial consultation with <My_Company_Name>. To reschedule, just reply to this message or phone us on " + v_TwilioLocalNumber;
- //
- // build up the endpoint (see https://www.twilio.com/docs/usage/requests-to-twilio#http-methods for latest endpoint)
- v_Endpoint = "https://" + v_Account_SID + ":" + v_Auth_Token + "@api.twilio.com/2010-04-01/Accounts/" + v_Account_SID + "/Messages.json";
- //
- // build up the JSON request
- m_Data = Map();
- m_Data.put("Body",v_Message);
- m_Data.put("From",v_OurTwilioNumber);
- m_Data.put("To",v_CustomerNumber);
- info m_Data;
- //
- // JSON request
- r_TwilioResponse = invokeUrl
- [
- url :v_Endpoint
- type :POST
- parameters:m_Data
- ];
- }
Error(s) Encountered
- The 'To' number XXXXXXXXXX is not a valid phone number - Resolved: Do not url encode from and to phone numbers (also do not url encode the body/message). Number is formatted to E.164 [+] [country code] [subscriber number including area code] and can have a maximum of 15 digits.
Source(s):
- Twilio Docs - HTTP Methods - Creating or Updating Resources with the POST Method
- Twilio Docs - Error 21211
- Twilio Docs - E.164 (Telephone numbering plan)