LoginSignup
15
2

More than 1 year has passed since last update.

はじめに

Azure FunctionsでLINE Bot作成の記事にてAzure Functionsを用いたLINEのオウム返しLINEBotの作成方法を説明している。
今回はただのオウム返しではなく、語尾に絵文字を付けて返すLINEBotの作成方法を記載する。

前提

  • LINEアカウントを持っていること
    • 通常のLINEを利用できるもの
    • もしLINEを使っていない場合は、アカウントの作成が必要
  • Azureアカウントを持っていること
    • もしアカウントを持っていない場合は、こちらからアカウント登録
  • Azure FunctionsでLINE Bot作成の記事にてオウム返しLINEBotを作成していること

手順

Azure Functionsのコードを下記のものに書き換え、LINE Messaging API channelの作成で発行した「チャンネルシークレット」と「チャンネルアクセストークン」をとに入力します。

index.js
const line = require('@line/bot-sdk');

const config = {
    channelAccessToken: '<YOUR_TOKEN>',
    channelSecret:  '<YOUR_SECRET>',
};

const client = new line.Client(config);

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.message || (req.body && req.body.events)) {
        if (req.body && req.body.events[0]) {
            message = {
                type: "text",
                text: req.body.events[0].message.text + "(๑>◡<๑)"
            }
            console.log(message);
            if (req.body.events[0].replyToken) {
                client.replyMessage(req.body.events[0].replyToken, message);
            }
        }
        else {
            context.res = {
                status: 200,
                body: req.query.message
            };
        }
    }
    else {
        context.res = {
            status: 200,
            body: "Please check the query string in the request body"
        };
    };
};

置き換えたら、「保存」ボタンをクリックする。

動作確認

作成したLINEアカウントにメッセージを送信してみる。
すると「<送信したメッセージ>+(๑>◡<๑)」が返ってくることが確認できる。

さいごに

今回同様、返すmessageの中を編集することで返信メッセージを変えることができる。
そのため、今回のような簡単な修正ではなく、自然言語処理を導入すれば、オウム返しではないチャットボットができる。

15
2
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
15
2