Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アルバイト LINE BOT で使った「コマンドパターン」の紹介

kiwi
February 07, 2018

アルバイト LINE BOT で使った「コマンドパターン」の紹介

社内LT大会で登壇した際の資料です。

kiwi

February 07, 2018
Tweet

More Decks by kiwi

Other Decks in Technology

Transcript

  1. ユーザーからのメッセージをどう処理するか switch message.text { } case ‘Hello’: message.user.friendPoint += 1;

    bot.replyMessage(‘Hello, my friend!’); case ‘Find Arbeit’: res = Arbeit.find(message.text); bot.replyMessage(res.title, res.link);
  2. ユーザーからのメッセージをどう処理するか switch message.text { } case ‘Hello’: message.user.friendPoint += 1;

    bot.replyMessage(‘Hello, my friend!’); case ‘Find Arbeit’: res = Arbeit.find(message.text); bot.replyMessage(res.title, res.link); bot.buttonMessage(‘Find More’); case ‘Find More’: res = Arbeit.find(message.text).skip(1); bot.replyMessage(res.title, res.link);
  3. LINE BOT バイト探しの場合(擬似コード) protocol CommandMessage { func init(user); func postQuestion();

    // 質問するとき func receiveAnswer(answer); // 回答を受け取ったとき } class AskGender: CommandMessage { func init(user) { this.user = user; } func postQuestion() { bot.pushMessage(user, ‘Whats your gender?’); } func receiveAnswer(answer) { user.gender = answer; } }
  4. LINE BOT バイト探しの場合(擬似コード) class MessageController { func receive(request) { message

    = Message.from(request) commandForReceived = null; commandForPost = null; switch message.type { case MessageTypeStart: commandForPost = AskGender(message.user); case MessageTypeGender: commandForReceived = AskGender(message.user); commandForPost = AskStudent(message.user); } commandForReceived?.receiveAnswer(message); commandForPost?.postQuestion();
  5. 悩みどころ class MessageController { func receive(request) { message = Message.from(request)

    commandForReceived = null; commandForPost = null; switch message.type { case MessageTypeStart: commandForPost = AskGender(message.user); case MessageTypeGender: commandForReceived = AskGender(message.user); commandForPost = AskStudent(message.user); } commandForReceived?.receiveAnswer(message); commandForPost?.postQuestion();
  6. 悩みどころ class MessageController { func receive(request) { message = Message.from(request)

    commandForReceived = null; commandForPost = null; switch message.type { case MessageTypeStart: commandForPost = AskGender(message.user); case MessageTypeGender: commandForReceived = AskGender(message.user); commandForPost = AskStudent(message.user); } commandForReceived?.receiveAnswer(message); commandForPost?.postQuestion(); 別のところにある……
  7. たとえば • メッセージ判別メソッド • メッセージの受信~次のメッセージの送信 をまとめてみる class ReceiveGender: CommandMessage {

    class func init(user) { this.user = user; } func receiveAnswer(answer) { user.gender = answer; bot.pushMessage(user, ‘You are student?’); } }
  8. たとえば class MessageController { let mesToCmd: [((Message -> bool), class)]

    = [ ]; func receive(message) { user = message.user; command = null; for tuple in mesToCmd { if tuple[0](message) { command = tuple[1]; break; } } command?.receiveAnswer(); ({mes in return mes.text == ‘Start’}, AskGender.class), ({mes in return mes.data == ‘Gender’}, ReceiveGender.cl