Search code examples
rubyruby-on-rails-5botsslack

How to extract the values from a string from slack ruby bot


I am trying to create a slackbot which can save incoming data to a database.

I have used the gem slack-ruby-bot and I'm able to receive my text, but I want to extract specific parts of the text, to save in a database.

Given the following received text:

  • I worked on 'stackoverflow' for 10 hours.

I must be able to extract the project name, stackoverflow, and the hour, which is 10. Is there someway to do it?

  command 'activity', '/^Activity/' do |client, data, match|
    client.say(text: "#{data.text}", channel: data.channel)
  end

This is just a sample code which gets the command activity.


Solution

  • Wherever you receive the string, you might use the regular expression and String#scan to get the data.

    "I worked on 'stackoverflow' for 10 hours.".scan /'[^']+'|\d+/
    #⇒ ["'stackoverflow'", "10"]