#257

Product support

Get help in the plugin support forum.

Categories

  • Developer Tools
  • Social
  • Utility

Make a Telegram bot in 3 easy steps:

  1. Create your bot via BotFather and set webhook address

  2. Install this plugin and then add in Settings API key from BotFather

  3. Make a few Steps in plugin menu, then create a Dialog from these steps

Your can create any bot using this plugin. Example of fully conversational bot which calculating distance in documentation.

Setup and usage

About

The plugin uses irazasyed/telegram-bot-sdk. Documentation for all methods which you can use in steps is there.

Variables and functions in steps:

Variable Using for
$user_id ID of Telegram user
$user_name Name of Telegram user
$user_message Message of Telegram user
$chat_id Chat ID where message from
$next_dialog Jump to another dialog
$next_step Jump to another step in current dialog (if you want to jump to exact step in exact dialog, use both variables)
$user Object of User model class
$telegram Object of Telegram\Bot\Api class
Function Using for
set_variable($variable_name, $variable_value, $user) You can set custom variable for user
get_variable($variable_name, $user) You can get variable that you have set
delete_variable($variable_name, $user) You can delete variable that you have set

Tutorial:

Create a new bot via BotFater.

There are a lot of tutorials on how to do it.

Set a webHook.

To set webHook just go to this address:

https://api.telegram.org/bot123456789:ABCDIFGHIGKLMNOpqrstuvwxyz/setWebhook?url=https://example.com/eugenetolok/bot/

Where

  1. "123456789:ABCDIFGHIGKLMNOpqrstuvwxyz" is example token, so replace it with your own token.
  2. https://example.com/eugenetolok/bot/ - example bot url. If you didn't change routes.php, just replace domain.

Note: you need ssl enabled on your site to use webhooks in this plugin!

Add API key to settings

Go to the settings and type in the field API key your token. There you can also write the "welcome message" which user will see only once after /start.

Make your first three steps:

  1. Press on step and then press button "Create"
  2. Name this step. I recommend to name it like: "Home: Send main keyboard". Where "Home" is dialog name.
  3. Insert php code:
    $share_location_button['text'] = 'Share location';
    $share_location_button['request_location'] = true;
    $share_location_keyboard = [
    [(object)$share_location_button]
    ];
    $share_location_markup = $telegram->replyKeyboardMarkup([
    'keyboard' => $share_location_keyboard, 
    'resize_keyboard' => true, 
    'one_time_keyboard' => true
    ]);
    $telegram->sendMessage([
    'chat_id' => $chat_id, 
    'text' => "Share your location to find a distance between cities",
    'reply_markup' => $share_location_markup
    ]);
  4. Save and close Do the same with two other steps, but insert other php codes:

    if (null !== $telegram->getWebhookUpdates()->getMessage()->getLocation()->getLatitude() && null !== $telegram->getWebhookUpdates()->getMessage()->getLocation()->getLongitude())
    {
    set_variable("latitude",$telegram->getWebhookUpdates()->getMessage()->getLocation()->getLatitude(),$user);
    set_variable("longitude",$telegram->getWebhookUpdates()->getMessage()->getLocation()->getLongitude(),$user);
    
    $category_keyboard = [
            ['Moscow'],
            ['Los Angeles']
        ];
    
    $category_reply_markup = $telegram->replyKeyboardMarkup([
            'keyboard' => $category_keyboard, 
            'resize_keyboard' => true, 
            'one_time_keyboard' => true
    ]);
    
    $telegram->sendMessage([
        'chat_id' => $chat_id, 
        'text' => "Ok, choose a place!",
        'reply_markup' => $category_reply_markup
    ]);
    }
    else
    {
    $share_location_button['text'] = 'Share location';
    $share_location_button['request_location'] = true;
    
    $share_location_keyboard = [
        [(object)$share_location_button]
    ];
    
    $share_location_markup = $telegram->replyKeyboardMarkup([
        'keyboard' => $share_location_keyboard, 
        'resize_keyboard' => true, 
        'one_time_keyboard' => true
    ]);
    
    $telegram->sendMessage([
        'chat_id' => $chat_id, 
        'text' => "Didn't understand location... Try again!",
        'reply_markup' => $share_location_markup
    ]);
    $next_step = 2;
    }

    And:

    if($user_message == "Moscow")
    {
    $distance = distance((float)get_variable("latitude", $user), (float)get_variable("longitude", $user), (float)"55.7558", (float)"37.6173");
    $telegram->sendMessage([
    'chat_id' => $chat_id, 
    'text' => "You are " . $distance . " m from Moscow"
    ]);
    }
    else
    {
    $distance = distance((float)get_variable("latitude", $user), (float)get_variable("longitude", $user), (float)"34.0522", (float)"118.2437");
    $telegram->sendMessage([
    'chat_id' => $chat_id, 
    'text' => "You are " . $distance . " KM from Los Angeles"
    ]);
    }
    function distance($lat1, $lon1, $lat2, $lon2) {
    $theta = $lon1 - $lon2;
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
    $dist = acos($dist);
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515;
    return ($miles * 1.609344);
    }
    delete_variable("latitude", $user);
    delete_variable("longitude", $user);

