PhpBotFramework  1.0.0
A framework for Telegram Bots' APIs.
Functions | Variables
Multilanguage

Methods to create a localized bot. More...

Functions

 DanySpin97\PhpBotFramework\Bot::getLanguageDatabase ($default_language='en')
 Get current user language from the database, and set it in $language. More...
 
 DanySpin97\PhpBotFramework\Bot::getLanguageRedis ($default_language='en')
 Get current user language from redis, and set it in language. More...
 
 DanySpin97\PhpBotFramework\Bot::getLanguageRedisAsCache ($default_language='en', $expiring_time='86400')
 Get current user language from redis, as a cache, and set it in language. More...
 
 DanySpin97\PhpBotFramework\Bot::setLanguageRedisAsCache ($language, $expiring_time='86400')
 Set the current user language in both redis, sql database and $language. More...
 
 DanySpin97\PhpBotFramework\Bot::loadLocalization ($dir='./localization')
 Load localization files (JSON-serialized) from a folder and set them in $local variable. More...
 

Variables

 DanySpin97\PhpBotFramework\Bot::$language
 Store the language for a multi-language bot.
 
 DanySpin97\PhpBotFramework\Bot::$local
 Store localization data.
 
 DanySpin97\PhpBotFramework\Bot::$user_table = '"User"'
 Table contaning bot users data in the sql database.
 
 DanySpin97\PhpBotFramework\Bot::$id_column = 'chat_id'
 Name of the column that represents the user id in the sql database.
 

Detailed Description

Methods to create a localized bot.

Function Documentation

§ getLanguageDatabase()

DanySpin97\PhpBotFramework\Bot::getLanguageDatabase (   $default_language = 'en')

Get current user language from the database, and set it in $language.

Parameters
$default_languageOptional. Default language to return in case of errors.
Returns
Language set for the current user, $default_language on errors.

§ getLanguageRedis()

DanySpin97\PhpBotFramework\Bot::getLanguageRedis (   $default_language = 'en')

Get current user language from redis, and set it in language.

Using redis database we get language stored and the value does not expires.

Parameters
$default_languageOptional. Default language to return in case of errors.
Returns
Language for the current user, $default_language on errors.

§ getLanguageRedisAsCache()

DanySpin97\PhpBotFramework\Bot::getLanguageRedisAsCache (   $default_language = 'en',
  $expiring_time = '86400' 
)

Get current user language from redis, as a cache, and set it in language.

Using redis database as cache, seeks the language in it, if there isn't then get the language from the sql database and store it (with default expiring of one day) in redis. It also change $language parameter of the bot to the language returned.

Parameters
$default_languageOptional. Default language to return in case of errors.
$expiring_timeOptional. Set the expiring time for the language on redis each time it is took from the sql database.
Returns
Language for the current user, $default_language on errors.

§ loadLocalization()

DanySpin97\PhpBotFramework\Bot::loadLocalization (   $dir = './localization')

Load localization files (JSON-serialized) from a folder and set them in $local variable.

Save all localization files, saved as json format, from a directory and put the contents in $local variable. Each file will be saved into $local with the first two letters of the filename as the index. Access the english data as $this->local["en"]["Your key"]. File ./localization/en.json:

{"Hello_Msg": "Hello"}

File ./localization/it.json:

{"Hello_Msg": "Ciao"}

Usage in processMessage():

$sendMessage($this->local[$this->language]["Hello_Msg"]);
Parameters
$dirDirectory where the localization files are saved.

§ setLanguageRedisAsCache()

DanySpin97\PhpBotFramework\Bot::setLanguageRedisAsCache (   $language,
  $expiring_time = '86400' 
)

Set the current user language in both redis, sql database and $language.

Save it on database first, then create the expiring key on redis.

Parameters
$languageThe new language to set.
$expiring_timeOptional. Time for the language key in redis to expire.
Returns
On sucess, return true, throw exception otherwise.