Introduction
The SMS service lets the K2 server send and receive text messages. K2 can also act on the content of a received message: simply storing it, starting a specific workflow when you text a specific hashtag, or actioning a task by SMS.
What you need
For the service to work you need a subscription to the Esendex SMS service (www.esendex.com). You can register for a trial account to start with, which gives you some SMS credits and a mobile number. Make a note of the following:
- Account number
- Username
- Password
- Mobile number
What’s in the service
The zip file contains these components:
- SQL scripts to create the database
- GUID for the SQL database service object
- Package and Deploy file for the K2 artefacts
- SMS receiver
- SMS endpoint assembly
- Code solution for the SMS receiver and endpoint assembly
Setup
These steps go through setting up the K2 server with the SMS service.
- Register for an account at Esendex.com.
- Run the SQL scripts. This creates the K2 SMS database.
- Register the database with K2. Make sure the ‘Authentication Mode’ is set to ‘Service Account’, the service is called ‘K2 SMS’ and it has the GUID ‘1be32a72-9693-4f4a-a143-99adea6e850d’.
- Once the service object has been created you’ll see all the stored procedures and tables that make up the K2 SMS database. It should look something like this:
- The next service to register is an endpoint assembly that lets the K2 SMS service action tasks. In the zip file there’s a DLL called ‘K2Core.dll’. Register it as an endpoint assembly with the name ‘K2 Core Tools’ and the GUID ‘dab558bf-99ca-4a4f-80d7-865032849d5’. This creates the following service objects for you:
- The last service to install is another endpoint assembly that lets K2 send SMS messages. Open the ‘K2SMS’ solution, edit the ‘K2SMS’ project, and in the files ‘Receive.cs’ and ‘SendTxt.cs’ replace the following details with your Esendex details:
private static string _Username = “Esendex Username”;
private static string _Password = “Password”;
private static string _Account = “Account”;Once you’ve done this, build the project and register the DLL it creates as an endpoint assembly. Call the endpoint ‘K2 SMS’ and make sure it has the GUID ‘08700ee5-116f-4894-b921-1fd593e93554C’. It then creates the following service objects:
- Now that all the service objects exist, deploy the K2 package called ‘K2 SMS.kspx’. This deploys the following K2 artefacts for you:
- With the package deployed you can test the service by sending an SMS. Use the SmartObject tester to execute the ‘Send SMS’ SmartObject, entering a mobile number and a message. You should receive the message on your phone, and if you execute the ‘GetInbox’ SmartObject you’ll see the message in the list.
- Next, add the Web API handler that Esendex will post its responses to. Copy the ‘sms’ folder from the zip file onto the C drive.
- Open IIS and expand ‘K2’. Right-click and select ‘Add Application’.
- Enter ‘K2SMS’ as the alias, select ‘K2 SmartForms’ as the application pool and browse to the ‘sms’ folder you copied. It should look something like this:
- On the new application, click ‘Authentication’ and enable ‘Anonymous Authentication’. Your handler is now ready to receive posts from Esendex. We just need to tell Esendex where to post the messages. The URL for your service will be:
http://[domainname]/K2SMS/API/InboundMessages/Post
If you go to that URL now you should get an XML document error. Don’t worry, that’s fine; it’s just expecting to receive an XML document.
- Log into your Esendex account, go to ‘Developer tools’, and in the ‘Configure Push Notifications’ section click ‘configure’ for ‘SMS received’ and enter the URL of your SMS handler.
- You’re now ready to test the service. Reply to the message you sent earlier. If there’s an error, you’ll get an email at the account used to register the service.
- Go back to the SmartObject tester and execute the ‘Messages’ SmartObject. You should see the message you just sent in the list. If you do, setup has worked: K2 has picked up the message and saved it into SQL, and the server is ready to use the service.
Using the service
Now that the service is installed you can send and receive text messages. I’ve split this section into a few use cases.
Simple send and receive
- To send a message, use the ‘Send SMS’ SmartObject. It sends messages to any mobile number and takes the parameters ‘Message’ and ‘Mobile Number’.
- To get a list of received messages, use the ‘Messages’ SmartObject. This shows the messages K2 has picked up.
Starting a workflow from an SMS
The service uses hashtags to identify which process to start.
- First, register the workflow you want started by SMS using the ‘SMSHashtagLibrary’ SmartObject. You need to provide the hashtag, the full process name and the app definition ID (App Framework).
- The process being called also needs these data fields: AppDefinitionId (integer), AppId (integer) and MessageId (integer).
If the SMS contains more text after the hashtag, the service tries to connect it to an existing app instance by matching the app instance folio to that text.
For example, if you sent ‘#Help A0011’, the service would start the workflow linked to ‘#Help’ and then try to find the record where the folio in the ‘CM App’ SmartObject matches ‘A011’.
- You also need to give the ‘K2WebService’ account permission to start the process.
- Once that’s done you can test it. Look in K2 Workspace reporting to see whether an instance of your SMS-enabled process has started.
Sending and actioning tasks by SMS
There are no special wizards for sending and actioning a task by SMS. It’s just a couple of SmartObjects that need to be called.
Send a task
To send a task by SMS, use the ‘SMSTasks’ SmartObject and populate these properties:
- SN
- Status
- MessageId (if applicable)
- Mobile (the mobile number of the destination user)
- AppId
Once these details have been created, the SMS service monitors incoming messages for the serial number. Note that you still need to create your normal client event, with ‘K2 Web Service’ as the destination user.
Then use the ‘Send SMS’ SmartObject. Make sure the mobile number is the same one used to record the task, and that the message body contains the serial number and the available actions.
Action a task by SMS
To action a task by SMS, the user who received the text replies to it. The reply needs to contain the serial number and the action, for example ‘12_1222 Yes’.
K2 picks up the message and first checks whether the serial number is valid for SMS task actions. If it is, it checks whether the reply came from the same mobile number the task was sent to. If everything is valid, it tries to execute the task using the action in the message.
Where can I get the service?
You can download the source code from GitHub: https://github.com/SimonAllport/K2-SMS
The K2 artefacts and SQL scripts were shared at http://1drv.ms/1C9KpUp.
This project is completely open source and isn’t supported by K2.