(EN) Dev Guide

This guide is meant to be complementary to the Quick start for service owners guide. It is recommended to read it before this document. It offers a description of the most relevant features and options provided via the two core configuration files. Updated to version R32. Please check the Updates from version R34 to see what's new.

ZWEB telegram booking bot

A simple, fast, hassle-free reservation tool helping structures and services for rent by the hour. Made with love by zorida @ zweb.studio. For any suggestions and issues, take a look at the small forum too (https://forum.zweb-apps.xyz)

Requirements

Preliminary operations

This is a program meant to be associated with a Telegram bot. In order to create a bot, you must use the bot named Telegram Botfather. We encourage you to take a look at https://core.telegram.org/bots/tutorial or surf the web and get a basic understanding of what is a bot.

Once you create the bot in Telegram, you need to copy the bot's API token and put it in a .env file in the project root folder. To do it, first of all duplicate the .env.dist file, and rename it as .env. The application will read this file automatically (and needs it) when it runs. Paste the API token string inside the TOKEN variable of the env file (in the double quotes at the right of the equal sign).

The program communicates with the Telegram Bot API by using the polling method (mainly meant and recommended for development) or the webhook method. If you set the .env ENVIRONMENT variable as equal to "DEV" in the env file, polling will be used, otherwise webhook will be used. When using a webhook, you must set a WEBHOOK variable in the env file, where you basically tell Telegram how to reach your application, for example https://mysite.com/bot.php. This depends on your hosting and/or web server configuration. The program will try to set the webhook for you, but if you notice that the webhook is not set, you can manually send a HTTP POST message to Telegram API according to the documentation https://core.telegram.org/bots/api#setwebhook. Remember: for the webhook method to work, it is mandatory that your hosting machine has a full chain SSL certificate (either third-party or self-signed) associated to your domain, and the communication must travel the web via HTTPS, or telegram will silently ignore the messages.

Using a webhook you can also decide to use a SECRET text (likely not an easily guessed text), by writing it in the respective .env variable named SECRET; this string is useful to avoid messages coming from outside the bot, and works only when you are in a production environment (i.e. with a webhook, have a look at the Telegram bot API to set the proper communication method with the API), not during development.

To start developing or testing the application

Run composer install in a terminal from the main folder of the project. Create the .env file from the .env.dist file as told before, and set the value "DEV" for the ENVIRONMENT (polling mode).

Create a bot in Telegram and copy/paste the token api in the env TOKEN variable.

Run php bot.php in the command line from the root folder of the project, where the bot.php file is (it requires the 'php' command which, depending on the OS and the specific php installation you are using, may or may not be available globally and automatically: try typing php -v in the terminal to check it, otherwise you should probably add the php executable to your OS PATH or create a link between the command and the executable. There are quite a number of tutorials online explaining PHP installation in depth).

The program is instructed to use a config file to get all the necessary setup. Here, you can customize a lot of things we'll discuss in detail later. The config file is in the project root folder and is by convention named config-xx.json, where 'xx' is replaced with the language code, defined in the .env file. At the current time, 'en', 'es' and 'it' languages are already added to the project, but you can create your own by copying one and customize it, or editing the default configuration file, i.e. config-en.json (better making a backup copy of it). All the human-readable data moving back and forth through the application - such as a thank you message - will appear in that language.

Project structure

The main PHP files are all located in the root folder of the project. They handle all the most important actions involved in communicating with the database, the Telegram Bot API, and so on. Here you will also find the demo config files in english and spanish to help you get started.

The /assets folder will be useful if you want to put custom files for ticket images, fonts, text etc.

Since the bot communication is almost stateless, there are folders like /extras, /reservations and /temp that will be used to store small information files, required to keep track of what is happening, as well as the last excel files generated.

Libraries installed via composer from the command line will be under /vendor folder. It is recommended not to edit this files directly, but rather extend their classes and functionalities outside this folder.

The config file

This file in JSON format provides all the configurable parameters of the bot application, here is the explanation. Some of the JSON properties, especially in the text object, refer to translation strings and are almost self-explanatory, so they won't be shown here. However, it is important to remind that in the text property group, some translation strings contain special character sequences like ::s that the software will replace at runtime with the appropriate data if it finds them. Other config properties or groups of properties (i.e. 'objects') have a significant impact on the application behaviour instead. We suggest to begin with modifying just a few properties, and see the results reflected in the bot behaviour.

Main parameters

How do you discover your telegram account number? Here is two ways to get it:

  1. The easier way. Start the bot in polling/development mode, complete the signup or a reservation, then connect to the file database reservations.sqlite and recover the record with your ID!
  2. The complicated way (from Reddit). Talk to @BotFather in Telegram, and create a bot with some random username. Send it a message, and open the following link https://api.telegram.org/bot[--TOKEN--]/getUpdates. Replace [--TOKEN--] with the bot-token that you got from @BotFather. The page will contain the information of your message to the bot. It should say, amongst a whole lot of other info, ... { message: { ... "from": {... "id": 0123456789 ...} ... } }

User roles

It is possible to have a list of user roles in the configuration file, i.e. an array in JS object notation. At the moment, each role object contains only two properties:

The 'time' object

The service objects

Each service may use a number of properties, some of which are optional.

Text and button commands

Media files

You can make further customizations to your application by modifying the media files. There are two media files that you can use: one is the reservation ticket. The application code is packed with a sample blueprint of a ticket, located in assets/ticket_blueprint_generic.png. You can change it to your liking, but since the code will try to print text on top of it, it is recommended to use an image file of the same (or similar) size for the text to fit in the image canvas. You can also have a small overprinted logo on the ticket (appearing on the top right).

The info command, available for all users, is meant to provide general information about your bot. You can put text in simplified html format (only 'inline' tags like b, i, pre are allowed in Telegram) in the help config variable. At the current stage of development, you can also associate an image or a text file if you need to show something different. all you have to do is create that file, and put the file path relative to the project root in a config variable named infographics. Text will be shown inside 'pre' tags, so that it will ben monospace (this is useful if you want to shows some ASCII tables like the example in assets/info.txt but be careful: Telegram has a limit of horizontal characters, then the text will be shown in a new line); instead, the image allows for more flexibility, although it is recommendable to keep the file size as small as possible, so the user will receive a faster response.

Shift rules

In order to show availabile reservation times to a user, the application tries to apply a hierarchy of rules that determine if a certain time of the day is available for reservations or not. Given a specific day of the week, as chosen by the user in the booking process, the code will:

  1. Look for rules of time shifts associated with that day of the week and service, and currently active; these rules are stored in the database.
  2. Look for general rules of time shifts related to that day; these rules are stored in the database.
  3. Look for the general working shifts specified in the config file for that service.
  4. Look for the general working shifts specified in the config file for ANY service.
  5. Make all times of the day available, i.e. 00:00 -> 23:59.

You can specify all of the first four or even none of these types of rules. The fifth rule is a fallback rule set directly inside the code to avoid errors.

Connecting to Google Calendar API

To add reservations as events to a Google Calendar, even if the calendar is not associated with a Google Workspace account, this bot uses Google service credentials. Please follow these steps:

  1. Go to the Google Cloud Console.
  2. Create a new project (or select an existing one).
  3. Enable the Google Calendar API for the project.
  4. Go to APIs & Services > Credentials and create a new credential of type Service Account.
  5. When creating the service account, make sure to download the JSON file with the credentials.
  6. Save the credentials in a folder of the bot software (create a new one if you wish to)
  7. Go to the Calendar (it may belong to any account, not just the one where you activated the G Calendar API) with the service account email (e.g. service-account-name@project-id.iam.gserviceaccount.com), giving it permission to Add events or Edit events.
  8. Put the account email and the relative path to your credentials (without trailing slash) in you ENV variables.
  9. Start the bot and book a service :)