Sending & receiving your first message

Elarian allows you to consume functionality from multiple service providers without the implementation overhead. Your code maintains the same semantics whether sending an email or chat, and this is possible as there's a unified sendMessage command regardless of the communication channel or provider.

Creating a channel

Let's send and receive Telegram messages. To do this, you have to set up a Telegram bot on the dashboard. We'll be doing this in the test environment, which means we don't need to configure any live bot tokens or configure live webhooks.

creeate telegram channel gif

Sending a message

Great! If you've got your bot, let's start by sending a message.

const client = new Elarian({
    orgId: 'your-org-id',
    appId: 'your-app-id',
    apiKey: 'your-api-key'
});
ā€‹
async function onConnected () {
  console.log('App is running!');

  const alice = new client.Customer({
    number: '+254712345678',
    provider: 'cellular'
  });

  await alice.sendMessage(
    {
      number: 'your-telegram-bot',
      channel: 'telegram'
    },
    { body: { text: 'Hello World!' } },
  );
}

client
    .on('error', (err) => console.error(`Connnection error: ${err}`))
    .on('connected', onConnected)
    .connect();

Before running this code, launch the phone simulator from the bottom left corner of the dashboard. Use a valid phone number to launch the simulator, and make sure to use the same number in the code you're about to run.

launch simulator gif

If the force is strong with you šŸ’„šŸ’Æ, you should have received a "Hello World" Telegram message on the simulator and a successful message logged to the console.

Receiving a message

Receiving messages is very straightforward. Set an event listener and handler like this:

const client = new Elarian({
    orgId: 'your-org-id',
    appId: 'your-app-id',
    apiKey: 'your-api-key'
});
ā€‹
async function onConnected () {
  console.log('App is running!');

  const alice = new client.Customer({
    number: '+254712345678',
    provider: 'cellular'
  });

  await alice.sendMessage(
    {
      number: 'your-telegram-bot',
      channel: 'telegram'
    },
    { body: { text: 'Hello World!' } },
  );
}

async function onReceivedTelegram(notification, customer) {
  console.log(`Received telegram message -> ${notification}`);
  ...
}

client
    .on('error', (err) => console.error(`Connnection error: ${err}`))
    .on('connected', onConnected)
    .on('receivedTelegram', onReceivedTelegram)
    .connect();

Elarian is opinionated on using this event-driven pattern, which is possible as Elarian sets up a persistent bi-directional connection between your application and the Elarian server. A considerable advantage you gain from this is not needing to configure webhooks or a HTTP server.

Go ahead and respond to the Telegram message you just sent using the simulator; this should invoke the received telegram notification handler. The customer parameter sent to the handler is a programmable object belonging to the customer that sent the message.