VKボットコミュニティのコールバックAPIを設定する
サーバー側の接続の準備
コールバックAPIは、VKontakteコミュニティでのユーザーアクティビティを追跡するためのツールです。 これにより、次のような新しい便利な機能を実装できます。
- 着信メッセージにインスタント返信を送信するためのボット。
- コンテンツの自動管理システム。
- 視聴者エンゲージメントインジケータを収集および処理するサービス。
Callback APIの使用を開始するには、コミュニティ設定でサーバーを接続し、データを受信するイベントの種類(たとえば、新しいコメントや新しい写真)を選択します。 コミュニティで選択したタイプのイベントが発生すると、VKontakteは、イベントをトリガーしたオブジェクトに関する基本情報(コメントの追加など)とともにJSONリクエストをサーバーに送信します。 更新を追跡するために定期的なAPI呼び出しを行う必要はもうありません—すぐにそれらを受信します。
接続の手順については、VKontakte開発者向けの優れたドキュメントに詳しく説明されています。
チャットボットスクリプトを配置するには、機能するWebサーバーが必要です。
コールバックAPIを使用するには、VKontakteはhttpsプロトコルを使用することをお勧めします。無料のcloudflare証明書とサーバー設定を取得する手順は、 無料CloudFlare SSL証明書の Google検索エンジンで見つけることができます。
サーバーのセットアップの最後に、ボットのスクリプトをアップロードする機能するWebサーバーが必要です。
VKコミュニティの設定
アクセスキー生成
コミュニティボットの作業における重要なポイントは、ユーザーメッセージとさまざまなイベントへの応答です。そのため、コミュニティに代わってユーザーとやり取りできるように、特別なキーを作成する必要があります。 これを行うには、「 コミュニティ管理 」タブに移動します。

次に、セクション「 APIの操作 」→「 アクセスキー 」に進みます 。

キーを作成するには、「キーを作成」をクリックして、ボットに付与する必要な権限を選択する必要があります。

私たちの場合、コミュニティのメッセージに十分にアクセスできます。

このキーを保存します。バックエンドのセットアップ時に必要になります。
コールバックAPIのセットアップ
サーバーとコミュニティを接続する必要があります。そのためには、サーバーのデータを指定し、秘密キーを作成する必要があります。 これを行うには、コミュニティ管理セクションに移動し、 [APIの操作]タブに移動する必要があります 。

次に非常に重要な点があります。サーバーのアドレスを入力し、秘密キーを作成する必要があります。すべての行はキーであり、アドレスはそれぞれサーバー上のphpスクリプトのアドレスです。

シークレットキーを入力して[保存]をクリックすると、キーのインストールが成功したことを示す適切な通知が表示されます。 サーバーアドレスのフィールドの反対側の「確認」ボタンはクリックされません。
指定した秘密鍵は、サーバーからの通知ごとに個別の秘密フィールドに送信されます。 これにより、通知が当社のサーバーから来たことを確実に判断できます。

また、サーバーが返すコードを覚えて、それを書き留めておく必要があります。バックエンドをセットアップするときに必要になります。

イベントの種類を構成する
コミュニティボットの作業における重要なポイントは、ユーザーメッセージとさまざまなイベントへの応答です。そのため、コミュニティに代わってユーザーとやり取りできるように、受信するイベントを正確に示す必要があります。 着信メッセージを追跡し、コミュニティに参加してそこから退出するので、対応するボックスをチェックする必要があります。 これを行うには、「 コミュニティ管理 」タブに移動します。

次に、セクション「 APIの操作 」→「 イベントタイプ 」に進みます 。


このセクションで必要な項目を設定します。
VKontakteバックエンドボットを構成する
次のステップは、VKontakteコールバックAPIからのリクエストを受け入れ、特定の方法でイベントに応答する特別なスクリプトを作成することです。 たとえば、phpスクリプトhandler.phpを作成してみましょう。このスクリプトのアドレスは、バックエンドを設定した後、コミュニティ設定で指定する必要があります。
次の変数の値に注意してください。
$ confirmToken $ token $ secretKey
confirmTokenには、サーバーが返す必要のあるコードが格納されます(この場合)。
004eec27
トークンには、「アクセスキーの生成」の章で生成したアクセスキーが格納されます
コールバックAPIのコミュニティ管理セクションでsecretKeyを設定します。 結果のコードは次のとおりです(handler.php):
最終コードhandler.php
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; } ?>
コードをダウンロードしたら、「コミュニティ管理」→「APIの操作」→「コールバックAPI」セクションに戻り、スクリプトの前にアドレスを入力して「確認」をクリックします。

その後、ボットと連携するためのコミュニティの設定が完了しました。コミュニティにメッセージを送信すると、応答が返されます。

追加機能
VKontakteコミュニティに参加する際の挨拶の実装
この段階では、サーバーとボットハンドラーが既に構成されています。考えられるすべてのイベントはドキュメントに記載されており、十分に記述されています。詳しく理解することをお勧めします。

group_joinイベントをキャッチします。このため、switchステートメントに新しいセクションを作成します。 開いているグループがあるため、join_typeの詳細は考慮しませんが、このパラメーターは自由に処理できます。
コミュニティアラートコードスニペット
// case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break;

ウェルカムメッセージ機能を備えたVKボットハンドラーの完全なコード:
ソースコードhandler.php。 ようこそメッセージ
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; } ?>
コミュニティを去った後、メンバーに別れを告げる
group_leaveを処理する特定のケースでは、実装は同一です
終了コミュニティ通知コードスニペット
// case 'group_leave': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, 😔<br>" . " .<br>" . " - <br>" . " - https://vk.com/kulakovkostya", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break;
完全なコードリスト:
高度な機能を備えた結果のhandler.phpコード
<?php if (!isset($_REQUEST)) { return; } // Callback API $confirmationToken = '004eec27'; // $token = ' '; // Secret key $secretKey = 'testSecureKey'; // $data = json_decode(file_get_contents('php://input')); // secretKey if(strcmp($data->secret, $secretKey) !== 0 && strcmp($data->type, 'confirmation') !== 0) return; //, "type" switch ($data->type) { // ... case 'confirmation': //... echo $confirmationToken; break; // ... case 'message_new': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, !<br>". " .", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; // case 'group_join': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => " . 5 2016, {$user_name}!<br>" . " , .<br>" . " .<br>" . " !", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; // case 'group_leave': //... id $userId = $data->object->user_id; // users.get $userInfo = json_decode(file_get_contents("https://api.vk.com/method/users.get?user_ids={$userId}&v=5.0")); // $user_name = $userInfo->response[0]->first_name; // messages.send $request_params = array( 'message' => "{$user_name}, 😔<br>" . " .<br>" . " - <br>" . " - https://vk.com/kulakovkostya", 'user_id' => $userId, 'access_token' => $token, 'v' => '5.0' ); $get_params = http_build_query($request_params); file_get_contents('https://api.vk.com/method/messages.send?' . $get_params); // "ok" Callback API echo('ok'); break; } ?>
GitHubのソースコード: github.com/KostyaKulakov/vksocialbot