Line-Notifyが2025年3月末で終了するとのことで、代替を探してみたがどれもパッとせず。
LINE Official AccountでのメッセージAPIも200通/月以上は有料(5,000円/月)となるので小規模では使えず。
とりあえずの用途として顧客へ通知するわけではなく、申込みフォームからの申込みを管理者側へ通知できれば良いので、SlackかChatWorkへの投稿ができればスマホにアプリ入れれば通知くるのでまぁいいかと。
で、ChatWorkへのPOSTをPHPで簡単に。(要php-curl : >sudo apt install php-curl)
<?php
class ChatWorkComponent
{
public static function send(string $roomId, string $token, string $bodyText) : array | false
{
$options = array(
CURLOPT_URL => 'https://api.chatwork.com/v2/rooms/'.$roomId.'/messages',
CURLOPT_HTTPHEADER => [
'X-ChatWorkToken: '. $token,
'Content-Type: application/x-www-form-urlencoded'
],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS =>
http_build_query(['body' => $bodyText, 'self_unread=0'])
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$res = curl_exec($ch);
curl_close($ch);
if ($res === false) {
return false;
}
return json_decode($res, true);
}
}
こんな感じのを
$roomId = 'XXXXXXXXX';
$token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$res = ChatworkComponent::send($roomId, $token, 'this is test');
if ($res !== false) {
if (array_key_exists('message_id', $res)) {
//success
}
else {
//fail
}
}
else {
//fail
}
こんな感じで呼び出してやる。