In this article, I will show how you can create an Echo Bot using Corvid by Wix. It’s quick, easy, and absolutely FREE.

Most modern messaging apps have the ability to write bots, and they work in a pretty similar way. So, in this post, I’ll show you an example only for Telegram Messenger. You will get the idea of how it might work with other messengers.

Let’s create a simple bot that will reply with the same message it gets. Let’s call it EchoBot.

This is what the flow we need to implement looks like:

Message-server workflow

Message-server workflow
  1. A user sends a message to EchoBot.
  2. A server takes this message and creates all the needed logic.
  3. The server responds to the user’s action by sending a message using the Telegram API.

Before we dive into these steps, you need to create your Telegram bot. To create bots, Telegram offers BotFather, so follow the instructions and get your own bot token.

Also, you need a server. We will use a Wix site with Corvid because it’s easy and free. Go create a Wix Site and enable Corvid (follow steps one and two in the article).

You may also like:
State Management in Corvid.

Step 1

To implement step one in our flow, we need to create a connection between the Telegram bot and your Wix site (backend side). We will use HTTP-functions for this. To create an HTTP function, add a file named http-functions.js to the backend of your site. In this file, you can find an example function. Empty this file and write the following function:

After publishing the site, your endpoint will be available at:

https://{user_name}.wixsite.com/{site_name}/_functions/updatesLisner

Or, for premium sites, at :

https://www.{user_domain}/_functions/updatesLisner

Next, you need to set this endpoint as a webhook to your bot. To do this, you need to make an HTTP GET request. You can do this from browser:

https://api.telegram.org/bot<token>/setWebhook?url=<endpoint_url>

You should get a response like this:

 {  "ok": true,  "result": true,  "description": "Webhook was set" } 

After that, step one is done!

To check functionality, open site monitoring and send a message to your bot. In our function, we log every update, so you should see logs:

Real-time errors and logs

Real-time errors and logs

Step 2

Simplifying the example logic of our bot is pretty simple: making echo. Let’s extract message text from an update. According to Update Object Specs:

 const text = update.message.text; 

So, we have text, which we need to send back in a chat.

Step 3

Now, we need to respond to the user with sendMessage. Create this function in backend/http-functions.js:

This function just makes a POST request with two parameters in its body:

1. text.

2. chat_id.

We already have text from Step two. We can get chat_id from the update object:

 const chatId = update.message.chat.id; 

Let’s complete our endpoint function with sendMessage. Finally, our code will look like this:

Finally, publish your site and send a message to your bot!

Note: creating bots with Corvid by Wix is a very scalable solution. You can use Wix Data to store data for your bot. Just create a collection and you are ready to go.

Also, you can create an admin page with a UI by simply using a drag & drop editor to view some statistics or other elements.

Further Reading

This post originally appeared on Medium.



Source link

Write A Comment