Module: Chatterbot::UI

Included in:
Bot
Defined in:
lib/chatterbot/ui.rb

Overview

routines for outputting setup instructions

Constant Summary collapse

API_SIGNUP_URL =

Where to send users who need to get API keys

"https://apps.twitter.com/app/new"

Instance Method Summary collapse

Instance Method Details

#ask_yes_no(q) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/chatterbot/ui.rb', line 64

def ask_yes_no(q)
  prompt = "> "
  response = ""


  while ! ["y", "n"].include?(response)
    puts "#{q} [Y/N]"
    print prompt
    response = $stdin.gets.chomp.downcase[0]
  end
  
  if response == "y"
    true
  else
    false
  end
end

#deprecated(msg, src) ⇒ Object

print out a deprecation notice



21
22
23
24
# File 'lib/chatterbot/ui.rb', line 21

def deprecated(msg, src)
  red(msg)
  green("Called from " + src)
end

#display_oauth_errorObject

error message for auth



177
178
179
# File 'lib/chatterbot/ui.rb', line 177

def display_oauth_error
  STDERR.puts "Oops!  Looks like something went wrong there, please try again!"
end

#get_api_keyObject

Ask the user to get an API key from Twitter.



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/chatterbot/ui.rb', line 103

def get_api_key
  
  green "****************************************"
  green "****************************************"
  green "****        API SETUP TIME!         ****"
  green "****************************************"
  green "****************************************"      


  puts "\n\nWelcome to Chatterbot. Let's walk through the steps to get a bot running.\n\n"


  #
  # At this point, we don't have any API credentials at all for
  # this bot, but it's possible the user has already setup an app.
  # Let's ask!
  #
  
  puts "Hey, looks like you need to get an API key from Twitter before you can get started.\n\n"
  
  app_already_exists = ask_yes_no("Have you already set up an app with Twitter?")

  if app_already_exists
    puts "Terrific! Let's get your bot running!\n\n"
  else
    puts "OK, I can help with that!\n\n"
    send_to_app_creation
  end

  
  print "\n\nPaste the 'Consumer Key' here: "
  STDOUT.flush
  config[:consumer_key] = STDIN.readline.chomp.strip

  print "Paste the 'Consumer Secret' here: "
  STDOUT.flush
  config[:consumer_secret] = STDIN.readline.chomp.strip


  puts "\n\nNow it's time to authorize your bot!\n\n"
  
  if ! app_already_exists && ask_yes_no("Do you want to authorize a bot using the account that created the app?")
    puts "OK, on the app page, you can click the 'Create my access token' button to proceed.\n\n"

    print "Paste the 'Access Token' here: "
    STDOUT.flush
    config[:access_token] = STDIN.readline.chomp.strip


    print "\n\nPaste the 'Access Token Secret' here: "
    STDOUT.flush
    config[:access_token_secret] = STDIN.readline.chomp.strip

  
    # reset the client so we can re-init with new OAuth credentials
    reset_client

    # at this point we should have a fully validated client, so grab
    # the screen name
    @screen_name = client.user.screen_name rescue nil
  else
    reset_client
  end
    
  
  #
  # capture ctrl-c and exit without a stack trace
  #
rescue Interrupt => e
  exit
end

#get_oauth_verifierObject

print out a message about getting a PIN from twitter, then output the URL the user needs to visit to authorize



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/chatterbot/ui.rb', line 30

def get_oauth_verifier
  green "****************************************"
  green "****************************************"
  green "****        BOT AUTH TIME!          ****"
  green "****************************************"
  green "****************************************"      

  puts "You need to authorize your bot with Twitter.\n\nPlease login to Twitter under the bot's account. When you're ready, hit Enter.\n\nYour browser will open with the following URL, where you can authorize the bot.\n\n"

  url = request_token.authorize_url

  puts url

  puts "\nIf that doesn't work, you can open the URL in your browser manually."

  puts "\n\nHit enter to start.\n\n"
  
  STDIN.readline.chomp
  
  Launchy.open(url)

  # sleep here so that if launchy has any output (which it does
  # sometimes), it doesn't interfere with our input prompt
 
  sleep(2)

  puts "Paste your PIN and hit enter when you have completed authorization.\n\n"
  print "> "

  STDIN.readline.chomp.strip
rescue Interrupt => e
  exit
end

#green(str) ⇒ Object



16
17
18
# File 'lib/chatterbot/ui.rb', line 16

def green(str)
  puts str.colorize(:green)
end

#red(str) ⇒ Object

:nocov:



12
13
14
# File 'lib/chatterbot/ui.rb', line 12

def red(str)
  puts str.colorize(:red)
end

#send_to_app_creationObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chatterbot/ui.rb', line 82

def send_to_app_creation
  puts "Please hit enter, and I will send you to #{API_SIGNUP_URL} to start the process."
  puts "(If it doesn't work, you can open a browser and paste the URL in manually)"

  puts "\nHit Enter to continue."
  
  STDIN.readline

  Launchy.open(API_SIGNUP_URL)

  # pause to allow any launchy output
  sleep(2)

  puts "\n\n"


  puts "Once you've filled out the app form, click on the 'Keys and Access Tokens' link"
end