mrMazai
@mrMazai
WebDeveloper

Как послать ответ от бота через Skype API v3?

Добрый день!

Эксперементирую с ботами для скайпа. Не могу понять, как ответить через API 3-й версии в чат с ботом.
Т.е. на тестовом общении, я отправляю своему боту сообщение, принимаю его, в виде JSON, который в массиве выглядит так:
Array
(
    [type] => message
    [id] => 18…2c7|0000001
    [timestamp] => 2017-04-03T09:02:34.5815073Z
    [serviceUrl] => https://webchat.botframework.com/
    [channelId] => webchat
    [from] => Array
        (
            [id] => u…qG
            [name] => You
        )

    [conversation] => Array
        (
            [id] => 18bc…2c7
        )

    [recipient] => Array
        (
            [id] => p…o@My…r-M
            [name] => П…о
        )

    [textFormat] => plain
    [locale] => ru-RU
    [text] => Привет!
    [channelData] => Array
        (
            [clientActivityId] => 14912…5196.0
        )

)


И не могу понять, что отправить ему в ответ. К примеру, в Телеграме — формируется JSON ответ и отдается, в ВК формируется GET-ответ, в WeChat формируется XML-ответ.

А как ответить скайпу? Есть у кого-нибудь пример кода? Или, может носом ткнёте, где в манах ответ?

UPD: Никто не ответил, решил сам, решение в комментах.
  • Вопрос задан
  • 1190 просмотров
Решения вопроса 1
mrMazai
@mrMazai Автор вопроса
WebDeveloper
<?php

// Получаем токен, он живет 3600 сек, можно кешировать
/*
  "token_type": "Bearer",
  "expires_in": 3599,
  "ext_expires_in": 0,
  "access_token": "eyJ0eXAiOi..."
*/
$url='https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token';
$params=array
    (
    'client_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx', //your-app-id
    'client_secret' => 'xxxxxxxxxxxxxxxxx',          // your-app-secret
    'grant_type'=>'client_credentials',                     //client_credentials&
    'scope'=>'https://api.botframework.com/.default'
    );
$result=file_get_contents($url, false, stream_context_create(array('http' => array
    (
    'method' => 'POST',
    'header' => 'Content-type: application/x-www-form-urlencoded',
    'content' => http_build_query($params)
    ))));

$token = json_decode($result, TRUE);

// Соответствия входящему массиву из вызова
// $IN['CHANNEL_NAME']
// $IN['CHANNEL']  = ['conversation']['id']
// $IN['URL'] = ['serviceUrl']
// $IN['TO'] = ['recipient']['id']
// $IN['FROM_ID'] = ['from']['id']

$url =$IN['URL'].'/v3/conversations/'.$IN['CHANNEL'].'/activities/';
$data_string = '
{
  "type": "message",
  "from": {
    "id": "'.$IN['TO'].'",
    "name": "Echo Bot"
  },
  "conversation": {
    "id": "'.$IN['CHANNEL'].'"
  },
  "recipient": {
    "id": "'.$IN['CHANNEL'].'",
    "name": "User Name"
  },
  "text": "'.$OUT['MSG'].'",
  "replyToId": "'.$IN['FROM_ID'].'"
}
';

$ch = curl_init($url);                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',
	'Authorization: Bearer '.$token['access_token'].'',
    'Content-Length: ' . strlen($data_string))                                                                       
);        

$result = curl_exec($ch); // должен вернуть {"id":"0:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы