いい大人が熱くなって後悔しないためのガシャBotを作りました！

### こんな感じでお役に立てば
昔カプセルトイ$^{注）}$って1回100円、200円で回してたように思います。それがいまや300～500円が主流で、1回1,000円を超えるものも続々リリースされています。気軽に回せて何が出るかわからないのが醍醐味やったのに、引き際を間違えて欲しいものが出るまで回したらエライことになってしまいます。この「引き際」を身につけるためのLINE-Botを作ってみました。カプセルトイ中毒の治療にも良いと思います。ちなみに「引き際」は株式投資やギャンブルにも会社でも人生でも重要です。

注）ガチャガチャ、ガチャ、ガチャポン、ガシャポンはメーカー各社の登録商標です。

### ガシャBot
でき上がりをご覧ください。1、9以外の何かを入れると説明が出ます。1を入れると1回回すことになります。出てくるのはLINEスタンプですが、最後の肝心なところでバグが。実はでき上がってないんです。

<blockquote class="twitter-tweet"><p lang="ja" dir="ltr">LINE BOT でちょっとしたガシャを作りました！　シクレありの鬼アソです。ただ、最後のところでバグが取れず苦戦中。肝心なところやのに、ホンマ…。 <a href="https://twitter.com/hashtag/protoout?src=hash&amp;ref_src=twsrc%5Etfw">#protoout</a> <a href="https://twitter.com/hashtag/%E5%88%9D%E5%BF%83%E8%80%85?src=hash&amp;ref_src=twsrc%5Etfw">#初心者</a> <a href="https://twitter.com/hashtag/%E3%82%AC%E3%82%B7%E3%83%A3%E3%83%9D%E3%83%B3?src=hash&amp;ref_src=twsrc%5Etfw">#ガシャポン</a> <a href="https://twitter.com/hashtag/LINEBot?src=hash&amp;ref_src=twsrc%5Etfw">#LINEBot</a> <a href="https://t.co/mcJQW6YXGk">pic.twitter.com/mcJQW6YXGk</a></p>&mdash; Xedge748hagi (@MmL71GZrZLrFhLD) <a href="https://twitter.com/MmL71GZrZLrFhLD/status/1583960662900109312?ref_src=twsrc%5Etfw">October 22, 2022</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>

### 環境
Node-RED（Heroku環境）
LINE Massaging API

### まずは機能の洗い出し
☆カプセルトイの中身は6種類でうち1種類がシークレットです。
☆ノーマル5種類にはLINE Massaging APIで取得したスタンプを送信します。
☆シークレットにはランダムにキツネの画像を提供してくれるAPIから取得した写真を送信します。
　　≪[RandomFox](https://randomfox.ca/floof/)≫
　　　キツネをランダムで表示してくれる
　　　実行例: https://randomfox.ca/floof/
```
{"image":"https:\/\/randomfox.ca\/images\/23.jpg","link":"https:\/\/randomfox.ca\/?i=23"}
```
☆LINEで数字を入力してガシャBotを操作します。
　→　1：300円投入　9：終了　左記以外：説明メニューの表示
☆最初、あるいは定められた入力値以外の入力がされた場合は説明メニューを返します。
☆出現確率は3種類。6:4:1の鬼アソートとします。
☆1回300円とし、終了時に得られた回した回数と投入金額を返します。

### Node-REDのフロー
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2887131/16e8d4c5-0507-e800-8534-8e075b0d3526.png)
・入力された値で分岐して、①説明表示、➁画像取得と表示、③終了時のコメント表示、を行います。
・➁の画像取得はLINE Massaging APIからスタンプ、外部APIからキツネの画像を取得しています。
・③のコメントで使うためフロー内のCONTEXT変数を定義して回数と金額を累積しています。

