Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use hooks with slack-ruby-bot #154

Closed
kanwardeep007 opened this issue Aug 30, 2017 · 3 comments
Closed

How to use hooks with slack-ruby-bot #154

kanwardeep007 opened this issue Aug 30, 2017 · 3 comments

Comments

@kanwardeep007
Copy link

kanwardeep007 commented Aug 30, 2017

I have started making a bot for attendance where a user will be marked as present as soon as his presence_change attribute for slack RTM is active and exit when this is away. I want to implement a web hook for this event. I am currently using rails as the framework in which I have the following code

class PongBot < SlackRubyBot::Bot
  ENV['SLACK_API_TOKEN'] = Rails.application.secrets.slack_api_token
  match 'present' do |client, data, match|
    user_id = "#{data[:user]}"
    user = Employee.find_by(slack_id: user_id)
    Attendance.create(entry: DateTime.now,employee_id: user.id)
    client.say(text: "Marked <@#{data.user}> as present at #{DateTime.now.strftime('%B %d, %Y  %I:%M:%S %p')}", channel: data.channel)
  end
end

Can some one kindly guide me as to how to implement hooks for this purpose and also how to add custom functionality for this gem. I am new to adding custom functionality to gems and how to make use of the existing libraries.

Help appreciated.
:)

@dblock
Copy link
Collaborator

dblock commented Aug 30, 2017

There's lots of detail in https://github.com/slack-ruby/slack-ruby-bot#hooks. You need to get hold of the server instance, then do on :presence_change. Depending on where you start this instance of PongBot, you probably just want PongBot.instance.on :presence_change.

PongBot.instance.on(:presence_change, ->(client, data) { ... })

I'll close this for now, but feel free to add more questions!

@skolsuper
Copy link

skolsuper commented Jan 16, 2019

@dblock first of all, thanks so much for writing this gem! As a noob to ruby, it's not clear how to "get hold of the server instance" from inside my SlackRubyBot::Bot subclass' body. An example in the README would be great if you have time.

It would be nice if some common hooks could be defined alongside command, e.g. :

  class MyBot < SlackRubyBot::Bot
    command 'ping' do |client, data|
      client.say(text: 'pong', channel: data.channel)
    end

    hello do |client, data|
      ...
    end
    
    # or
    hook 'hello' do |client, data|
      ...
    end
  end

@dblock
Copy link
Collaborator

dblock commented Jan 16, 2019

I agree. Please try to contribute!

I opened #211

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants