以下の点に注意してから作業を始めてください!
- パソコンで作業をする
- ブラウザの自動翻訳機能をオフにする
スプレッドシートの準備
1.ページ右上のをクリックし、メニューに表示されるスプレッドシートをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/1googlelogin-1024x469.png)
2.新しいスプレッドシートを作成の下にある、空白をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/supuretto-1024x484.png)
3.スプレッドシート名とシート名を変更します。
変更するには上側にある無題のスプレッドシートをクリックし、自分の好きな名前に書き換えます。下側にあるシート名はダブルクリックをして自分の好きな名前に書き換えます。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/sito-1024x718.png)
4.受信用と返信用の語句を入力します。
A列には自分が送信する語句を書き、B列にはBOTが返信する語句を書きます。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/hensin.png)
5.スプレッドシートIDの確認をします。
スプレッドシードIDはURLの一部です、スプレッドシートを開いているブラウザのアドレスバーを見ることでスプレッドシートIDを確認できます。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/ID-1024x270.png)
https://docs.google.com/spreadsheets/d/スプレッドシートID/edit#gid=0
Apps Scriptにプログラミング+公開設定
1.Apps Scriptを開きます。
ページ内の上側にある拡張機能をクリックし、下に表示されたApps Scriptをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/appsp-1024x500.png)
2.Apps Scriptの画面が表示されます。
元々書いてあるコードはすべて消し、画像下にあるコードを貼り付けます。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/apps-1024x498.png)
var LINE_ACCESS_TOKEN = "チャネルアクセストークンをここに入力";
var ss = SpreadsheetApp.openById("スプレッドシートIDをここに入力");
var sh = ss.getSheetByName("シート名をここに入力");
function doPost(e) {
if (typeof e === "undefined") {
return;
} else {
var json = JSON.parse(e.postData.contents);
replyFromSheet(json)
}
}
function replyFromSheet(data) {
var replyUrl = "https://api.line.me/v2/bot/message/reply";
var lastRow = sh.getLastRow();
var wordList = sh.getRange(1, 1, lastRow, 2).getValues();
var reply_token = data.events[0].replyToken; //reply token
var text = data.events[0].message.text;
var replyTextList = [];
for (var i = 1; i < wordList.length; i++) {
if (wordList[i][0] == text) {
replyTextList.push(wordList[i][1]);
}
}
if (replyTextList.length < 1) {
return;
} else if (replyTextList.length > 5) {
var messageLength = 5;
} else {
var messageLength = replyTextList.length;
}
var messageArray = [];
for (var j = 0; j < messageLength; j++) {
messageArray.push({ "type": "text", "text": replyTextList[j] });
}
var headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": "Bearer " + LINE_ACCESS_TOKEN,
};
var postData = {
"replyToken": reply_token,
"messages": messageArray
};
var options = {
"method": "post",
"headers": headers,
"payload": JSON.stringify(postData)
};
UrlFetchApp.fetch(replyUrl, options);
}
3.Apps Scriptに必要な情報を入力します。
- var LINE_ACCESS_TOKEN = “チャネルアクセストークン”;
- var ss = SpreadsheetApp.openById(“スプレッドシートID”);
- var sh = ss.getSheetByName(“シート名”);
上記3点を自分の物に書き換えればプログラミングは完了です。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/teikeibot-1-1024x469.png)
4.GASとMessaging APIの接続をしていきます。
ページ右上のデプロイをクリックし、下に表示される新しいデプロイをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/depuroo-1024x511.png)
新しいデプロイの右下にある歯車のアイコンをクリックし、表示されるウェブアプリをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/deppuweb.png)
アクセスできるユーザーを全員に変え、右下のデプロイをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/deppua.png)
5.デプロイをクリックすると、アクセスの許可が求められます。
アクセスの承認をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/depuakuse.png)
オレンジの枠で囲われた自分のアカウント上をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/depusekyuaa-1.png)
下にある許可をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/kyoka-1.png)
デプロイを更新しました。と表示されるので、この後使うウェブアプリのURLをコピーしてから完了をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/depuend.png)
6.LINE DevelopersコンソールにWebhook URLを入れます。
https://developers.line.biz/ja/にアクセスし、右上のログインをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2024/03/image6-1024x596.png)
最近閲覧したチャネルの下にある、自分が作ったチャネルをクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/tyannner-1024x520.png)
画面中央付近にあるMessaging API設定をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/API-1-1024x515.png)
画面を下にスクロールしていき、Webhook URLの横にある編集をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/hensyu-1-1024x515.png)
Webhook URL横の空欄に、先ほどコピーしておいたウェブアプリのURLを貼り付けてから更新をクリックします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/kousin-1-1024x147.png)
Webhookの利用 横にあるボタンをクリックしてオンにします。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/hook-1024x371.png)
Webhook URLの右下にある検証をクリックし、成功と表示されれば完了です。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/kensyo-1-1024x413-1.png)
検証ボタンを押すとエラーが出る場合
LINE Developpersが英語版になっていませんか?ブラウザの翻訳機能で無理やり日本語にしてしまうとエラーが出てしまいます。
日本語にする場合は一度ページを更新(F5キー等)して英語表示に戻してからページ右下のEnglish 横にある▼をクリックして日本語にしてください。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/qa1-1024x510.png)
7.実際にLINEで会話してみましょう。
![](https://nksg.net/lab/blog/wp-content/uploads/2022/08/image0-1-1.png)