# ActiveNotifier Notify message through webhooks. [![Gem Version](https://badge.fury.io/rb/active_notifier.svg)](https://badge.fury.io/rb/active_notifier) [![Build Status](https://travis-ci.org/pinewong/active_notifier.svg)](https://travis-ci.org/pinewong/active_notifier) [![Test Coverage](https://codecov.io/github/pinewong/active_notifier/coverage.svg?branch=master)](https://codecov.io/github/pinewong/active_notifier?branch=master) ## Installation Add this line to your application's Gemfile: ```ruby gem 'active_notifier' ``` And then execute: $ bundle Or install it yourself as: $ gem install active_notifier ## Basic Usage Just notify the message through a webhook token, ActiveNotifier use Dingtalk by default, the webhook token need be a accessible dingtalk webhook token. ```ruby ActiveNotifier.exec(token: "your-webhook-token", message: "your-message") ``` Is it too much trouble to set the token and message always? You can save commonly used ones and access them via better ways. For token, ActiveNotifier use channel_tokens to store it, and for message, we can use the ERB template and locals data, like the familiar ActiveView does. ```ruby ActiveNotifier.configure do |config| config.channel_tokens = { my_channel: "xxx" } config.template_home = Rails.root.join("app", "views", "active_notifier") end ``` ```shell echo "## #{data[:title]\n> #{data[:body]}}" > app/views/active_notifier/my_channel.markdown.erb ``` ```ruby ActiveNotifier.exec(token_channel: :my_channel, template: :my_channel, data: { title: "Message Title", body: "Message Body" }) ``` Also when token_channel and template is the same, we can simplify it by using the first optional argument channel ```ruby ActiveNotifier.exec(:my_channel, data: { title: "Message Title", body: "Message Body" }) ``` ## Advanced knowledge ### Send message with dynamic type Message type will dynamic set according to a valid template, imagine we have two template files named `order.text.erb` and `order.markdown.erb`, now we want use the text type of template, we need add a type option ```ruby ActiveNotifier.exec(:order, data: { title: "Message Title", body: "Message Body" }, type: :text) ``` But if there is only one type of template, the type option is optional ```ruby # Now we have only template file `product.text.erb` for product channel ActiveNotifier.exec(:product, data: { title: "Message Title", body: "Message Body" }) # Needn't type option ``` And also we can configure priority type for ActiveNotifier ```ruby ActiveNotifier.configure do |config| # default is :markdown config.priority_type = :text end ``` ```ruby # Now even we have template files `book.text.erb` and `book.markdown.erb` for book channel ActiveNotifier.exec(:book, data: { title: "Message Title", body: "Message Body" }) # Needn't type option, and will choose the text type ``` ### Set a short constant ActiveSupport hook `after_initialize` will set this by default ```ruby ActiveNotifer.config.const_name = :Notifier Notifer.exec(...) ``` ## Help and Docs * [More Examples](https://github.com/pinewong/active_notifier/blob/master/spec/active_notifier_spec.rb) * [RDoc](https://www.rubydoc.info/github/pinewong/active_notifier) * [Gem RDoc](http://www.rubydoc.info/gems/active_notifier/0.4.0) ## Development After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/pinewong/active_notifier. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. ## Code of Conduct Everyone interacting in the ActiveNotifier project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pinewong/active_notifier/blob/master/CODE_OF_CONDUCT.md).