Yandex.Alisa and Telegram bot in PHP with the same functionality

Good afternoon.

There are a lot of articles on the topic of Telegram bots, but few people write about skills for Alice, and I didn’t find any information on how to make a single bot, so I decided to share my experience on how to make a simple Telegram bot and the Yandex.Alice skill for the site having the same functionality.

So, I won’t tell you how to raise a web server and get an ssl certificate, enough has been written about it.

Creating a Telegram bot

First, let's create a Telegram bot, for this we go to Telegram and find the BotFather bot there.

Yandex.Alisa and Telegram bot in PHP with the same functionality

Yandex.Alisa and Telegram bot in PHP with the same functionality

Choose /newbot

Yandex.Alisa and Telegram bot in PHP with the same functionality

We enter the name of the bot by which it will respond, then we enter the name of the bot, in response we get a token to control the bot, we write down this key, it will be useful to us in the future.

Yandex.Alisa and Telegram bot in PHP with the same functionality

The next step is to tell the Telegram servers which server to send data from the bot to. To do this, we make a link of the form:

https: //api.telegram.org/bot___ТОКЕН___/setWebhook?url=https://____ПУТЬ_ДО_СКРПИТА___

___TOKEN___ we replace with our token from the bot, received earlier

____PATH_TO_SCRIPT____ we replace with the address of the script on our server where the data will be processed (for example, www.my_server.ru/webhook_telegram.php).

There is a problem here, the api.telegram.org server is under blocking, but you can do this: rent the cheapest server where there are no restrictions and give the command from the console of this server

wget ___ПОЛУЧИВШИЙСЯ_АДРЕС___

That's it, the Telegram bot is created and connected to your server.

Creating a skill for Yandex.Alisa

Let's move on to creating a skill for Yandex.Alice.

To create a skill, you need to go to the Yandex.Dialogues developers page Yandex.Dialogs developer page, click there "Create dialogue" and select "Skill in Alice".

Yandex.Alisa and Telegram bot in PHP with the same functionality

The skill settings dialog will open.

Yandex.Alisa and Telegram bot in PHP with the same functionality

We begin to enter the skill settings.

Enter the name of your skill.

Yandex.Alisa and Telegram bot in PHP with the same functionality

The activation name should be chosen very carefully so that Alice understands it correctly, from the nuances - a mobile application with Alice and columns like Yandex.Station or Irbis A can perceive words differently.

We enter the path to the script on our server in the same way as for Telegram, but it will be a script specifically for Alice, for example www.my_server.ru/webhook_alice.php.

Yandex.Alisa and Telegram bot in PHP with the same functionality

We choose the voice with which the skill will speak, I like Alice's voice more.

Yandex.Alisa and Telegram bot in PHP with the same functionality

If you plan to work only on mobile devices or in a browser, then select "You need a device with a screen."

Next, enter the settings for Alice's skills catalog. If you plan to use the word "brand" for activation, you need to verify the brand's website in the webmaster.yandex.ru service.

Yandex.Alisa and Telegram bot in PHP with the same functionality

That's all with the settings, let's move on to the scripts.

Telegram bot script

Let's start with a script for Telegram.

We connect the library where messages from the bot and Alice will be processed:

include_once 'webhook_parse.php';

We set the token of our bot:

$tg_bot_token = "_____YOUR_BOT_TOKEN_____";

We receive data:

$request = file_get_contents('php://input');
$request = json_decode($request, TRUE);

Parsing the data into variables:

if (!$request)
{
  die();
    // Some Error output (request is not valid JSON)
}
else if (!isset($request['update_id']) || !isset($request['message']))
{
  die();
    // Some Error output (request has not message)
}
else
{
  $user_id = $request['message']['from']['id'];
  $msg_user_name = $request['message']['from']['first_name'];
  $msg_user_last_name = $request['message']['from']['last_name'];
  $msg_user_nick_name = $request['message']['from']['username'];
  $msg_chat_id = $request['message']['chat']['id'];
  $msg_text = $request['message']['text'];


  $msg_text = mb_strtolower($msg_text, 'UTF-8');


  $tokens = explode(" ", $msg_text);
}

Now you can work with variables:

$tokens - here are now all the words that the user entered

$user_id - user id here

$msg_chat_id - chat in which the bot received the command

$msg_user_name - username

Next, we call the Parse_Tokens function for processing:

$Out_Str = Parse_Tokens($tokens);

And send a response:

Send_Out($user_id, $Out_Str);

the Send_Out function is simple and looks like this:

function Send_Out($user_id, $text, $is_end = true)
{
  global $tg_bot_token;
  if (strlen($user_id) < 1 || strlen($text) < 1) {return;}
  $json = file_get_contents('https://api.telegram.org/bot' . $tg_bot_token . '/sendMessage?chat_id=' . $user_id . '&text=' . $text);
}

Skill script for Yandex.Alisa

Now let's move on to the script for Alice, it is almost the same as for Telegram.

We also connect the library where messages from the bot and Alice will be processed, plus a library with classes for Alice:

include_once 'classes_alice.php';
include_once 'webhook_parse.php';

We receive data:

$data = json_decode(trim(file_get_contents('php://input')), true);

Parsing the data into variables:

if (isset($data['request']))
{

//original_utterance


  if (isset($data['meta']))
  {
    $data_meta = $data['meta'];
    if (isset($data_meta['client_id'])) {$client_id = $data_meta['client_id'];}
  }

  if (isset($data['request']))
  {
    $data_req = $data['request'];

    if (isset($data_req['original_utterance']))
    {
      $original_utterance = $data_req['original_utterance'];
    }


    if (isset($data_req['command'])) {$data_msg = $data_req['command'];}
    if (isset($data_req['nlu']))
    {
      $data_nlu = $data_req['nlu'];
      if (isset($data_nlu['tokens'])) {$tokens = $data_nlu['tokens'];}
//      $data_token_count = count($data_tokens);
    }
  }
  if (isset($data['session']))
  {
    $data_session = $data['session'];
    if (isset($data_session['new'])) {$data_msg_new = $data_session['new'];}
    if (isset($data_session['message_id'])) {$data_msg_id = $data_session['message_id'];}
    if (isset($data_session['session_id'])) {$data_msg_sess_id = $data_session['session_id'];}
    if (isset($data_session['skill_id'])) {$skill_id = $data_session['skill_id'];}
    if (isset($data_session['user_id'])) {$user_id = $data_session['user_id'];}
  }
}

There are a few fewer variables here:

$tokens - here are now all the words that the user entered

$user_id - user id here

Yandex constantly pings published skills, and I added a line to immediately exit the script without starting the full processing of the message:

  if (strpos($tokens[0], "ping") > -1)     {Send_Out("pong", "", true);}

We call the Parse_Tokens function for processing, it is the same as for Telegram:

$Out_Str = Parse_Tokens($tokens);

And send a response:

Send_Out($user_id, $Out_Str);

The Send_Out function is more complicated here:

function Send_Out($user_id, $out_text, $out_tts = "", $is_end = false)
{
  global $data_msg_sess_id, $user_id;

  ///// GENERATE BASE OF OUT //////
    $Data_Out = new Alice_Data_Out();
    $Data_Out->response = new Alice_Response();
    $Data_Out->session = new Alice_Session();
  ///// GENERATE BASE OF OUT End //////

  ///// OUT MSG GENERATE /////
  $Data_Out->session->session_id = $data_msg_sess_id;;
  $Data_Out->session->user_id = $user_id;

  $Data_Out->response->text = $out_text;
  $Data_Out->response->tts = $out_tts;

  if (strlen($out_tts) < 1) {$Data_Out->response->tts = $out_text;}

  $Data_Out->response->end_session = $is_end;

  header('Content-Type: application/json');
  print(json_encode($Data_Out, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT));

  die();
}

Finished the script for Alice.

The Parse_Tokens processing script itself was made purely for an example, you can do any checks and processing there.

function Parse_Tokens($tokens)
{
  $out = "";
  // do something with tokens //
  $out =  "Your eneter " . count($tokens) . " words: " . implode($tokens, " ");
  return $out;
}

If you need to communicate with a user of a more complex form than a question-answer, then you will need to save the user's $user_id and the data already received from the user in the database (for example, mysql) and analyze them in the Parse_Tokens function.

Actually, this is almost everything, if everything is done correctly, then the Telegram bot is already available, Alice's skill can be checked dialogs.yandex.ru/developerby going to your new skill on the testing tab.

Yandex.Alisa and Telegram bot in PHP with the same functionality

If everything works correctly, you can send the skill for moderation by clicking the "For moderation" button.

Now you have two bots for different platforms at once, which work in the same way.

Documentation for the Yandex.Dialogues service here

Full scripts posted on github download.

Source: habr.com

Add a comment