Send SMS from Chatter Feed with Action Link Template

In this blog, I am going to explain how to send SMS  from chatter feed by using action link templates. I am going to use Twilio rest API to send the SMS. Please refer this link for more information on action link templates Inside Action Link Templates

Step 1: –   Defining Action Link Group Templates

go to Setup, enter Action Link Templates in the Quick Find box, then select Action Link Templates create a new one with the below values

Field Value
Name Send Text Messages
Developer Name Send_Text_Messages
Category Primary action
Executions Allowed Once
Hours until Expiration  

10.PNG

 

Step 2: – Creating an  Action Link Temple 

Create a new Action link template under the above-created action link group template with the following details.

Field Value
Action Link Group Template Send Text Messages
Action Type Api API Async
Action URL https://api.twilio.com/2010-04-01/Accounts/{!Bindings.accountSID}/SMS/Messages.json
User Visibility Every one can see
HTTP Request Body Body={!Bindings.body}&To={!Bindings.toNumber}&From={!Bindings.fromNumber}
HTTP Headers Authorization: Basic {!Bindings.authToken}
Host: {!Bindings.host}
Content-Length: {!Bindings.lenght}
X-Target-URI: {!Bindings.uri}
Content-Type: {!Bindings.conenttype}
Position 0
Label Key None
Label Text Message
HTTP Method POST

after saving the Action link template its looks as shown below

11.PNG

Go back to the action link group template and publish it

Step 3: – Posting it to chatter feed 

Now I am going to use apex to instantiated action link template and post it to chatter.

go to developer console and paste the below code.

 

  // Query the Action link Groupd Id 
        ActionLinkGroupTemplate template = [SELECT Id FROM ActionLinkGroupTemplate WHERE
                                            DeveloperName='Send_Text_Messages'];
        Map<String, String> bindingMap = new Map<String, String>();
        // Map of Binding variables 
        bindingMap.put('accountSID', '<Your Account SID>');
        bindingMap.put('body', 'testing');
        bindingMap.put('toNumber','2159155090');
        bindingMap.put('fromNumber','2674600419');
        bindingMap.put('authToken', 'Auth token ==');
        bindingMap.put('host', 'api.twilio.com');
        bindingMap.put('lenght','113');
        bindingMap.put('uri','https://api.twilio.com');
        bindingMap.put('conenttype','application/x-www-form-urlencoded');

        List&lt;ConnectApi.ActionLinkTemplateBindingInput&gt; bindingInputs = new  List&lt;ConnectApi.ActionLinkTemplateBindingInput&gt;();
        for (String key : bindingMap.keySet()) {
            ConnectApi.ActionLinkTemplateBindingInput bindingInput = new
                ConnectApi.ActionLinkTemplateBindingInput();
            bindingInput.key = key;
            bindingInput.value = bindingMap.get(key);
            bindingInputs.add(bindingInput);
        }

        ConnectApi.ActionLinkGroupDefinitionInput actionLinkGroupDefinitionInput = new
            ConnectApi.ActionLinkGroupDefinitionInput();
        actionLinkGroupDefinitionInput.templateId = template.id;
        actionLinkGroupDefinitionInput.templateBindings = bindingInputs;
        // Action link Group Definition .
        ConnectApi.ActionLinkGroupDefinition actionLinkGroupDefinition = ConnectApi.ActionLinks.createActionLinkGroupDefinition(Network.getNetworkId(),
                                                                                                                                actionLinkGroupDefinitionInput);

        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        ConnectApi.FeedElementCapabilitiesInput feedElementCapabilitiesInput = new
            ConnectApi.FeedElementCapabilitiesInput();
        ConnectApi.AssociatedActionsCapabilityInput associatedActionsCapabilityInput = new
            ConnectApi.AssociatedActionsCapabilityInput();
        ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
        ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
        feedItemInput.body = messageBodyInput;
        feedItemInput.capabilities = feedElementCapabilitiesInput;
        feedItemInput.subjectId = 'me';
        messageBodyInput.messageSegments = new List&lt;ConnectApi.MessageSegmentInput&gt;();
        textSegmentInput.text = 'Click to post a feed item.';
        messageBodyInput.messageSegments.add(textSegmentInput);

        feedElementCapabilitiesInput.associatedActions = associatedActionsCapabilityInput;
        associatedActionsCapabilityInput.actionLinkGroupIds = new List&lt;String&gt;();
        associatedActionsCapabilityInput.actionLinkGroupIds.add(actionLinkGroupDefinition.id);
         ConnectApi.FeedElement feedElement =
            ConnectApi.ChatterFeeds.postFeedElement(Network.getNetworkId(), feedItemInput);

 

Now you can see new action link on the chatter feed as shown below.

12.PNG Once you click on the Text message it will send an SMS and also update the Statis to chatter feed.