Sunbird UCI
  • Overview of Sunbird UCI
  • 📙Learn
    • UCI use cases
    • UCI for Education(Case study)
    • Tech Overview
      • Glossary
      • High-level Architecture Design
      • Design Principles
      • Standards
  • 🚀Use
    • Choose your Persona
    • Adopter - Install and use UCI
      • Pre-requisites, Installation Setup, Post Setup
      • Setting up your very first conversation
      • API Documentation
      • Data Exhaust and Analytics
      • Posthog Event
      • Whatsapp (Netcore)
      • Environment Variables
    • Developer - Contribute to UCI
      • UCI Basics
        • XMessage Specification
        • Transformers
          • ODK Transformer
            • Interactive-Messages
            • Media Messages
            • Location
          • Broadcast Transformer
        • Adapters
          • Firebase Notification Adapter
        • User Segment
        • Schema Overview
          • UCI Core Schema
            • XMessage Schema
            • Assessment DB Schema
          • UCI API Schema
      • Development environment
        • Backend Setup
        • Setting up IDE
        • Environment variable setup
        • Debug services
        • Build and Execute UCI
        • Frontend Setup (Admin Console)
        • Frontend Setup (PWA)
        • Transport Socket
      • API Documentation
        • Bot Setup APIs
        • History APIs
        • Direct Message APIs
        • Vault APIs
      • Database Schema
        • Cassandra Xmessage Database
        • Postgres Forms Database
        • Postgres User Analytics Database
        • Postgres Comms Database
      • Contribution Guide
        • Your first PR
        • Contribute an Adapter
        • Adapter Implementation
        • Create a Transformer
    • Contact the administrator
  • ✅Releases
    • Release V2
  • 🤝Engage
    • Software License
    • Acceptable Use Policy
    • Terms of Service
    • Source Code
    • Discussion Forum
Powered by GitBook
On this page
  • 1. Overview
  • 2. Inbound Implementation
  • 2.1 Setup inbound
  • 2.2 Sample Code
  • 2.3 Existing Implementations
  • 3. Outbound Implementation
  • 3.1 Sample code

Was this helpful?

Edit on GitHub
  1. Use
  2. Developer - Contribute to UCI
  3. Contribution Guide

Adapter Implementation

PreviousContribute an AdapterNextCreate a Transformer

Last updated 1 year ago

Was this helpful?

1. Overview

A new adapter can be created using this . Once the adapter is created we can incorporate this apater in inbound and outbound service for receiving and sending message to user respectively.

Below is the process on how to implement a adapter in inbound & outbound.

2. Inbound Implementation

Inbound receives the messages from a channel, and uses the channel adapter to convert it to XMessage format. Once the messages is converted it will get processed.

When a new adapter is created we will have to add a class that will receive the messages from channel and use the adapter to convert this message to XMessage.

In the next section of the document, we will see the sample code for the same.

2.1 Setup inbound

Follow the steps below to setup the inbound repository to start working on it.

  1. Fork the below repository & clone it.

    https://github.com/samagra-comms/inbound/

  2. Checkout to the recent stable master branch.

  3. Open the project in any IDE spring/intellij.

  4. Follow the next steps.

2.2 Sample Code

Create a class for the channel & provider for the adapter created. Eg. NetcoreWhatsappController.

@Slf4j
@RestController
@RequestMapping(value = "/provider")
public class ProviderChannelController {

    @Value("${inboundProcessed}")
    private String inboundProcessed;

    @Value("${inbound-error}")
    private String inboundError;

    private ProviderChannelAdapter providerChannelAdapter;

    @Autowired
    public SimpleProducer kafkaProducer;

    @Autowired
    public XMessageRepository xmsgRepo;

    @Autowired
    public BotService botService;

    @Autowired
    public RedisCacheService redisCacheService;

    @Value("${outbound}")
    public String outboundTopic;

    @Value("${messageReport}")
    public String topicReport;

    @RequestMapping(value = "/channel", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public void providerChannel(@RequestBody WebMessage message) throws JsonProcessingException, JAXBException {

        providerChannelAdapter = ProviderChannelAdapter.builder().build();

        XMsgProcessingUtil.builder()
                .adapter(providerChannelAdapter)
                .xMsgRepo(xmsgRepo)
                .inboundMessage(message)
                .topicFailure(inboundError)
                .topicSuccess(inboundProcessed)
                .kafkaProducer(kafkaProducer)
                .botService(botService)
                .redisCacheService(redisCacheService)
                .topicOutbound(outboundTopic)
                .topicReport(topicReport)
                .build()
                .process();
    }
}

Once the class is create, we can use the link http://host:port/provider/channel to receive messages from user. A user will receive a message in below format.

{
    "messageId": "67630c26-e29e-46ae-acb6-756704362d84",
    "text": "Hi",
    "from": "98********"
}

A sample api to receive messages from user is given below.

curl --location --request POST 'localhost:8085/provider/channel' \
--header 'Content-Type: application/json' \
--data-raw '{
    "messageId": "67630c26-e29e-46ae-acb6-756704362d84",
    "text": "Hi",
    "from": "98********"
}'

2.3 Existing Implementations

Below are some of the existing implementation code of currently available adapters.

3. Outbound Implementation

Outbound converts the xMessage to the one that will be sent to the channel(sms/whatsapp). It will then be sent to the network provider who will send it to the channel using the implementation done in adapter.

3.1 Sample code

Add code to return adapter object based on the channel & provider set in XMessage.

if(provider.toLowerCase().equals("specifiedProvider") && channel.toLowerCase().equals("specifiedChannel")){
    return ProviderChannelAdapter.builder().build();
}

Once the code is added, we should be able to use the new adapter to sent messages to user.

Outbound uses this class to send messages to user. There is class called which uses the channel & provider set in the XMessage and return the adapter object which will be used to call the processOutBoundMessageF method.

🚀
link
Netcore Whatspp
Gupshup Whatsapp
UCI Web Channel
OutboundKafkaController
ProviderFactory