Adding Rich Messages to your In-App Chat
Published: 2021-07-06 05:58:34

<!-- wp:paragraph -->
<p>So, what exactly is <em>rich messaging</em>?</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In some ways, it’s exactly what it sounds like: messaging that is deeper, richer, and more layered. But it’s also much more than that.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Integrating rich messaging into your chat allows you to push something more interactive than just plain text. It allows both parties to share interactive media like images, buttons, and cards seamlessly. In addition, customers can easily interact and respond using rich messaging buttons and even make secure purchases. For example, giving customers the opportunity to view and purchase sandwiches within the chat window led to an <a href="https://www.gsma.com/futurenetworks/wp-content/uploads/2020/03/RCS-Whitepaper-Infobip_Feb20.pdf">increase in Subway’s conversions by 140%</a>.&nbsp;</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Rich messaging can simplify interactions with your customers in a significant manner. If your in-app chat isn’t using rich messaging in 2021, you’re definitely missing out!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To add rich messaging and other exciting features to your app, <a href="https://console.applozic.com/signup">register for a free trial of Applozic's chat API</a>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Rich messaging using Applozic SDK</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In addition to traditional messaging types like text and audio, Applozic also provides a set of messaging options in the form of <em>structured </em>content templates. This article will guide you through creating a structure for sending some types of rich messages.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In order to achieve this behavior, the information must be sent in a structured format that can be included in the metadata of a message. The most common format is JSON (Javascript Object Notation).</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Note</strong>: Rich message UI is only Supported in Applozic-Android-SDK and Applozic-Swift-SDK.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Identifying a rich message</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Rich messages in Applozic can be categorized into 4 types - Buttons, Images, Lists, and Cards. But before we get to them, let's talk about how to identify a rich message in our Applozic-powered chat app.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To send a rich message, we need to add the <em>contentType</em> property into the message's metadata to separate it from normal messages. The value of <em>contentType </em>is <em>300</em> for rich messages. Below is the metadata for all rich messages templates:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
    "contentType":"300"
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Apart from the content type, each rich message also has a <em>templateId</em>, which is different for different types of rich messages. The data of the rich message must be passed onto its payload. We will examine the payload for some types of rich messages as we discuss them in detail.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here is how a rich message is generally structured:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
    "message": "click on the buttons",
    "metadata": {
        "contentType": "300",//the content type is 300 for rich messages
        "templateId": "x",//x is a integer which differs for different types of rich messages
        "payload": "{//data pertaining to the rich message }"
    }
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Here are the different types of rich messages you can use in Applozic:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Buttons<ol><li>Link Buttons</li><li>Submit Buttons</li><li>Suggested Replies</li></ol></li><li>Images</li><li>List</li><li>Cards<ol><li>Generic Card</li><li>Card Carousel</li></ol></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>In this article, we’ll be examining the implementation of suggested replies, images, and generic cards into our chat application</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Suggested Reply Button</h2>
<!-- /wp:heading -->

<!-- wp:heading {"align":"center"} -->
<h2 class="has-text-align-center"><img width="478" height="143" src="https://lh5.googleusercontent.com/k0iUUADpby0I-DwcKPsyScbD49w6m2OV5C-yf-yVGS-BhnA5N56h0GzcKFuv8CbeQLtq8vtVb2QsqW-qcwB-nKj_-Y4jVgelpstIEeHguhth6zhD4CVisihiJSqK6mccmrr8ts2t"></h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Suggested Replies provide a way to send some common replies to messages with just a click, without having to type them out manually.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To render suggested replies on the UI, we need to send 2 fields in the payload:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><em>title</em>: display name for the button.</li><li><em>message</em>: A message that would be sent as a reply.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Here is a sample payload for suggested replies:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>"payload": &#91;{
   "title": "Yes",
   "message": "Cool! send me more."
   },
   {
   "title": "No",
   "message": "Not at all",
   }
}]
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>On clicking the button, a message would be sent with the text from the message property of the payload. To get the click outside the SDK you can use the following code. For Android, you will need to implement the <em>ALRichMessageListener</em>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>public class SampleApplication extends Application implements ALRichMessageListener//this can also be an activity or a fragment
{
  
   @Override
    public void onAction(Context context, String action, Message message, Object object) {
        if("Click" == action){
              String text = (String) object;//object will be the message text from the button
          //message will be applozic message object
          }   
    }
}
</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Here is the sample JSON for suggested replies:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>{
  "to": "userid",  
  // or
  // "groupId" : "groupId",
  "message": "Do you want more updates?",
    "metadata": {
        "contentType": "300",
        "templateId": "6",
        "payload": "&#91;{\"title\": \"Yes\",\"message\": \"Cool! send me more.\"},         {\"title\": \"No\",\"message\": \"Not at all\"}]"
    }
}
</code></pre>
<!-- /wp:code -->

##Continue this Tutorial on Applozic Blog!
We have the detailed tutorial with code snippets available on [our blog](https://www.applozic.com/blog/adding-rich-messaging-to-your-in-app-chat/) for you to continue with your integration!

[Click here](https://www.applozic.com/blog/adding-rich-messaging-to-your-in-app-chat/) to learn how to:
* Add images in chat
* Use Generic card templates
* Add lists in chat
* Use card carousels in chat