### ソースコード
```
[{"id":"dcd449e781809a41","type":"ReplyMessage","z":"190db8ff9dedd60c","name":"カプセル排出","replyMessage":"","x":1340,"y":500,"wires":[]},{"id":"0c9cfbee3dac0330","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":100,"wires":[["dcd449e781809a41"]]},{"id":"eb9e47080c00186b","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":340,"wires":[["dcd449e781809a41"]]},{"id":"a1f7733fbbbe87be","type":"change","z":"190db8ff9dedd60c","name":"どんまい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979917","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":580,"wires":[["dcd449e781809a41"]]},{"id":"ca7a625e632a7b24","type":"change","z":"190db8ff9dedd60c","name":"おねがい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979915","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":740,"wires":[["dcd449e781809a41"]]},{"id":"b75acdbc7dcb4fc9","type":"change","z":"190db8ff9dedd60c","name":"おつかれ","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979914","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":900,"wires":[["dcd449e781809a41"]]},{"id":"f09d250b9ff9e68a","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":140,"wires":[["dcd449e781809a41"]]},{"id":"29c4a3e69b5ea1fd","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":180,"wires":[["dcd449e781809a41"]]},{"id":"b057fae2180a2797","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":220,"wires":[["dcd449e781809a41"]]},{"id":"64375efe6e10670c","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":260,"wires":[["dcd449e781809a41"]]},{"id":"689d363ae1da20f3","type":"change","z":"190db8ff9dedd60c","name":"ありがとう","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551386","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":930,"y":300,"wires":[["dcd449e781809a41"]]},{"id":"eef85d5f2d1d4d97","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":380,"wires":[["dcd449e781809a41"]]},{"id":"d834027aefa2856c","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":420,"wires":[["dcd449e781809a41"]]},{"id":"7b649ad0aa83480c","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":460,"wires":[["dcd449e781809a41"]]},{"id":"b97ad6e45789e41d","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":540,"wires":[["dcd449e781809a41"]]},{"id":"c3f4acc328d81e37","type":"change","z":"190db8ff9dedd60c","name":"大変失礼","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6136","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10551393","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":500,"wires":[["dcd449e781809a41"]]},{"id":"04d7eb1f4c68cb1c","type":"change","z":"190db8ff9dedd60c","name":"どんまい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979917","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":620,"wires":[["dcd449e781809a41"]]},{"id":"09803a03f8ef8c32","type":"change","z":"190db8ff9dedd60c","name":"どんまい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979917","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":660,"wires":[["dcd449e781809a41"]]},{"id":"04dfd936264e80fd","type":"change","z":"190db8ff9dedd60c","name":"どんまい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979917","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":700,"wires":[["dcd449e781809a41"]]},{"id":"dcae2a072e3a3e59","type":"change","z":"190db8ff9dedd60c","name":"おねがい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979915","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":780,"wires":[["dcd449e781809a41"]]},{"id":"12385abdab122097","type":"change","z":"190db8ff9dedd60c","name":"おねがい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979915","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":820,"wires":[["dcd449e781809a41"]]},{"id":"19517973c4368cbe","type":"change","z":"190db8ff9dedd60c","name":"おねがい","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979915","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":860,"wires":[["dcd449e781809a41"]]},{"id":"5b26ca12107692a6","type":"change","z":"190db8ff9dedd60c","name":"おつかれ","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979914","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":940,"wires":[["dcd449e781809a41"]]},{"id":"91094d9e119f51dc","type":"change","z":"190db8ff9dedd60c","name":"おつかれ","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979914","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":980,"wires":[["dcd449e781809a41"]]},{"id":"a9071d7b191e80cb","type":"change","z":"190db8ff9dedd60c","name":"おつかれ","rules":[{"t":"delete","p":"payload","pt":"msg"},{"t":"set","p":"payload.type","pt":"msg","to":"sticker","tot":"str"},{"t":"set","p":"payload.packageId","pt":"msg","to":"6325","tot":"num"},{"t":"set","p":"payload.stickerId","pt":"msg","to":"10979914","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":920,"y":1020,"wires":[["dcd449e781809a41"]]},{"id":"9f7596cb386ae824","type":"change","z":"190db8ff9dedd60c","name":"シクレ画像取得","rules":[{"t":"set","p":"payload.type","pt":"msg","to":"image","tot":"str"},{"t":"set","p":"payload.originalContentUrl","pt":"msg","to":"payload.image","tot":"msg"},{"t":"set","p":"payload.previewImageUrl","pt":"msg","to":"payload.image","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1120,"y":1060,"wires":[["dcd449e781809a41"]]},{"id":"60954e034f8c3159","type":"switch","z":"190db8ff9dedd60c","name":"入力値で分類","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"9","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":560,"y":400,"wires":[["eb9e47080c00186b","a1f7733fbbbe87be","ca7a625e632a7b24","b75acdbc7dcb4fc9","0c9cfbee3dac0330","6064e12be92c868e","f09d250b9ff9e68a","29c4a3e69b5ea1fd","b057fae2180a2797","64375efe6e10670c","689d363ae1da20f3","eef85d5f2d1d4d97","d834027aefa2856c","7b649ad0aa83480c","c3f4acc328d81e37","b97ad6e45789e41d","04d7eb1f4c68cb1c","09803a03f8ef8c32","04dfd936264e80fd","dcae2a072e3a3e59","12385abdab122097","19517973c4368cbe","5b26ca12107692a6","91094d9e119f51dc","a9071d7b191e80cb"],["a581f2955f238491"],["8a3a717534794a55"]]},{"id":"6064e12be92c868e","type":"httprequest","z":"190db8ff9dedd60c","name":"","method":"GET","ret":"obj","paytoqs":"ignore","url":"https://randomfox.ca/floof/","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"x":930,"y":1060,"wires":[["9f7596cb386ae824"]]},{"id":"08c2ce831c5eade7","type":"function","z":"190db8ff9dedd60c","name":"カウンターアップ","func":"var wkkosu = context.get('kosu')||0;\nvar wkyen = context.get('yen')||0;\nwkkosu += 1;\nwkyen += 300;\ncontext.set('kosu', wkkosu);\ncontext.set('yen', wkyen);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":370,"y":400,"wires":[["60954e034f8c3159"]]},{"id":"8a3a717534794a55","type":"ReplyMessage","z":"190db8ff9dedd60c","name":"説明表示","replyMessage":"数字を入力［1：300円投入、9：終了、その他：説明メニュー］。商品は6種類［ありがとう、大変失礼、どんまい、おねがい、おつかれ、シークレット］","x":920,"y":1140,"wires":[]},{"id":"a581f2955f238491","type":"function","z":"190db8ff9dedd60c","name":"メッセージ出力とカウンターリセット","func":"var wkk = context.get('kosu')||0;\nvar wky = context.get('yen')||0;\nvar wkk2 = wkk||0;\nvar wky2 = wky||0;\nmsg.payload = 'ありがとうございました。' + wkk2 + '回回して' + wky2 + '円使いました。またね！';\nwkk = 0;\nwky = 0;\ncontext.set('kosu', wkk);\ncontext.set('yen', wky);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1010,"y":1100,"wires":[["0fe1cf55ba4d5587"]]},{"id":"31dbf15c30e47719","type":"Webhook","z":"190db8ff9dedd60c","name":"","url":"/webhook","x":200,"y":400,"wires":[["08c2ce831c5eade7"]]},{"id":"0fe1cf55ba4d5587","type":"ReplyMessage","z":"190db8ff9dedd60c","name":"終了","replyMessage":"","x":1250,"y":1100,"wires":[]}]
```

### 反省と考察
・乱数による決定をNode-REDのフローを使った形で作りましたが、1回の操作でスタンプが2、3個送られることがあり、また下の方のNodeが出にくい感じもあります。乱数は関数を使ってswitchノードで6種類に分ければフローもすっきりして正常に動いたかと思います。でも、結線での乱数選択がどうなるのか見てみたかったというのもあるのですが。
・フロー内の変数受け渡しはCONTEXT変数を使うとネットに出ていたので試してみましたが、受け渡しに失敗しているようです。終了時に、ほらこんなに使っちゃたんだよと示すところが肝心だったのですが、なぜか0回、0円になってしまいます。引き続き検討します。

![Screenshot_20221023-091111.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2887131/5a862532-84c0-f6db-375f-d28ed2282b5e.png)