polito-inginf / inginf_bot

A Telegram bot template to share university groups.
https://politoinginf.it/
GNU Lesser General Public License v3.0
7 stars 7 forks source link

New API calls needed in BotAPIfunctions.php #18

Open simocosimo opened 3 years ago

simocosimo commented 3 years ago

Todo

Description

We should be writing new functions (API call wrappers) in BotAPIfunctions.php. The aim should be writing functions with all possible parameters, paying attention to check for the required ones. Try to target only useful calls, no need to implement them all. Telegram Bot API documentation

Before start working on this, please comment with the function(s) you're currently coding, so we don't overlap work.

How to write functions

We should be writing functions in a way that will let us use different numbers of parameters very easily, without writing unreadable (and difficult to use) code. Bitmasks are a perfect tool for this. This necessity derives directly from the very high number of boolean parameters that the Telegram Bot API lets us use. We'll not use every single API method and we'll not use every single parameter that the API offers us, but when we do, we'll have the need to specify only some of the optional parameters in a very readable way.

Every wrapper of an API method, that allows the passing of optional (and, for us, useful) parameters, should take a $flag parameter, which default value should be 0 (zero). This parameter is of type int but the value inside will be considered as a binary number.

Function declaration: https://github.com/polito-inginf/inginf_bot/blob/c6a8fa61b5b78a768986413ed0f8ea2ead7f4ca2/basefunctions.php#L366

When calling the function from the outside, we will use something like this: $message = sendMessage($chat_id, $text, ENABLE_PAGE_PREVIEW | DISABLE_NOTIFICATION);

As you can see, the first 2 parameters are the required ones, while the third is optional, with a default value of zero. When specifying the $flags you can see we are making use of the bitwise OR operator applied to some constants defined in constants.php.

These constants are binary values, each of them representing a boolean feature that we want our function to use. In the example, we see we want to send the message enabling the link preview while not sending the user a notification. To combine features, it's necessary to use the | operator. Features constants look like this: https://github.com/polito-inginf/inginf_bot/blob/c6a8fa61b5b78a768986413ed0f8ea2ead7f4ca2/constants.php#L23-L26

Adding a constant is really easy:

Writing the functions, we have to handle the value generated by the bitmask passed as a parameter while also defining the constants allowed to be passed in the function itself. Take a look at this snippet of code: https://github.com/polito-inginf/inginf_bot/blob/c6a8fa61b5b78a768986413ed0f8ea2ead7f4ca2/basefunctions.php#L386-L399

Using the & operator (bitwise AND) we check the presence of the feature in the $flag parameter, this will let us be able to set internal variables correctly and make the right API call.

Main API calls to implement (constantly updating)

The following list is just for reference. If you find it useful to write different functions for the same API call, please tell us in the comment so we can discuss it.

simocosimo commented 3 years ago

I'm now working on the following functions:

These functions are already present in basefunctions.php but some modifications are needed. I'm on it!

Binco97 commented 3 years ago

I'm working on the following functions:

giulio-coa commented 3 years ago

I have completed the list of the API calls

giulio-coa commented 3 years ago

@atlas-ark have improved the sendMessage() API calls

simocosimo commented 3 years ago

Working on replyToMessage() method. This is not listed in the API calls to implement, but I think it's going to be useful. It'll be a sendMessage() wrapper with the required parameter of the $messageId to respond to.

giulio-coa commented 3 years ago

Working on replyToMessage() method. This is not listed in the API calls to implement, but I think it's going to be useful. It'll be a sendMessage() wrapper with the required parameter of the $messageId to respond to.

Maybe it can be a wrapper for the sendMessage()

Implement the changes into the sendMessage() The sendMessage() will have the $messageId parameter set to the default value (0), while the replyToMessage() will have it set to a specific value

simocosimo commented 3 years ago

Maybe it can be a wrapper for the sendMessage()

Implement the changes into the sendMessage() The sendMessage() will have the $messageId parameter set to the default value (0), while the replyToMessage() will have it set to a specific value

Took care of this in #24 with the last commit

giulio-coa commented 3 years ago

I'll work on the following functions:

Binco97 commented 3 years ago

I'll work on the following functions:

simocosimo commented 3 years ago

I'll work on the following functions: