A
A
ASiD2018-09-19 14:56:37
Microsoft
ASiD, 2018-09-19 14:56:37

Why doesn't the message leave the user from the bot through the Microsoft Bot Framework?

Good day. I am writing a bot for Skype in PHP. It used to have its own API, but now it can only be done through the Microsoft Bot Framework.
What did you do:

  1. Registered in MS Azure, signed up for a free subscription
  2. Created a bot, added a password (received an application ID and password), specified a Message endpoint (a link that will send messages from users)
  3. Wrote a script that sends "123" in response to any message

The script does the following:
  1. Receives input
  2. Sends a request to get a token
  3. Sends a response message

The script receives data, the authorization token is obtained, but there were problems with sending the message - the response to the request comes "1", but the message does not appear in the dialog. Tried via web chat for test in MS Azure and via skype.
The code:
<?php 

$client_id = 'aaaaaaaa-bbbb-4444-acc1-f000000081b0';
$client_secret = 'sbjpfcxagfd19:ibEIB3*[#';
$authRequestUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';

$request = file_get_contents("php://input");
$input = json_decode($request, true);

$authRequestOptions = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query(
            array(
                'grant_type' => 'client_credentials',
                'client_id' => $client_id, //ID приложения
                'client_secret' => $client_secret, //Пароль приложения
                'scope' => 'https://graph.microsoft.com/.default'
            )
        )
    )
);

$authRequestContext  = stream_context_create($authRequestOptions);
$authResult = file_get_contents($authRequestUrl, false, $authRequestContext);

$authData = json_decode($authResult, true);

$deserializedResponseActivity = array(

    //Мы отвечаем обычным сообщением
    'type' => 'message',

  //Сообщаем id и имя участника чата (берем из полей recipient->id и recipient->name входящего POST-запроса с сообщением, то есть id и name, которым было адресовано входящее сообщение)
    'from' => array(
        'id' => (string)$input['recipient']['id'], 
        'name' => (string)$input['recipient']['name']
  ),
  
  //Устанавливаем id беседы, в которую мы отвечаем (берем из поля conversation->id входящего POST-запроса с сообщением)
    'conversation' => array(
        'id' => (string)$input['conversation']['id'] 
    ),
  
  //Устанавливаем id и имя участника чата, к которому обращаемся, он отправил нам входящее сообщение (берем из полей from->id и from->name входящего POST-запроса с сообщением)
    'recipient' => array(
        'id' => (string)$input['from']['id'],
        'name' => (string)$input['from']['name']
    ),
  
  
    //Текст ответа на сообщение
    // 'text' => $message,
    'text' => '123',

    
    //Устанавливаем внутренний ID активности, в контексте которого мы находимся (берем из поля id входящего POST-запроса с сообщением)
    'replyToId' => (string)$input['id']
    
);

$responseActivityRequestUrl = rtrim($input['serviceUrl'], '/') . '/v3/conversations/' . $deserializedResponseActivity['conversation']['id'] . '/activities/' . urlencode($deserializedResponseActivity['replyToId']);


$ch = curl_init($responseActivityRequestUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($deserializedResponseActivity));

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'Authorization: ' . $authData['token_type'] . ' ' . $authData['access_token'],
  'Content-Type: application/json'
));

$result = curl_exec($ch);
curl_close($ch);

What could be wrong?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question