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

Building Your Own R2 Unit

Building Your Own R2 Unit

There is another massive shift happening with how we interact with companies through software. Users feel comfortable naturally talking with their applications through chat bots. Chat is the next generation of the user interface.

Companies like Slack, Facebook, WhatsApp, and WeChat have some of the most popular apps in the world and they are all betting on a messaging interface.

Ruby and it’s ecosystem of libraries can support these new types of interactions. In this session, we will see how we can build scalable, realtime web applications (or “bots”) using the Slack API. We will see what a good bot architecture looks like and how we can integrate with existing artificial intelligence services to make our bots smarter.

Jamie Wright
PRO

March 12, 2016
Tweet

More Decks by Jamie Wright

Other Decks in Programming

Transcript

  1. Building Your
    Own
    R2 Unit

    View Slide

  2. serial #
    R2-D2

    View Slide

  3. #emoji-bot

    View Slide

  4. #emoji-bot

    View Slide

  5. But I was going
    to go into Tosche
    Station to pick up
    some power
    converters…
    "
    #emoji-bot

    View Slide

  6. a long time ago…

    View Slide

  7. a long time ago…
    1950

    View Slide

  8. a long time ago…
    1965
    1950

    View Slide

  9. a long time ago…
    1965
    1950

    View Slide

  10. a long time ago…

    View Slide

  11. a long time ago…
    1995

    View Slide

  12. a long time ago…
    1995

    View Slide

  13. a long time ago…
    1995 1997

    View Slide

  14. a long time ago…
    1995 1997

    View Slide

  15. a long time ago…

    View Slide

  16. a long time ago…
    2000

    View Slide

  17. a long time ago…
    2000 2007

    View Slide

  18. a long time ago…
    2000 2007

    View Slide

  19. View Slide

  20. 2016

    View Slide

  21. caring
    module
    #D5e7r6

    View Slide

  22. 3Apps

    View Slide

  23. familiar
    1.

    View Slide

  24. familiar
    1.
    2. < barriers

    View Slide

  25. UBER

    View Slide

  26. UBER

    View Slide

  27. UBER

    View Slide

  28. UBER

    View Slide

  29. UBER

    View Slide

  30. Bewhere
    users
    the
    are

    View Slide

  31. ‘86

    View Slide

  32. ‘86

    View Slide

  33. ‘86
    ‘96

    View Slide

  34. ‘86
    ‘96
    ‘06

    View Slide

  35. ‘16
    ‘86
    ‘96
    ‘06

    View Slide

  36. 80
    $
    M
    slack fund

    View Slide

  37. 80
    $
    M
    slack fund

    View Slide

  38. speaking
    module
    #S40926

    View Slide

  39. UI
    the
    design

    View Slide

  40. Can you schedule a meeting
    for tomorrow with Jim and
    Tim?
    Which Jim do you want me to
    schedule a meeting with?
    1. Jim F.
    2. Jim P.
    Sure. It looks like the
    afternoon is best. Do you
    want me to schedule this for
    then?
    Meeting invite went out for
    2PM tomorrow to everyone. I
    will follow up to confirm
    everyone is booked.
    Ok. There is no more free
    time tomorrow. Do you want
    me to try for Wednesday?
    Multiple contacts
    "Yes"
    "No"

    View Slide

  41. Design
    Challenges

    View Slide

  42. Discoverability

    View Slide

  43. Discoverability
    Are you ready to
    have that
    meeting? You
    can reply with
    "yes" or "no".
    "yes"

    View Slide

  44. Discoverability

    View Slide

  45. Discoverability
    Meeting scheduled!
    Did you know you can
    reschedule by just saying
    "reschedule meeting with
    Dave"?

    View Slide

  46. Validation

    View Slide

  47. Validation
    Andy wants to
    meet with you
    today at 2PM.
    Are you
    available?
    "blerg"

    View Slide

  48. Validation
    "blerg"

    View Slide

  49. Validation
    I'm sorry, "blerg" is not an
    answer.
    You can say "yes" or "no".
    Are you available to meet
    with Andy today at 2PM?
    "blerg"

    View Slide

  50. Selection

    View Slide

  51. Selection
    Where do you want to meet
    tonight with MacKenzie?
    1. Coffee Shop
    2. Restaurant for dinner
    3. Local bar
    "1"

    View Slide

  52. Selection
    "1"

    View Slide

  53. Selection
    Ok. Which coffee shop?
    1. Starbucks on Central
    2. Cup 'o Joe Downtown
    "1"

    View Slide

  54. Feedback

    View Slide

  55. Feedback
    No one in your meeting is
    available on Friday.
    Would you like me to ask
    them anyway?
    R2 is typing

    View Slide

  56. Best
    Practices

    View Slide

  57. Gender Neutral

    View Slide

  58. Gender Neutral
    "are you a
    boy or a
    girl?"
    Neither.
    Those X-Wing
    pods are skinny.
    I would not fit
    with any extra
    parts.

    View Slide

  59. Beer Test

    View Slide

  60. Beer Test
    Just keep me
    away from those
    Jawas and we'll
    be cool.

    "you are my
    best friend"

    View Slide

  61. Different Responses

    View Slide

  62. Different Responses
    "how come you had
    rocket feet early on
    but then you didn't
    after episode 3?"
    They were using
    potatoes for
    special effects in
    the first 3.
    How would they
    pull off rocket
    feet?

    View Slide

  63. Different Responses
    "Yeah, but how do
    you explain Jabba
    the Hutt in the
    special edition?"

    View Slide

  64. Different Responses
    "Yeah, but how do
    you explain Jabba
    the Hutt in the
    special edition?"
    They were using
    potatoes for
    special effects in
    the first 3.
    How would they
    pull off rocket
    feet?

    View Slide

  65. Use Their Surroundings

    View Slide

  66. Use Their Surroundings
    "Dagobah did not
    look like a pleasant
    place!"
    It's 70 degrees in
    February where
    you're at.
    Global warming
    is affecting all
    systems.

    View Slide

  67. Fallback

    View Slide

  68. Fallback
    "Can you
    blerg?"
    I'm sorry, that oil
    bath got into
    some of my
    circuits.
    What were you
    saying?

    View Slide

  69. power
    module
    #63wrqy

    View Slide

  70. power
    module
    #63wrqy

    View Slide

  71. speaking
    module
    #S40926

    View Slide

  72. speaking
    module
    #S40926

    View Slide

  73. listen
    module
    #aaYg5

    View Slide

  74. listen
    module
    #aaYg5

    View Slide

  75. process
    module
    #9862sw

    View Slide

  76. process
    module
    #9862sw

    View Slide

  77. View Slide

  78. View Slide

  79. Slack App Directory

    View Slide

  80. Slack App Directory

    View Slide

  81. Add to Slack

    View Slide

  82. Add to Slack

    View Slide

  83. Authorize app

    View Slide

  84. Authorize app

    View Slide

  85. Authorize app

    View Slide

  86. new bot user

    View Slide

  87. new bot user

    View Slide

  88. chat!

    View Slide

  89. power
    module
    #63wrqy

    View Slide

  90. Create Slack App
    http://api.slack.com/applications/new

    View Slide

  91. Create Slack App
    http://api.slack.com/applications/new

    View Slide

  92. ngrok
    ngrok http -subdomain=my-slack-bot 5000

    View Slide

  93. Create Slack App
    http://api.slack.com/applications/new

    View Slide

  94. Web Component

    View Slide

  95. Web Component

    View Slide

  96. Web Component



    View Slide

  97. OAuth
    # Gemfile
    #…
    gem "omniauth-slack"
    #…

    View Slide

  98. OAuth
    #config/initializers/omniauth.rb
    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :slack, ENV["SLACK_CLIENT_ID"], ENV["SLACK_SECRET"],
    scope: "bot,identify"
    end

    View Slide

  99. OAuth
    #config/initializers/omniauth.rb
    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :slack, ENV["SLACK_CLIENT_ID"], ENV["SLACK_SECRET"],
    scope: "bot,identify"
    end

    View Slide

  100. OAuth
    #config/initializers/omniauth.rb
    Rails.application.config.middleware.use OmniAuth::Builder do
    provider :slack, ENV["SLACK_CLIENT_ID"], ENV["SLACK_SECRET"],
    scope: "bot,identify"
    end

    View Slide

  101. routes
    # config/routes.rb
    Rails.application.routes.draw do
    get "/auth/:provider/callback", to: "sessions#create"
    # …
    end

    View Slide

  102. controller
    # app/controllers/sessions_controller.rb
    class SessionsController < ApplicationController
    def create
    auth = request.env["omniauth.auth"]
    end
    end

    View Slide

  103. oauth response
    {"provider"=>"slack",
    "uid"=>"UXXXXXXX",
    "info"=>
    {"nickname"=>"jwright",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX",
    "name"=>nil,
    "email"=>nil,
    "first_name"=>nil,
    # …
    },
    "credentials"=>
    {"token"=>"xoxp-XXXXX-XXXXXXX-XXXXXX",
    "expires"=>false},
    "extra"=>
    {"raw_info"=>
    {"ok"=>true,
    "url"=>"https://bf.slack.com/",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX"},
    "web_hook_info"=>{},
    "bot_info"=>
    {"bot_user_id"=>"UXXXXXX",
    "bot_access_token"=>"xoxb-XXXXXX-XXXXXXXX"}}}}

    View Slide

  104. oauth response
    {"provider"=>"slack",
    "uid"=>"UXXXXXXX",
    "info"=>
    {"nickname"=>"jwright",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX",
    "name"=>nil,
    "email"=>nil,
    "first_name"=>nil,
    # …
    },
    "credentials"=>
    {"token"=>"xoxp-XXXXX-XXXXXXX-XXXXXX",
    "expires"=>false},
    "extra"=>
    {"raw_info"=>
    {"ok"=>true,
    "url"=>"https://bf.slack.com/",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX"},
    "web_hook_info"=>{},
    "bot_info"=>
    {"bot_user_id"=>"UXXXXXX",
    "bot_access_token"=>"xoxb-XXXXXX-XXXXXXXX"}}}}

    View Slide

  105. oauth response
    {"provider"=>"slack",
    "uid"=>"UXXXXXXX",
    "info"=>
    {"nickname"=>"jwright",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX",
    "name"=>nil,
    "email"=>nil,
    "first_name"=>nil,
    # …
    },
    "credentials"=>
    {"token"=>"xoxp-XXXXX-XXXXXXX-XXXXXX",
    "expires"=>false},
    "extra"=>
    {"raw_info"=>
    {"ok"=>true,
    "url"=>"https://bf.slack.com/",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX"},
    "web_hook_info"=>{},
    "bot_info"=>
    {"bot_user_id"=>"UXXXXXX",
    "bot_access_token"=>"xoxb-XXXXXX-XXXXXXXX"}}}}

    View Slide

  106. oauth response
    {"provider"=>"slack",
    "uid"=>"UXXXXXXX",
    "info"=>
    {"nickname"=>"jwright",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX",
    "name"=>nil,
    "email"=>nil,
    "first_name"=>nil,
    # …
    },
    "credentials"=>
    {"token"=>"xoxp-XXXXX-XXXXXXX-XXXXXX",
    "expires"=>false},
    "extra"=>
    {"raw_info"=>
    {"ok"=>true,
    "url"=>"https://bf.slack.com/",
    "team"=>"Brilliant Fantastic",
    "user"=>"jwright",
    "team_id"=>"TXXXXXX",
    "user_id"=>"UXXXXXX"},
    "web_hook_info"=>{},
    "bot_info"=>
    {"bot_user_id"=>"UXXXXXX",
    "bot_access_token"=>"xoxb-XXXXXX-XXXXXXXX"}}}}

    View Slide

  107. Store Information
    # app/controllers/sessions_controller.rb
    class SessionsController < ApplicationController
    def create
    auth = request.env["omniauth.auth"]
    team = Team.create_from_auth auth
    user = User.create_from_auth auth, team
    bot = Bot.create_from_auth auth, team
    end
    end

    View Slide

  108. Store Information
    # db/schema.rb
    create_table "bots", force: :cascade do |t|
    t.string "bot_id"
    t.string "access_token"
    t.integer "team_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["team_id"], name: "index_bots_on_team_id"
    end
    create_table "teams", force: :cascade do |t|
    t.string "access_token"
    t.string "domain"
    t.string "team_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    end
    create_table "users", force: :cascade do |t|
    t.string "user_id"
    t.integer "team_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    end

    View Slide

  109. Start Bot
    # Gemfile
    #…
    gem "slack-ruby-client"
    #…

    View Slide

  110. Start Bot
    EM.next_tick do
    client = Slack::RealTime::Client.new(token: bot.access_token)
    begin
    client.start_async
    client.on(:close) do
    restart
    end
    rescue StandardError => e
    # TODO: handle slew of errors here
    end
    end
    *
    over simplification
    http://api.slack.com/rtm

    View Slide

  111. speaking
    module
    #S40926

    View Slide

  112. Introduce Bot
    client = Slack::Web::Client.new(token: bot.access_token)
    response = client.im_open(user_id: user.user_id)
    client.chat_postMessage(channel: response.channel.id, text: "Hello there")
    http://api.slack.com/chat.postMessage

    View Slide

  113. listen
    module
    #aaYg5

    View Slide

  114. Event listening
    client = Slack::RealTime::Client.new(token: bot.access_token)
    client.on :message do |data|
    case data.text
    when /hello/ then
    client.web_client.chat_postMessage channel: data.channel, text: "Hi!"
    when /not now/ then
    client.web_client.chat_postMessage channel: data.channel, text: "Goodbye"
    end
    end
    http://api.slack.com/events

    View Slide

  115. process
    module
    #9862sw

    View Slide

  116. behaviors
    client = Slack::RealTime::Client.new(token: bot.access_token)
    client.on :message do |data|
    case data.text
    when /hello/ then
    #…
    when /not now/ then
    #…
    when did_something(data.text) then
    #…
    when did_something_else(data.text) then
    #…
    end
    end

    View Slide

  117. behaviors
    class Bot < Slack::RealTime::Client
    include Behaviors::IntroducesSelf
    include Behaviors::RespondsToInsult
    include Behaviors::RespondsToWeatherCommand
    #…
    end

    View Slide

  118. Natural language processing

    View Slide

  119. Natural language processing
    Is BB8 ready to
    go?

    View Slide

  120. Natural language processing
    Is BB8 ready to
    go?
    intent: charge_status

    View Slide

  121. Natural language processing
    Is BB8 ready to
    go?
    intent:
    entities:
    charge_status
    target: "BB8"
    status: "ready"

    View Slide

  122. Natural language processing
    Is BB8 ready to
    go?
    intent:
    entities:
    confidence:
    charge_status
    target: "BB8"
    status: "ready"
    0.82

    View Slide

  123. nlp as a service

    View Slide

  124. nlp as a service

    View Slide

  125. nlp as a service
    require "wit"
    response = Wit.text_query("Is BB8 ready to go?", ENV["WIT_ACCESS_TOKEN"])
    bot.process_intent(response)
    http://api.wit.ai/message

    View Slide

  126. nlp as a service
    "outcomes" : [ {
    "_text" : "Is BB8 ready to go?",
    "intent" : "charge_status",
    "entities" : {
    "target" : [ {
    "value" : "BB8"
    } ],
    "status" : [ {
    "value" : "ready"
    } ],
    },
    "confidence" : 0.82
    } ]

    View Slide

  127. go and build bots

    View Slide

  128. Thank you for learning
    about how to build me.
    go and build bots

    View Slide

  129. go and build bots

    View Slide

  130. I know I'm not as difficult to
    build as that pain in the ass
    gold-plated primadonna.
    go and build bots

    View Slide

  131. go and build bots

    View Slide

  132. …and it took Anakin 7
    years to build that useless
    translator.
    go and build bots

    View Slide

  133. Gotcha bot
    http://github.com/jwright/gotcha-bot

    View Slide

  134. Tatsu
    http://tatsu.io

    View Slide

  135. Tatsu
    http://tatsu.io

    View Slide

  136. Resources
    http://dev4slack.xoxco.com

    View Slide

  137. Resources
    http://api.slack.com

    View Slide

  138. Resources
    http://twitter.com/slackapi

    View Slide

  139. Resources
    http://medium.com/@slackapi

    View Slide

  140. Resources
    http://botweekly.com

    View Slide

  141. @jwright

    View Slide

  142. Thanks for
    listening…
    …to a human?
    @jwright

    View Slide