Chatworkから別アカウントへ自動転送するシステムを作りました

Chatworkで複数のアカウントを作った場合に、例えばメインで使っているアカウントでログインするとサブのアカウントの通知を見逃す恐れがあります。
ブラウザを別にすればいいかなとも思ったのですが、立ち上げるのを忘れると通知に気づけないおそれがあるので、なんとかメインアカウントにサブアカウントの通知を転送できるようにならないかなと思って考えた結果、
1.サブアカウントがメッセージを受け取った際にWebhookでサーバーにアクセス
2.Webhookで飛ばした内容を処理してAPIでサブアカウント自身に送信
3.あらかじえmメインアカウントとサブアカウントを友達登録してつなげておいて、サブアカウントからメインアカウントにメッセージを送る
という形で通知が飛ぶようにできました。
最初はメインアカウントのAPIを使って直接Webhookの内容をマイチャットに取り込もうと思ったのですが、通知のアイコンが光らなかったため、サブアカウントからメインアカウントへ送信させるという方法をとりました。
これで複数のChatworkの通知を見逃さないようにできたと思います。
例えばこの中継しているサーバーに生成AIでの処理を挟めば「重要度を自動判定して付記する」とか、「荒くれた文章をやさしいものに変換する」というようなことも可能になりそうです。
全部のメッセージをデータベースに入れておけば抜けてるタスクを指摘してくれるとか、対応を提案してくれるというようなものもできるかもしれません。
できれば仕事の連絡は一か所に集約したいと思うので、WebhookやAPIを使ってメールとかLINEの連絡も集約できるといいですね。
<?php// 設定$api_token_b = ‘***’; // アカウントBのAPIトークンに置き換えてください$room_id_b = ‘***’; // アカウントBのマイチャットのRoom ID。通常は自分自身のaccount_id です。$account_id_a_webhook_target = ‘***’; // Webhookの監視対象アカウントAのアカウントID (Webhook payloadから取得するか、設定で指定)// ** メンションしたいアカウントのアカウントID (例: アカウントC) **$mention_account_id_c = ‘***’; // メンションしたいアカウントCのアカウントIDに置き換えてください$mention_user_name_c = ‘我’; // メンションしたいアカウントCのユーザー名に置き換えてください (表示用)// Webhook ID と名称の対応テーブル (以前のコードから変更なし – 必要に応じて設定ください)$webhook_name_table = [‘99999’ => ‘***’,‘99999’ => ‘***’,‘99999’ => ‘***’,‘99999’ => ‘***’,];// ログファイル設定 (以前のコードから変更なし – 必要に応じて設定ください)$log_file = ‘webhook.log’;ini_set(‘error_log’, $log_file);// HTTPリクエストヘッダー設定 (以前のコードから変更なし)$headers_b = [‘X-ChatWorkToken: ‘ . $api_token_b,‘Content-Type: application/json’];// ログ出力関数 (以前のコードから変更なし)function write_log($message) {$timestamp = date(‘Y-m-d H:i:s’);error_log(“[” . $timestamp . “] ” . $message . “\n”, 3, ini_get(‘error_log’));}// Webhookからのリクエストボディを取得write_log(“Webhook request received.”);$request_body = file_get_contents(‘php://input’);$webhook_payload = json_decode($request_body, true);// Webhookの種類を確認 (message_created イベントをチェック)if (isset($webhook_payload[‘webhook_event_type’]) && $webhook_payload[‘webhook_event_type’] === ‘message_created’) {write_log(“Webhook event type: message_created”);// Webhook ID を取得$webhook_id = $webhook_payload[‘webhook_setting_id’];write_log(“Webhook ID: ” . $webhook_id);// Webhook ID に対応する名称を取得 (テーブル参照)if (isset($webhook_name_table[$webhook_id])) {$webhook_name = $webhook_name_table[$webhook_id];write_log(“Webhook Name found in table: ” . $webhook_name);} else {$webhook_name = ‘その他のスレッド’;write_log(“Webhook Name not found in table. Using default: ” . $webhook_name);}// 送信元アカウントの情報を確認$sender_account_id = $webhook_payload[‘webhook_event’][‘account_id’];write_log(“Sender Account ID: ” . $sender_account_id);if ($sender_account_id == $account_id_a_webhook_target) {write_log(“Message from Account A detected.”);// メッセージ内容を取得$message_body = $webhook_payload[‘webhook_event’][‘body’];// ** 修正: メンション付きメッセージを作成 **$message_send_b = “[To:” . $mention_account_id_c . “] ” . $mention_user_name_c . ” さん\n[info][title]” . $webhook_name . “からの新着メッセージ[/title]” . $message_body . “[/info]”; // メンションを先頭に追加 (改行必須)write_log(“Message to send to Account B: ” . $message_send_b);// Chatwork API エンドポイント (メッセージ送信)$api_endpoint_b = ‘https://api.chatwork.com/v2/rooms/’ . $room_id_b . ‘/messages’;// APIリクエスト実行 (アカウントBのマイチャットへメッセージ送信)$post_data_b = http_build_query([‘body’ => $message_send_b]);$curl_b = curl_init($api_endpoint_b);curl_setopt($curl_b, CURLOPT_POST, true);curl_setopt($curl_b, CURLOPT_POSTFIELDS, $post_data_b);curl_setopt($curl_b, CURLOPT_HTTPHEADER, $headers_b);curl_setopt($curl_b, CURLOPT_RETURNTRANSFER, true);write_log(“Sending API request to: ” . $api_endpoint_b);$response_b = curl_exec($curl_b);$http_status_b = curl_getinfo($curl_b, CURLINFO_HTTP_CODE);curl_close($curl_b);write_log(“API request completed. HTTP Status Code: ” . $http_status_b);write_log(“API Response: ” . $response_b);if ($http_status_b === 200) {http_response_code(200);write_log(“Message sent to Account B’s MyChat successfully.”);echo “Message sent to Account B’s MyChat successfully.”;} else {http_response_code($http_status_b);write_log(“Failed to send message to Account B’s MyChat. HTTP Status Code: ” . $http_status_b);write_log(“Response from Chatwork API: ” . $response_b);echo “Failed to send message to Account B’s MyChat. HTTP Status Code: ” . $http_status_b . “\n”;echo “Response from Chatwork API: ” . $response_b;}} else {// アカウントA以外からのメッセージの場合は何もしない (必要に応じて処理を追加)http_response_code(200);write_log(“Message is not from Account A. No action taken.”);echo “Message is not from Account A. No action taken.”;}} else {// message_created 以外のイベントの場合http_response_code(200);// 修正: ログ出力メッセージを修正if (isset($webhook_payload[‘webhook_event_type’])) {$received_event_type = $webhook_payload[‘webhook_event_type’];write_log(“This webhook event is not ‘message_created’. Received event type: ” . $received_event_type);echo “This webhook event is not ‘message_created’. Received event type: ” . $received_event_type;} else {write_log(“This webhook event is not ‘message_created’. Webhook event type is not defined in payload.”);echo “This webhook event is not ‘message_created’. Webhook event type is not defined in payload.”;}write_log(“No action taken.”);}write_log(“Webhook request processing finished.”);?>