はじめに
前回記事で物足りなかった方向けの記事です。
課題:postback
を使用してクイズを3問つくる
こんな課題を、前回記事の最後に出しました。
その解答例を載せておきます。
解答例
ヒント
postback
を使うメリットは、
-
message
と違い、(非正規の)ユーザー操作で呼び出すことができない - 「正しい回答」と「間違った回答」で処理をまとめられること(問題を増やしたときに、同じ処理を繰り返し書かなくてすむこと)
です。
特に、「正しい回答」と「間違った回答」で処理をまとめられること
これを実現してみてください。
条件分岐には、すでにあるものが使えます。
def callback
の中の、case event ~ when Line::Bot::Event::Message ~
を拡張しましょう。
解答例
class LinebotController < ApplicationController
def callback
body = request.body.read
events = client.parse_events_from(body)
events.each do |event|
message = []
case event
when Line::Bot::Event::Message
case event.type
when Line::Bot::Event::MessageType::Text
message.push(next_quiz(0))
end
when Line::Bot::Event::Postback
data = Rack::Utils.parse_nested_query(event['postback']['data'])
case data['action']
when 'correct'
message.push(sticker_correct)
message.push(next_quiz(data['quizid'].to_i))
when 'incorrect'
message.push(sticker_incorrect)
message.push(next_quiz(data['quizid'].to_i - 1))
end
end
client.reply_message(event['replyToken'], message)
end
end
private
def next_quiz(quizid)
case quizid
when 0
quiz_runteq_language
when 1
quiz_runteq_spelling
when 2
{'type': 'text', 'text': '全問正解!'}
else
quiz_runteq_language
end
end
def quiz_runteq_language
{
"type": "template",
"altText": "第一問:ランテックで学ぶメインの言語は?",
"template": {
"type": "confirm",
"text": "第一問:ランテックで学ぶメインの言語は?",
"actions": [
{
"type": "postback",
"label": "Ruby",
"data": "action=correct&quizid=1"
},
{
"type": "postback",
"label": "Java",
"data": "action=incorrect&quizid=1"
}
]
}
}
end
def quiz_runteq_spelling
{
"type": "template",
"altText": "第二問:ランテックのつづりは?",
"template": {
"type": "confirm",
"text": "第二問:ランテックのつづりは?",
"actions": [
{
"type": "postback",
"label": "RANTEK",
"data": "action=incorrect&quizid=2"
},
{
"type": "postback",
"label": "RUNTEQ",
"data": "action=correct&quizid=2"
}
]
}
}
end
def sticker_correct
{
"type": "sticker",
"packageId": "11537",
"stickerId": "52002734"
}
end
def sticker_incorrect
{
'type': 'sticker',
'packageId': '789',
'stickerId': '10881'
}
end
def client
@client ||= Line::Bot::Client.new { |config|
config.channel_secret = ENV['LINE_CHANNEL_SECRET']
config.channel_token = ENV['LINE_CHANNEL_TOKEN']
}
end
end
二問しかないので、ぜひもう一問足してみてください!
他にも
-
next_quiz
アクションのロジックの修正 - クイズ用のファイルを分ける
- 2択以外のクイズを作成
などなど、改善の余地ありです!
おわりに
ここまでお読みいただいた方、本当にありがとうございました!
ぜひ、LINE botを使用して楽しい開発ライフを!!
次は何を書こうかしら・・・