LoginSignup
4
0

More than 1 year has passed since last update.

【第三回】コロナ禍だから何かできることをー 自宅療養者連絡ツール ー

Last updated at Posted at 2021-10-01

前回までのあらすじ

【第二回】コロナ禍だから何かできることを

実装イメージ図

LINEを利用して保健所の担当者の負担を軽減するとともに、自宅療養者はいつでもつながっている安心感を持たせるためのツールイメージです。
メイン.00_00_51_12.Still002.jpg

まずはLINEからデータベースまでの流れを考えよう

システム構成.jpg

WebHookで取得したデータを解析して、メッセージの内容によって処理を分ける部分の説明をします。
今回はPHPで実装します。

やりとりする中身のフォーマットはこちらを参考にしてください。
MessagingAPIリファレンス

LINEbotからWebhookで取得したデータの取得処理

//ユーザーからのメッセージ取得
$j_str = file_get_contents('php://input');
$j_obj = json_decode($j_str);

//取得データ
$replyToken = $j_obj->{"events"}[0]->{"replyToken"};              //返信用トークン
$line_id = $j_obj->{"events"}[0]->{"source"}->{"userId"};         //LINE_ID
$message_type = $j_obj->{"events"}[0]->{"message"}->{"type"};     //メッセージタイプ
$message_text = $j_obj->{"events"}[0]->{"message"}->{"text"};     //メッセージ内容

//メッセージタイプが「text」以外のときは何も返さず終了
if($message_type != "text") exit;

LINEbotからWebhookで取得したデータの取得処理

「体温報告」の文字が存在した場合

    ボタンメッセージを返す

    体温報告ー結果.png

「体調報告」の文字が存在した場合

    ボタンメッセージを返す

    体調報告ー結果.png

以下の文字の場合
    【体温調査】"平熱"、"微熱"、"高熱"
    【体調調査】"OK"、"NG"、"HELP"

データベースに更新して、メッセージを返す
    連絡しましたー結果.png

それ以外は、

申し訳ありません。
入力されたメッセージ」については返答しかねます

と返す

    if ((strpos($message_text,'体温報告')) !== false) { 
        //体温測定
        $response_format_text = [
            [
                "type" => "template",
                "altText" => "現在の体温を教えてください",
                "template" =>  [
                    "type" => "buttons",
                    "title" => "体温調査",
                    "text" => "現在の体温を教えてください",
                    "actions" => [
                            [
                                "type" => "message",
                                "label" => "平熱",
                                "text" => "平熱"
                            ],
                            [
                                "type" => "message",
                                "label" => "微熱",
                                "text" => "微熱"
                            ],
                            [
                                "type" => "message",
                                "label" => "高熱",
                                "text" => "高熱"
                            ],
                        ]
                ]
            ]
        ];
    } elseif ((strpos($message_text,'体調報告')) !== false) { 
        //体調返答
        $response_format_text = [
            [
                "type" => "template",
                "altText" => "現在の体調を教えてください",
                "template" =>  [
                    "type" => "buttons",
                    "title" => "体調調査",
                    "text" => "現在の体調を教えてください",
                    "actions" => [
                            [
                                "type" => "message",
                                "label" => "無症状",
                                "text" => "OK"
                            ],
                            [
                                "type" => "message",
                                "label" => "熱があるまたは息苦しい",
                                "text" => "NG"
                            ],
                            [
                                "type" => "message",
                                "label" => "熱があり息苦しい",
                                "text" => "HELP"
                            ],
                        ]
                ]
            ]
        ];
    }else{
        switch($message_text){
            case "平熱":
            case "微熱":
            case "高熱":
                //体温調査
            case "OK":
            case "NG":
            case "HELP":
                //体調報告
                $return_message_text = "連絡しました";
                break;
            default: 
                $return_message_text = "申し訳ありません。\r\n「" . $message_text . "」については返答しかねます";
        };
        // レスポンスフォーマット
        $response_format_text = [
            [
                "type" => "text",
                "text" => $return_message_text
            ]
        ];
    }

生成した連想配列をjson形式に変更してLINEbotへ返す処理

$accessToken = '第二章で設定されていたアクセストークンをコピーする';

//ポストデータ
$post_data = [
    "replyToken" => $replyToken,
    "messages" => $response_format_text
];

//返信実行
sending_messages($accessToken, $post_data);

//メッセージの送信
function sending_messages($accessToken, $post_data){

    //curl実行
    $ch = curl_init("https://api.line.me/v2/bot/message/reply");
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json; charser=UTF-8',
        'Authorization: Bearer ' . $accessToken
    ));
    $result = curl_exec($ch);
    curl_close($ch);
}

次回はこの記述にデータベース更新処理の追加を説明します。

コロナ禍だから何かできることをー 自宅療養者連絡ツール ー

【第一回】実装イメージ図と動画
【第二回】LINEからデータを取得して返すまでの流れ
->>【第三回】LINEからデータベースまでの流れ
【第四回】データベースへの更新までの流れ
【第五回】ユーザー登録の仕組み-LINEbotの設定部分
【第六回】ユーザー登録の仕組みLIFFで表示する画面の開発
【第七回】データベースの構造
【第八回】WEB画面上でできる機能
【第九回】まとめ

4
0
0

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
4
0