Contribute an Adapter
1. Overview
Adapters convert information provided by channels (SMS, Whatsapp) for each specific provider to xMessages and vise versa.
2. How to setup adapter
Setup the adapter repository to start working on it. Follow the steps given to add a new adapter & test it.
Fork the below repository & clone it.
https://github.com/samagra-comms/adapter/
Checkout to the latest stable master branch.
Open the project in any IDE spring/intellij.
Follow point 3 to create a new adapter.
Write test cases for inbound method
convertMessageToXMsg
& outbound methodprocessOutBoundMessageF
.A simple example to test the send text message is given in the file.
3. Creating your own Adapters
All adapters are named as <ProviderName><ChannelName>Adapter
; for example GupshupWhatsappAdapter. Adapters should extend AbstractProvider
and implement IProvider
. Thus, it needs to implement the following methods:
public Mono<XMessage> convertMessageToXMsg(Object msg) // Converts API response object to XMessage
public Mono<XMessage> processOutBoundMessageF(XMessage nextMsg) //Converts XMessage object to API response and call it.
These methods are called by inbound
and outbound
services internally to process the incoming and outgoing messages.
All adapters with the above implementation will be valid. An example adapter can be found here.
3.1. Incoming content
Currently we accepts text/media/location/quickReplyButton/list messages. It should be implemeted according to the network provider(Netcore/Gupshup) documentation.
To enable these, we have to add them in convertMessageToXMsg
and convert these according to the xMessage spec
3.2. Outgoing Content
We can send a text message to user. This is the basic usage of an adapter. When creating a new adapter we should start with this scenario. A sample code for the same is also added here.
We can also send media/location/quickReplyButton/list messages to user. As we are using the ODK forms, we use bind::stylingTags, bind::caption to show the media file or to show the select choices as list/quick reply buttons. There are a few constraints which will be applied with the quickReplyButton/list content.
Below are the few styling tags we have allowed for now.
image //To send image file
audio //To send audio file
video //To send video file
document //To send document file
buttonsForListItems //To send choices as quick reply buttons
list //To send choices as list
To send this type of content to user, adapter should implement it according to the network provider(Netcore/Gupshup) documentation.
4. Adapter Credentials
To make a bot use this adapter we will have to later add this adapter configuration in databse using the create adapter API. This adapter may require you to have some credentials, which can be stored in a vault service. Click here to see how to add a secret in vault.
5. Sample Code
Sample code to receive a text message from channel & reply to channel.
Create a new provider class which implements IProvider.
Implement convertMessageToXMsg method for converting incoming message from channel to XMessage format.
Implement processOutBoundMessageF method to reply to channel by converting XMessage format to channel message format.
Create a web service class that handles api which is used to reply to channel.
Create a class for format which is accepted by reply to channel api Eg. http://example.com/replyMessageToChannel.
Create a message format class for OutboundMessage class property.
Create a class that accepts response from channel reply api.
Create a class that accepts format for message received from channel.
If we want to fetch the credentials of the adapter from vault service, see the code of FirebaseNotificationAdapter to check how to do this.
6. Adapter implementation
After the adapter is created we should be able to implement it in inbound & outbound service for receiving and sending messages to user. Click here to see the process & sample code on how to to do this.
7. List of Existing Adapter Implementations
Last updated