Class: WhatsappBot

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp_bot.rb

Constant Summary collapse

URL =
"https://web.whatsapp.com/"

Instance Method Summary collapse

Constructor Details

#initializeWhatsappBot

Returns a new instance of WhatsappBot.



6
7
8
9
10
11
# File 'lib/whatsapp_bot.rb', line 6

def initialize
  @driver = Selenium::WebDriver.for :chrome
  @driver.get(URL)
  @driver.find_element(css: '[name="rememberMe"]').click
  @logged_in = false
end

Instance Method Details

#logged_in?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/whatsapp_bot.rb', line 13

def logged_in?
  @logged_in
end

#send_message(to:, text:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/whatsapp_bot.rb', line 25

def send_message(to:, text:)
  if (logged_in?)
    return send_text_message(to, text)
  else
    if (try_logging_in)
      return send_text_message(to, text)
    end
    puts "Not logged in"
    return false
  end
rescue Selenium::WebDriver::Error::NoSuchElementError => e
  puts "Couldnt find the user please check if name is correct or try again after some seconds"
  return false
end

#try_logging_inObject



17
18
19
20
21
22
23
# File 'lib/whatsapp_bot.rb', line 17

def try_logging_in
  @driver.find_element(class: "landing-title")
  @logged_in = false
  puts "Please log in with your phone in the chrome driver"
rescue Selenium::WebDriver::Error::NoSuchElementError => e
  @logged_in = true
end