Virtual Bar Restaurant in a Telegram Group

Telegram-Bar-Chat-Bot-1.jpg

Creating a virtual bar or restaurant using a Telegram chat group and a Telegram bot.

The Telegram bot uses the Node.js telebot package to create a virtual bar owner that will take orders, record them, deliver them to the chat group and on request display the bill.

The bot itself is pretty simple and just uses preset messages based on keyboard requests from the user.

Bot Order Options

Orders are stored in a database and are sent to the group based on the time they were ordered and the type of order. Drinks are sent after 3 minutes and food after 10 minutes.

function serve(type, serve_time) { // drink or food, minutes delay before serving
 order_ready = Date.now() - (1000*60*serve_time); 
 const stmt = db.prepare('SELECT * FROM orders WHERE type=? AND served=? AND timestamp<?');
 const order = stmt.get(type, 'no', order_ready);
 if (typeof order !== 'undefined'){
    const update = db.prepare('UPDATE orders SET served = ? WHERE id = ?');
    const info = update.run('yes', order.id);
    bot.sendMessage(groupid, emojis[order.item] + ' for ' + order.person);
 }
}

// service interval - one item per call
setInterval(serve, 5000, 'drink', 3); // every 5s, check for drink orders over 3 mins old
setInterval(serve, 5000, 'food', 10); // every 5s, check for food orders over 10 mins old


This is uses a Node.js library telebot.js to run the chat bot and another Node.js library better-sqlite3 to record the interactions between the bot and the humans in the group chat.

Create a Telegram Bot

If you don’t have a Telegram account you need to create one first https://telegram.org/

After logging in to the web interface go to https://telegram.me/botfather and click ‘OPEN IN WEB’

Click ‘START’ and you will see the available commands. You can also type /start in the Telegram web interface to see these commands again.

Type /newbot to create a new bot and follow the instructions. Copy the token and save it somewhere. Also make a note of the link for the new bot chat. Something like t.me/yourbotname_bot

Type /setuserpic to send media for the avatar for your new bot.

Create a Telegram Group

From the menu, choose ‘New Group’. Click your name in the list and then type to search @thenameofyour_bot and when it appears click it as well to also add it to the group and click NEXT. Choose a name for the group and click ‘CREATE GROUP’.

Select the new group in the web interface and click its name at the top:

Group Name

In the Group info screen. Click Upgrade to supergroup and click OK.

Group Info Screen
Group Info Screen

Again click the name at the top to open the info screen and click the camera screen to add an image for the group.

In the same Group info screen, click Invite members. In the search box, type @rawdatabot and select it to add to the group.

A message will appear from Telegram Bot Raw. Copy the group chat id including the minus sign somewhere for later.

Group Chat ID

Open the the Group info screen once more and copy the t.me link shown somewhere for later. Remove Telegram Bot Raw from the group.

DigitalOcean Node.js Droplet Set Up

I used a Droplet at DigitalOcean to set up the bot. You can use any host that supports Node.js. I originally tried this within cPanel at another host and while telebot installed, it wasn’t possible to install better-sqlite3.

Set up an account at DigitalOcean and create a new project in your admin area.

Navigate to the Node.js Quickstart Droplet page https://cloud.digitalocean.com/marketplace/5df3a5a374ec0f0d4d2c0085?i=3d43d1 Click the Create Node.js Quickstart Droplet button.

Select the following settings:
Standard
$5/mo
Any datacenter near you
Authentication – One-time password
Enable backups if this is more than a temporary project

Click the Create Droplet button and wait for it to start up. Open the Console window.

DigitalOcean Access Console

DigitalOcean will have sent a password to your email. You will be asked to change this when you first log in.

I prefer to do the bare minimum in a text console so I use WinSCP to manage file editing and uploads. Use the details in the project with the new password to create a New Site in Winscp

Winscp Account Creation

Download the tele-chat-bot.js and database.db file from https://github.com/robotzero1/telegram-virtual-bar and edit the settings shown at the top. In WinSCP copy the database file and the tele-chat-bot.js file to the root directory.

Winscp Files

If you want to edit the database I used DB Browser for SQLite to create it.

In the Console window paste and submit the following three commands one at a time:

npm install telebot --save
npm install better-sqlite3 --save
node tele-chat-bot.js

Starting Chat

Your bot and the group are now ready. Before anyone can interact with the new bot you need to send them the link to the your new bot (as given by the BotFather similar to t.me/yourbotname_bot ). They have to click START and they will be invited to order something or proceed to the chat group.

Start Button
Chat Begin

If you intend to make this available to the public you should consider following steps 3 and onward in this tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04

Resources

Github Repository: https://github.com/robotzero1/telegram-virtual-bar
TeleBot – Node.js Telegram Bot: https://www.npmjs.com/package/telebot
Better-sqlite3 – Node.js SQLite3 database: https://www.npmjs.com/package/better-sqlite3
DigitalOcean Droplet: https://m.do.co/c/801f9f581abd . Supposedly this link gives a $100 in credit over 60 days
Emoji List: http://www.unicode.org/emoji/charts/full-emoji-list.html#1f355
Telegram Raw Data for Group – https://stackoverflow.com/questions/32423837/telegram-bot-how-to-get-a-group-chat-id

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top