質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

2283閲覧

Line bot のスタンプを返す機能を実装したいです。

yutaide317

総合スコア2

LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/12/17 02:56

編集2020/12/17 07:21

line botをrailsで作成しています。
テキストを返す機能は実装できたのですが、スタンプを返せません。
公式を読んで実装しまして、heroku上では動いております。

###line_bots_controller

ruby

1class LineBotsController < ApplicationController 2 require 'line/bot' # gem 'line-bot-api' 3 4 # callbackアクションのCSRFトークン認証を無効 5 protect_from_forgery :except => [:callback] 6 7 def client 8 @client ||= Line::Bot::Client.new { |config| 9 config.channel_secret = ENV["LINE_CHANNEL_SECRET"] 10 config.channel_token = ENV["LINE_CHANNEL_TOKEN"] 11 } 12 end 13 14 def callback 15 16 # Postモデルの中身をランダムで@postに格納する 17 @post=Post.offset( rand(Post.count) ).first 18 body = request.body.read 19 20 signature = request.env['HTTP_X_LINE_SIGNATURE'] 21 unless client.validate_signature(body, signature) 22 head :bad_request 23 end 24 25 events = client.parse_events_from(body) 26 27 events.each { |event| 28 29 # event.message['text']でLINEで送られてきた文書を取得 30 if event.message['text'].include?("お疲れ様です") 31 response = "はい、お疲れ様です" 32 elsif event.message["text"].include?("焼き方") 33 response = "カブちゃんに聞いてね♪" 34 elsif event.message['text'].include?("田") 35 response = "せいちゃん" 36 elsif event.message['text'].include?("店") 37 response = "BOSS" * 50 38 elsif event.message['text'].include?("今日の運勢は") 39 response = ["大吉", "中吉", "小吉", "凶", "大凶"].shuffle.first 40 elsif event.message['sticker'] 41 response1 = "1", 42 response2 = "4" 43 else 44 response = @post.name 45 end 46 #if文でresponseに送るメッセージを格納 47 48 case event 49 when Line::Bot::Event::Message 50 case event.type 51 when Line::Bot::Event::MessageType::Text 52 message = { 53 type: 'text', 54 text: response 55 } 56 when Line::Bot::Event::MessageType::Sticker 57 message = { 58 type: 'sticker', 59 packageId: response1, 60 stickerId: response2 61 } 62 end 63 client.reply_message(event['replyToken'], message) 64 end 65 } 66 67 head :ok 68 end 69end

条件分岐のところで、スタンプで送られてきたらスタンプで返すところができてないことが原因かと思われるのですがコードの記述がわかりません。

ruby

1elsif event.message['sticker'] 2 response1 = "1", 3 response2 = "4" 4else

お手数なのですが、わかる方いらっしゃいましたらご回答お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

投稿2020/12/17 08:11

shozi3

総合スコア691

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yutaide317

2020/12/17 14:09 編集

回答いただきありがとうございます。 IDをそれぞれ変更しましたが、スタンプを返しません。。 条件分岐のところの書き方はどのようにすれば良いでしょうか。 ※スタンプを送ったときスタンプを返すとしたいです。 events.each { |event| # event.message['text']でLINEで送られてきた文書を取得 if event.message['text'].include?("お疲れ様です") response = "はい、お疲れ様です" elsif event.message["text"].include?("焼き方") response = "カブちゃんに聞いてね♪" elsif event.message['text'].include?("池田") response = "せいちゃん" elsif event.message['text'].include?("銀座") response = "BOSS" * 50 elsif event.message['text'].include?("今日の運勢は") response = ["大吉", "中吉", "小吉", "凶", "大凶"].shuffle.first else response = @post.name end #if文でresponseに送るメッセージを格納 case event when Line::Bot::Event::Message case event.type when Line::Bot::Event::MessageType::Text message = { type: 'text', text: response } when Line::Bot::Event::MessageType::Sticker message = { type: 'sticker', packageId: '11537', stickerId: '52002734' } end client.reply_message(event['replyToken'], message) end }
shozi3

2020/12/17 14:41

メッセージタイプがスタンプ('sticker')の場合とテキスト('text')で処理を分けてください。
yutaide317

2020/12/17 15:00 編集

処理を分けたのですが、elsifのアクションはどのように記述すれよいのでしょうか。。 下記記述したのですが、反応なしでした。 elsif event.message['sticker'] message = { type: 'sticker', packageId: '11537', stickerId: '52002734' } textでresponseで返すような記述(resuponseに代入して値を返しています)はどのようにしたら良いでしょうか。
shozi3

2020/12/17 16:02

処理を分けたのならifで判断する必要はないはずです。
yutaide317

2020/12/18 02:53

ありがとうございます! 理解できました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問