Multilingual SMS Translation Service built with Hapi, Nodejs and Javascript
Tags: webdev, javascript, beginners, programming
Published: 2023-08-07 09:01:48

Hey there! I've created this awesome service that makes sending bulk SMS messages super easy! But that's not all – it goes beyond the usual and lets you translate your messages into any language you want. How cool is that?

I've integrated the powerful features of OpenAI and Termii to make sure your messages get translated accurately and delivered on time. No more worrying about language barriers or missing out on reaching your audience!


##Prerequisites
Before starting, please ensure that you have Node.js and npm installed as prerequisites. If you don't have them already, you can download and install them from the official Node.js website: https://nodejs.org.
This service leverages the power of OpenAI and Termii to ensure seamless translation and efficient delivery of SMS messages.


##Setup
- Install Node.js and npm on your system.
- Clone this repository to your local machine.
- Navigate to the project directory and install the required 
  dependencies using npm install.
- Create a .env file in the root directory and add the necessary 
  environment variables:

```javascript
 const Hapi = require("@hapi/hapi");
const axios = require("axios");
const dotenv = require("dotenv");
dotenv.config();
```


##Package and tools
This documentation offers a comprehensive overview of the external API utilized in the project.

##OPENAI:
OpenAI is a dedicated artificial intelligence research laboratory focused on the development of safe and beneficial artificial general intelligence (AGI). To use their services, you can visit their official website and access the documentation at this link: (https://platform.openai.com/docs/introduction). By creating an account on their platform, you will gain access to an API key, which is essential for using their AI capabilities in your applications.
OpenAI was seamlessly integrated  into the app, empowering users to effortlessly translate messages from one source language to their desired destination language. This powerful integration added a valuable language conversion feature to our application, enhancing its versatility and usability.

```javascript
async function translateText(text, source_language, destination_language) {
  const apiKey = process.env.OPENAI_KEY;
  const apiUrl = "https://api.openai.com/v1/chat/completions";
  const data = {
    model: "gpt-3.5-turbo",
    messages: [
      {
        role: "system",
        content: "You are a helpful assistant that translates text.",
      },
      {
        role: "user",
        content: `Translate the following '${source_language}' text to '${destination_language}': ${text}`,
      },
    ],
    max_tokens: 150,
    n: 1,
    stop: null,
    temperature: 0.5,
  };

  try {
    // Make the request with a delay of 2 seconds
    await new Promise((resolve) => setTimeout(resolve, 2000));
    const response = await axios.post(apiUrl, data, {
      headers: {
        Authorization: `Bearer ${apiKey}`,
        "Content-Type": "application/json",
      },
    });
    const translation = response.data.choices[0].message.content.trim();
    return translation;
  } catch (error) {
    console.error("Error:", error.message);
    throw error;
  }
}
```


##Termii : 
Termii is a platform that helps different companies send alerts. It helps other businesses authenticate user identities and transactions with one-time passwords (OTP).
you can visit their official website and access the documentation at this link: (https://developers.termii.com). By creating an account on their platform, you will gain access to an API key, which is plugged into the applications.
After the message has been converted by openAI, we incorporated the Termii api into our app to deliver the message to the recipient.


```javascript
const request = require("request");
      convertedPhoneNumbers.forEach((convertedPhoneNumber) => {
        const data = {
          to: convertedPhoneNumber,
          from: "kokash",
          sms: translation,
          type: "plain",
          channel: "generic",
          api_key: process.env.TERMII_KEY,
        };
        const options = {
          method: "POST",
          url: "https://termii.com/api/sms/send",
          headers: {
            "Content-Type": ["application/json", "application/json"],
          },
          body: JSON.stringify(data),
        };

        request(options, function (error, response) {
          if (error) throw new Error(error);
          console.log(response.body);
        });
      });
```

##Axios:
Axios played a crucial role in our application, enabling us to effortlessly interact with external APIs. Its user-friendly API, promise-based design, and robust error handling capabilities streamlined the process of fetching and processing data from different sources. By acting as a reliable bridge, Axios ensured smooth communication and significantly improved the overall performance of our application.

##.env.example
environment variable offers all the configuration-related data needed.