@Blog
日常日誌からプログラムやYOUTUBER紹介、旅日記まで日々更新中です。
文字数[7269文字] この記事は9分5秒で読めます.
チャットワークのAPIを使ってみました.プロンプトでほぼ書いています.
おはようございます.チャットワークのAPIを使ってみました.プロンプトでほぼ書いたコードになります、チャットGPTの無料版にリファレンスのURLリンクとPHPのクラス化、リターンに$thisで返却出来る所は$thisを使用してスマートにコードを書いてと指示を出しました.
出来上がったコードが下記になります.ソースコードは自分の方でモンキーテスト的に動かしてみましたが、ちゃんと動作するようです.
<?php
class ChatworkClient
{
private string $apiToken;
private string $baseUrl = 'https://api.chatwork.com/v2';
private int $retryCount = 3;
private int $retryDelay = 1000000; // microseconds
public function __construct(string $apiToken)
{
$this->apiToken = $apiToken;
}
public function setRetry(int $count, int $delayMicroseconds): self
{
$this->retryCount = $count;
$this->retryDelay = $delayMicroseconds;
return $this;
}
private function request(string $method, string $path, array $params = []): array
{
$attempts = 0;
while ($attempts < $this->retryCount) {
$attempts++;
$ch = curl_init();
$url = $this->baseUrl . $path;
if ($method === 'GET' && $params) {
$url .= '?' . http_build_query($params);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = ['X-ChatWorkToken: ' . $this->apiToken];
if (in_array($method, ['POST', 'PUT', 'DELETE'])) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
if ($attempts < $this->retryCount) {
usleep($this->retryDelay);
continue;
}
throw new RuntimeException("cURL error after {$attempts} attempts: {$error}");
}
if ($status >= 200 && $status < 300) {
return [
'status' => $status,
'body' => $body ? json_decode($body, true) : null,
];
}
if ($attempts < $this->retryCount && $status >= 500) {
usleep($this->retryDelay);
continue;
}
return [
'status' => $status,
'body' => $body ? json_decode($body, true) : null,
];
}
throw new RuntimeException("Request failed after {$this->retryCount} attempts");
}
public function setToken(string $token): self
{
$this->apiToken = $token;
return $this;
}
public function me(): array
{
return $this->request('GET', '/me');
}
public function getMyStatus(): array
{
return $this->request('GET', '/my/status');
}
public function getMyTasks(array $filters = []): array
{
return $this->request('GET', '/my/tasks', $filters);
}
public function getContacts(): array
{
return $this->request('GET', '/contacts');
}
public function getRooms(): array
{
return $this->request('GET', '/rooms');
}
public function createRoom(array $params): array
{
return $this->request('POST', '/rooms', $params);
}
public function getRoom(int $roomId): array
{
return $this->request('GET', "/rooms/{$roomId}");
}
public function updateRoom(int $roomId, array $params): array
{
return $this->request('PUT', "/rooms/{$roomId}", $params);
}
public function deleteRoom(int $roomId, string $action = 'leave'): array
{
return $this->request('DELETE', "/rooms/{$roomId}", ['action_type' => $action]);
}
public function getMembers(int $roomId): array
{
return $this->request('GET', "/rooms/{$roomId}/members");
}
public function updateMembers(int $roomId, array $params): array
{
return $this->request('PUT', "/rooms/{$roomId}/members", $params);
}
public function getMessages(int $roomId, bool $force = false): array
{
return $this->request('GET', "/rooms/{$roomId}/messages", ['force' => $force ? 1 : 0]);
}
public function postMessage(int $roomId, string $body, bool $selfUnread = false): self
{
$this->request('POST', "/rooms/{$roomId}/messages", ['body' => $body, 'self_unread' => $selfUnread ? 1 : 0]);
return $this;
}
public function markRead(int $roomId): self
{
$this->request('PUT', "/rooms/{$roomId}/messages/read");
return $this;
}
public function markUnread(int $roomId): self
{
$this->request('PUT', "/rooms/{$roomId}/messages/unread");
return $this;
}
public function getMessage(int $roomId, int $messageId): array
{
return $this->request('GET', "/rooms/{$roomId}/messages/{$messageId}");
}
public function getRoomTasks(int $roomId, array $filters = []): array
{
return $this->request('GET', "/rooms/{$roomId}/tasks", $filters);
}
public function createTask(int $roomId, array $params): array
{
return $this->request('POST', "/rooms/{$roomId}/tasks", $params);
}
public function uploadFile(int $roomId, string $filePath, string $message = ''): array
{
if (!file_exists($filePath)) {
throw new InvalidArgumentException("File not found: {$filePath}");
}
$ch = curl_init();
$url = $this->baseUrl . "/rooms/{$roomId}/files";
$cfile = curl_file_create($filePath);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-ChatWorkToken: ' . $this->apiToken]);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => $cfile, 'message' => $message]);
$body = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
throw new RuntimeException(curl_error($ch));
}
curl_close($ch);
return ['status' => $status, 'body' => json_decode($body, true)];
}
}
人が今回のコードを書いた場合、早い人でも10分ぐらいはコードを書かないといけないと思います、どんなに早くてもそれぐらいの時間は必要だと思いますが、生成AIはこれを数十秒で書ける訳ですから、確実に時間短縮になります.
なので人工知能が使える現場は間違いなく最初のコード出力は人工知能に任せた方が良いです.特に新規案件の土台は生成AIに任せると開発コストは削減出来ます.
明日へ続く
3441番目の投稿です/37 回表示されています.
AIによるおすすめ記事
著者名
@taoka_toshiaki
※この記事は著者が40代前半に書いたものです.
Profile
高知県在住の@taoka_toshiakiです、記事を読んで頂きありがとうございます.
数十年前から息を吸うように日々記事を書いてます.たまに休んだりする日もありますがほぼ毎日投稿を心掛けています😅.
SNSも使っています、フォロー、いいね、シェア宜しくお願い致します🙇.
SNS::@taoka_toshiaki
タグ
```, クラス, コード, コスト, これ, スマート, ソース, それ, チャット, テスト, プロンプト, モンキー, よう, リターン, リファレンス, リンク, ワーク, 下記, 人工, 今回, 使用, 出力, 削減, 動作, 土台, 場合, 必要, 指示, 新規, 明日, 時間, 最初, 案件, 無料, 現場, 生成, 知能, 短縮, 確実, 自分, 返却, 開発, 間違い,