LoginSignup
5
5

More than 5 years have passed since last update.

MicrosoftBotFrameworkのRESTAPIをPHPで SKYPEに悲しい言葉を吐くボット

Last updated at Posted at 2016-09-20

引用元

MicrosoftBotFrameworkのRestAPIを使ったTimerBot
大体はnyasba様の内容をゆるくPHPで書いた感じです。

実例

<?php
//トークンを取得する
$head = [];
$head[] = 'Content-Type: application/x-www-form-urlencoded';

$body = array();
$body['grant_type'] = 'client_credentials';
$body['client_id'] = '*******';
$body['client_secret'] = '*******';
$body['scope'] = 'https://graph.microsoft.com/.default';

$ch = curl_init('https://login.microsoftonline.com/common/oauth2/v2.0/token');

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
// 実行
$res = curl_exec($ch);
// エラーが発生したらとめる
if (!empty(curl_error($ch))) {
    var_dump(curl_error($ch));
    exit();
}
// ハンドルを閉じる
curl_close($ch);
$tmp = json_decode($res);

$token = $tmp->access_token;
//トークンが取れたのでボットにランダムに悲しい言葉を吐かせる
$messages = [
        '作り直すしかないね…',
        '予算がね…',
        '時間がね…',
        '見積もりがね…',
        'このバグ根が深いです',
        'なんでこうなったの?',
        '用意していないです。',
        'えっ!今の作業って無駄ってことですか?'
];

$body = array();
$body['type'] = 'message/text';
$body['text'] =  $messages[array_rand($messages)];

$head = [];
$head[] = 'Authorization: Bearer '.trim($token);
$head[] = "Content-Type: application/json";
$body = array();
$body['type'] = 'message/text';
$body['text'] = $messages[array_rand($messages)];
$ch = curl_init('https://api.skype.net/v3/conversations/'.urlencode('/get name の値').'/activities/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$res = curl_exec($ch);
// エラーが発生したらとめる
if (!empty(curl_error($ch))) {
    var_dump(curl_error($ch));
    exit();
}
// ハンドルを閉じる
curl_close($ch);

あとはcrontabにでも放り込めばて定期的にメッセージをPOSTしてくれます。

実際のとこはCURLでできてしまうので、特にPHPで書く理由もあまりなかったりします。
ここからまじめに書いていけばChatOpsのインターフェイスとしてSKYPEは悪い選択肢ではないかなと思ってきました。

あとまだ勉強中ですがMicrosoftBotFrameworkはslackにも対応しているようなので横断的なbot構築を考えるとなかなかいんじゃないかと

5
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5