Sending and Receiving WhatsApp Chats

Interactive Tutorials are currently not supported on mobile devices. To ensure an enhanced experience, kindly access this tutorial from a laptop/desktop browser instead.

Establishing a connection

const { Elarian }  = require('elarian');

const client = new Elarian({
  orgId: 'YOUR-ORG-ID',
  appId: 'YOUR-APP-ID',
  apiKey: 'YOUR-ORG-API-KEY',
});

client
  .on('error', (error) => {
    console.log('Connection failed with error...', error);
  })
  .on('connected', () => {
    console.log('App is connected');
  })
  .connect();

Sending your first message

const testCustomer = new client.Customer({
    provider: 'cellular',
    number: 'PHONE-NUMBER-FROM-SIMULATOR'
});

testCustomer
  .sendMessage(
    { number: 'YOUR-WHATSAPP-BUSINESS-NUMBER', channel: 'whatsapp' },
    {
      body: {
        text: 'Hello World!'
      },
    },
  )
  .then(console.log)
  .catch(console.log);

Receiving notifications

client.on('receivedWhatsapp', (notification, customer) => {
  console.log('Received Notification -> ', JSON.stringify(notification));
  customer
    .sendMessage(
    { number: 'YOUR-WHATSAPP-BUSINESS-NUMBER', channel: 'whatsapp' },
    {
      body: {
        text: 'Thanks for texting back :) You rock!'
      },
    },
  )
  .then(console.log)
  .catch(console.log);
});