Create your first dialog

Name it and then add steps in order from this tutorial.

Set default Dialog ID and Step ID (in our case it is 1 and 1).

We are finished! Our bot calculating the distance between you and Moscow or Los Angeles. As you can see you can do whatever you want with Easy Telegram Bot and October CMS and php libraries!

  • Found the plugin not useful on 19 Feb, 2017

    the plugin doesn't work!

  • author

    Replied on 20 Feb, 2017

    Hi, Daniel! Please check your ssl certificate. Five minutes ago I've installed pure octobercms on pure hosting with lentsencrypt certificate and it works good. Also try to change your webhook from https://yoursite.com/eugenetolok/bot to https://yoursite.com/bot/v3. If you have any problem, please connect with me by the mail: [email protected] Good Luck!

  • Found the plugin useful on 18 Feb, 2017

    How can i use it for automaticaly send message to telegram user that new post has added

  • author

    Replied on 20 Feb, 2017

    Hi, Just Dior! Now it is just dialog bot, but I will add sending method this week!

1.0.29

Added new columns

Apr 22, 2017

1.0.28

Updated table eugenetolok_telegram_users

Apr 22, 2017

1.0.27

Updated table eugenetolok_telegram_users

Apr 22, 2017

1.0.26

Updated table eugenetolok_telegram_users

Apr 22, 2017

1.0.25

Updated table eugenetolok_telegram_users

Apr 22, 2017

1.0.24

Updated table eugenetolok_telegram_users

Mar 16, 2017

1.0.23

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.22

Drop table eugenetolok_telegram_variables

Feb 05, 2017

1.0.21

Updated table eugenetolok_telegram_variables

Feb 05, 2017

1.0.20

Created table eugenetolok_telegram_variables

Feb 05, 2017

1.0.19

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.18

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.17

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.16

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.15

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.14

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.13

Updated table eugenetolok_telegram_users

Feb 05, 2017

1.0.12

Drop table eugenetolok_telegram_dialogs_steps

Feb 05, 2017

1.0.11

Updated table eugenetolok_telegram_dialogs_steps

Feb 05, 2017

1.0.10

Updated table eugenetolok_telegram_dialogs_steps

Feb 05, 2017

1.0.9

Updated table eugenetolok_telegram_dialogs_steps

Feb 05, 2017

1.0.8

Created table eugenetolok_telegram_dialogs_steps

Feb 05, 2017

1.0.7

Updated table eugenetolok_telegram_dialogs

Feb 05, 2017

1.0.6

Created table eugenetolok_telegram_steps

Feb 05, 2017

1.0.5

Created table eugenetolok_telegram_dialogs

Feb 05, 2017

1.0.4

Created table eugenetolok_telegram_users

Feb 05, 2017

1.0.3

Drop table eugenetolok_telegram_users

Feb 05, 2017

1.0.2

Created table eugenetolok_telegram_users

Feb 05, 2017

1.0.1

Initialize plugin.

Feb 05, 2017