From 8274bc3a4a99485ba2fc9fd5b762d3e27437c672 Mon Sep 17 00:00:00 2001 From: shine Date: Sun, 28 May 2017 20:53:03 +0530 Subject: [PATCH 01/17] Change birthdays database to YAML / JSON Parsing plain text files requires more processing than structured file types like YAML / JSON. YAML support is built-into ruby > 1.9.3. The YAML module can easily load a JSON file too. Why was the year field removed? Birthdays are recurring. You need to wish people every year. Why would you want to torment people with the notion of them getting old? :stuck_out_tongue_winking_eye: --- birthdays.example.txt | 1 - birthdays.json.example | 17 +++++++++++++++++ birthdays.yaml.example | 9 +++++++++ lib/birthday_reader.rb | 9 ++++----- 4 files changed, 30 insertions(+), 6 deletions(-) delete mode 100644 birthdays.example.txt create mode 100644 birthdays.json.example create mode 100644 birthdays.yaml.example diff --git a/birthdays.example.txt b/birthdays.example.txt deleted file mode 100644 index b3b7ad6..0000000 --- a/birthdays.example.txt +++ /dev/null @@ -1 +0,0 @@ -John Doe 90 05 30 diff --git a/birthdays.json.example b/birthdays.json.example new file mode 100644 index 0000000..076bad8 --- /dev/null +++ b/birthdays.json.example @@ -0,0 +1,17 @@ +{ + 01 : + { + 01 : [ "elvina.slovak" ] + }, + + 02 : + { + 02 : [ "john.doe", "jane.doe" ], + 28 : [ "chris.nolan" ], + }, + + 04 : + { + 15 : [ "erwin.down", "leora.pontious", "randy.young" ] + } +} diff --git a/birthdays.yaml.example b/birthdays.yaml.example new file mode 100644 index 0000000..c87815c --- /dev/null +++ b/birthdays.yaml.example @@ -0,0 +1,9 @@ +01 : + 01 : [ "elvina.slovak" ] + +02 : + 02 : [ "john.doe", "jane.doe" ] + 28 : [ "chris.nolan" ] + +04 + 15 : [ "erwin.down", "leora.pontious", "randy.young" ] diff --git a/lib/birthday_reader.rb b/lib/birthday_reader.rb index 069bca3..edb8d5f 100644 --- a/lib/birthday_reader.rb +++ b/lib/birthday_reader.rb @@ -1,12 +1,11 @@ +require 'yaml' + class BirthdayReader def self.get_birthdays(file_path) birthdays = [] if File.exist?(file_path) - file = File.new(file_path, 'r') - file.each_line do |line| - birthdays << line.chomp.split - end - file.close + bday_list = YAML.load_file(file_path) + birthdays = bday_list[Time.now.month][Time.now.day] end return birthdays end From 17a3ea0eea61ac7a96a581fd20cc9f288178635a Mon Sep 17 00:00:00 2001 From: shine Date: Sun, 28 May 2017 21:14:32 +0530 Subject: [PATCH 02/17] Configurize db_path for the birthdays database --- configurations.json | 1 + lib/birthday_bot.rb | 3 +-- lib/config_reader.rb | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/configurations.json b/configurations.json index e00d4e0..45deba8 100644 --- a/configurations.json +++ b/configurations.json @@ -1,4 +1,5 @@ { + "db_path": "birthdays.yaml", "slack_url": "https://hooks.slack.com/...", "channel_name": "general", "greeting_message": "Happy birthday !", diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 828d212..2fcadf0 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -3,7 +3,6 @@ require 'birthday_reader' class BirthdayBot - @@path = "birthdays.txt" def initialize() @config = ConfigReader.new @@ -11,7 +10,7 @@ def initialize() end def start! - birthdays = BirthdayReader.get_birthdays(@@path) + birthdays = BirthdayReader.get_birthdays(@config.db_path) today = Time.now puts "Checking who was born today (#{today.to_s})" diff --git a/lib/config_reader.rb b/lib/config_reader.rb index c732926..dca9ce5 100644 --- a/lib/config_reader.rb +++ b/lib/config_reader.rb @@ -1,7 +1,8 @@ require 'json' class ConfigReader - attr_accessor :slack_url, + attr_accessor :db_path, + :slack_url, :channel_name, :greeting_message, :bot_name, @@ -11,6 +12,7 @@ def load(filename) if File.exist?(filename) file = File.read(filename) config = JSON.parse(file) + @db_path = config['db_path'] @slack_url = config['slack_url'] @channel_name = config['channel_name'] @greeting_message = config['greeting_message'] From 726d61aa1799e0917aff21cf43adbf5cec2d6cb6 Mon Sep 17 00:00:00 2001 From: shine Date: Sun, 28 May 2017 21:18:11 +0530 Subject: [PATCH 03/17] Rename configurations.json to configuration.json.example because that is what it is - an example / placeholder configuration file --- configurations.json => configurations.json.example | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename configurations.json => configurations.json.example (100%) diff --git a/configurations.json b/configurations.json.example similarity index 100% rename from configurations.json rename to configurations.json.example From c45e1e18069c35765c8ff4923660df116f50dd76 Mon Sep 17 00:00:00 2001 From: shine Date: Sun, 28 May 2017 21:27:16 +0530 Subject: [PATCH 04/17] Optimize bot to work with {hash}ed birthdays Supports multiple birthdays from arrays. Earlier version sent multiple wishes for multiple birthdays, this version appends the users to a single array and sends out a single birthday wish message. --- lib/birthday_bot.rb | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 2fcadf0..3138d45 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -11,17 +11,28 @@ def initialize() def start! birthdays = BirthdayReader.get_birthdays(@config.db_path) - today = Time.now - puts "Checking who was born today (#{today.to_s})" - birthdays.each do |b| - if (b[3].to_i == today.month) && (b[4].to_i == today.day) - message = "#{@config.greeting_message} #{b[0]} #{b[1]}" - HTTParty.post(@config.slack_url, body: { channel: @config.channel_name, - username: @config.bot_name, - text: message, - icon_emoji: @config.bot_emoji }.to_json) + puts "Checking who was born today" + unless birthdays.nil? + users = "<@#{ birthdays[0] }>" + if birthdays.count > 1 + puts "#{ birthdays.count } people were born today" + if birthdays.count == 2 + users = " <@#{ birthdays[0] }> and <@#{ birthdays[1] }> " + else + for i in 1..birthdays.count-2 + users += ", <@#{ birthdays[i] }>" + end + users += " and <@#{ birthdays[i+1] }> " + end end + message = "#{users} #{@config.greeting_message}" + HTTParty.post(@config.slack_url, body: { channel: @config.channel_name, + username: @config.bot_name, + text: message, + icon_emoji: @config.bot_emoji }.to_json) + else + puts "Today is a day that no one was born" end end end From 9f7bc882a7b3ac161c2c992706126ddb474fa8b7 Mon Sep 17 00:00:00 2001 From: shine Date: Mon, 5 Jun 2017 01:55:32 +0530 Subject: [PATCH 05/17] Configurize mention'ing team members You can now configure the birthday database with your team memebers' usernames or real names. The bot will mention the users based on this parameter. --- configurations.json.example | 1 + lib/birthday_bot.rb | 17 +++++++++++++---- lib/config_reader.rb | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/configurations.json.example b/configurations.json.example index 45deba8..2450e60 100644 --- a/configurations.json.example +++ b/configurations.json.example @@ -1,5 +1,6 @@ { "db_path": "birthdays.yaml", + "mention": true, "slack_url": "https://hooks.slack.com/...", "channel_name": "general", "greeting_message": "Happy birthday !", diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 3138d45..d1fced4 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -14,16 +14,16 @@ def start! puts "Checking who was born today" unless birthdays.nil? - users = "<@#{ birthdays[0] }>" + users = "#{ mention birthdays[0] }" if birthdays.count > 1 puts "#{ birthdays.count } people were born today" if birthdays.count == 2 - users = " <@#{ birthdays[0] }> and <@#{ birthdays[1] }> " + users = " #{ mention birthdays[0] } and #{ mention birthdays[1] } " else for i in 1..birthdays.count-2 - users += ", <@#{ birthdays[i] }>" + users.concat( ", #{ mention birthdays[i] }" ) end - users += " and <@#{ birthdays[i+1] }> " + users.concat( " and #{ mention birthdays[i+1] } " ) end end message = "#{users} #{@config.greeting_message}" @@ -35,4 +35,13 @@ def start! puts "Today is a day that no one was born" end end + + def mention ( name ) + if @config.mention + "<@#{ name }>" + else + name + end + end + end diff --git a/lib/config_reader.rb b/lib/config_reader.rb index dca9ce5..50e4edf 100644 --- a/lib/config_reader.rb +++ b/lib/config_reader.rb @@ -2,6 +2,7 @@ class ConfigReader attr_accessor :db_path, + :mention, :slack_url, :channel_name, :greeting_message, @@ -13,6 +14,7 @@ def load(filename) file = File.read(filename) config = JSON.parse(file) @db_path = config['db_path'] + @mention = config['mention'] @slack_url = config['slack_url'] @channel_name = config['channel_name'] @greeting_message = config['greeting_message'] From dfbcc7848dafe06ce2a0d3726c46e803fbd85e25 Mon Sep 17 00:00:00 2001 From: shine Date: Mon, 5 Jun 2017 02:17:29 +0530 Subject: [PATCH 06/17] Update README.md with more detailed instructions --- README.md | 60 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index cfd8bc4..8c1183b 100644 --- a/README.md +++ b/README.md @@ -3,37 +3,59 @@ The purpose of this bot is to send a message to your team's Slack when is someones birthday. -## Setup & Deploy +## Setup + +1. Clone this repo to a desired location on your own computer +2. Configure an [Incoming Webhook URL](https://api.slack.com/incoming-webhooks) from Slack +3. Choose your desired database structure file ( `` : YAML / JSON ). + ( YAML is easier to configure as you have to bother about the indentation with spaces alone. You'll have to bother a lot more with JSON - Parentheses, commas. But in the end it's what you're more comfortable with that matters. ) + 1. Make a copy of the `birthdays..example` file naming it to `birthdays.`. + 2. Populate your birthdays database with the birthdays of your team members. + * NB : You can either use their real names or their usernames. Just remember to configure the mention configuration correctly. +4. Make a copy of the `configurations.json.example` file naming it `configurations.json`. Update the configuration values according to your preferences +``` + { + "db_path" : "birthdays.", # according to your preference + # birthdays.yaml for YAML and birthdays.json for JSON + "mention" : , # depends on your database configuration format + # if you're using real names, set this value to false + # if you're using usernames, set this value to true + "slack_url" : "https://hooks.slack.com/...", # the webhook URL you configured in step 2. + "channel_name" : "", # the channel you want your wishes to be sent to + "greeting_message" : "Happy birthday!" # the wish you would like the bot to send + "bot_name" : "Birthday Bot" # the name of the bot sending the wish + "bot_emoji" : ":tada:" # the emoji the bot should use as it's icon + } +``` + +## Deploy ### Heroku -1. Clone this repo to a desire location at your own computer -2. Get your [Incoming Webhook URL](https://api.slack.com/incoming-webhooks) from Slack -3. Save the url at `configurations.json` file and fill in the rest of the configurations as you like -4. Set your birthdays list using the format `FirstName LastName YY MM DD` at the `birthdays.txt` file -5. Create a blank app at Heroku -6. Push your code to Heroku -7. The bot was developed with the **2.2.6** version of ruby, any other versions may require changes. -7. Run `heroku addons:create scheduler:standard` to add the Scheduler add-on to your deploy -8. Run `heroku addons:open scheduler` to configure the scheduler -9. Click **Add a new job** and type `rake congratulate` as the command -10. Set frequency to **Daily** and choose the **Time** you want to be notified +1. Create a blank app at Heroku +2. Push your code to Heroku +3. The bot was developed with the **2.2.6** version of ruby, any other versions may require changes. +4. Run `heroku addons:create scheduler:standard` to add the Scheduler add-on to your deploy +5. Run `heroku addons:open scheduler` to configure the scheduler +6. Click **Add a new job** and type `rake congratulate` as the command +7. Set frequency to **Daily** and choose the **Time** you want to be notified ### Custom Server -1. Clone this repo to a desire location at your own server -2. Get your [Incoming Webhook URL](https://api.slack.com/incoming-webhooks) from Slack -3. Save the url at `configurations.json` file and fill in the rest of the configurations as you like -4. Set your birthdays list using the format `FirstName LastName YY MM DD` at the `birthdays.txt` file -5. Run `crontab -e` to edit your crontab -6. Add this line to the crontab and save it: `0 9 * * * cd /clone/location && /usr/local/bin/rake congratulate` (replace `/clone/location` by the location where you cloned the repo) +1. Run `crontab -e` to edit your crontab +2. Add this line to the crontab and save it: + `@daily cd /clone/location && /usr/local/bin/rake congratulate` + * replace `/clone/location` with the location where you cloned the repo) + * the `@daily` `cron` shorthand might vary among servers and/or distributions. + Replace `@daily` with a time of your preference according to the [`cron` syntax](http://tldp.org/LDP/lame/LAME/linux-admin-made-easy/using-cron.html). + * Remember to verify the timezone setting of your server or you won't get the expected result. ## Contributors This project was originally created by [Tiago Botelho](https://github.com/tiagonbotelho), while he was an intern at [jeKnowledge](http://jeknowledge.pt/). -It was later revised by [Diogo Nunes](http://www.diogonunes.com/) from [EqualExperts](https://www.equalexperts.com/) and [João Bernardo](http://jbernardo.me) from [jeKnowledge](http://jeknowledge.pt/). +It was later revised by [Diogo Nunes](http://www.diogonunes.com/) from [EqualExperts](https://www.equalexperts.com/), [João Bernardo](http://jbernardo.me) from [jeKnowledge](http://jeknowledge.pt/) and @shinenelson. ## License From d40912255998a85142bcfb3535b093118e4d714b Mon Sep 17 00:00:00 2001 From: shine Date: Mon, 5 Jun 2017 04:26:10 +0530 Subject: [PATCH 07/17] Rename db_path to birthdays_path (Resolves https://github.com/jeKnowledge/slack-birthday-bot/pull/9#discussion_r120021476) --- configurations.json.example | 2 +- lib/birthday_bot.rb | 2 +- lib/config_reader.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/configurations.json.example b/configurations.json.example index 2450e60..24f2a39 100644 --- a/configurations.json.example +++ b/configurations.json.example @@ -1,5 +1,5 @@ { - "db_path": "birthdays.yaml", + "birthdays_path": "birthdays.yaml", "mention": true, "slack_url": "https://hooks.slack.com/...", "channel_name": "general", diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index d1fced4..005b557 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -10,7 +10,7 @@ def initialize() end def start! - birthdays = BirthdayReader.get_birthdays(@config.db_path) + birthdays = BirthdayReader.get_birthdays(@config.birthdays_path) puts "Checking who was born today" unless birthdays.nil? diff --git a/lib/config_reader.rb b/lib/config_reader.rb index 50e4edf..14efe9c 100644 --- a/lib/config_reader.rb +++ b/lib/config_reader.rb @@ -1,7 +1,7 @@ require 'json' class ConfigReader - attr_accessor :db_path, + attr_accessor :birthdays_path, :mention, :slack_url, :channel_name, @@ -13,7 +13,7 @@ def load(filename) if File.exist?(filename) file = File.read(filename) config = JSON.parse(file) - @db_path = config['db_path'] + @birthdays_path = config['birthdays_path'] @mention = config['mention'] @slack_url = config['slack_url'] @channel_name = config['channel_name'] From 077a104368fde06f6cc69a6946aee6215a57a94a Mon Sep 17 00:00:00 2001 From: shine Date: Mon, 5 Jun 2017 04:31:16 +0530 Subject: [PATCH 08/17] Log Time.now --- lib/birthday_bot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 005b557..bc0480c 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -12,7 +12,7 @@ def initialize() def start! birthdays = BirthdayReader.get_birthdays(@config.birthdays_path) - puts "Checking who was born today" + puts "Checking who was born today ( #{ Time.now.to_s } )" unless birthdays.nil? users = "#{ mention birthdays[0] }" if birthdays.count > 1 From 2ee35b3baf792c59bb5d0008366e0bb4bb7e91f6 Mon Sep 17 00:00:00 2001 From: shine Date: Tue, 1 Aug 2017 23:16:14 +0530 Subject: [PATCH 09/17] change date keys to string The YAML module parses keys with a leading 0 as octcal digits, invalidating 08 and 09. Parsing them as strings would be safer --- birthdays.json.example | 14 +++++++------- birthdays.yaml.example | 14 +++++++------- lib/birthday_reader.rb | 2 +- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/birthdays.json.example b/birthdays.json.example index 076bad8..2526c30 100644 --- a/birthdays.json.example +++ b/birthdays.json.example @@ -1,17 +1,17 @@ { - 01 : + "01" : { - 01 : [ "elvina.slovak" ] + "01" : [ "elvina.slovak" ] }, - 02 : + "02" : { - 02 : [ "john.doe", "jane.doe" ], - 28 : [ "chris.nolan" ], + "02" : [ "john.doe", "jane.doe" ], + "28" : [ "chris.nolan" ], }, - 04 : + "04" : { - 15 : [ "erwin.down", "leora.pontious", "randy.young" ] + "15" : [ "erwin.down", "leora.pontious", "randy.young" ] } } diff --git a/birthdays.yaml.example b/birthdays.yaml.example index c87815c..1ae5e6f 100644 --- a/birthdays.yaml.example +++ b/birthdays.yaml.example @@ -1,9 +1,9 @@ -01 : - 01 : [ "elvina.slovak" ] +"01" : + "01" : [ "elvina.slovak" ] -02 : - 02 : [ "john.doe", "jane.doe" ] - 28 : [ "chris.nolan" ] +"02" : + "02" : [ "john.doe", "jane.doe" ] + "28" : [ "chris.nolan" ] -04 - 15 : [ "erwin.down", "leora.pontious", "randy.young" ] +"04" : + "15" : [ "erwin.down", "leora.pontious", "randy.young" ] diff --git a/lib/birthday_reader.rb b/lib/birthday_reader.rb index edb8d5f..6fc1015 100644 --- a/lib/birthday_reader.rb +++ b/lib/birthday_reader.rb @@ -5,7 +5,7 @@ def self.get_birthdays(file_path) birthdays = [] if File.exist?(file_path) bday_list = YAML.load_file(file_path) - birthdays = bday_list[Time.now.month][Time.now.day] + birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] end return birthdays end From 9b83de9852e09eb176e894f514eec1348a838e8c Mon Sep 17 00:00:00 2001 From: shine Date: Tue, 1 Aug 2017 23:21:38 +0530 Subject: [PATCH 10/17] handle empty / nil YAML.load_file --- lib/birthday_reader.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/birthday_reader.rb b/lib/birthday_reader.rb index 6fc1015..8b0b706 100644 --- a/lib/birthday_reader.rb +++ b/lib/birthday_reader.rb @@ -5,7 +5,9 @@ def self.get_birthdays(file_path) birthdays = [] if File.exist?(file_path) bday_list = YAML.load_file(file_path) - birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] + unless bday_list.nil? || bday_list.empty? + birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] + end end return birthdays end From ed5c9cb4deff0de2a118b9f997c602d4ca8d1a21 Mon Sep 17 00:00:00 2001 From: shine Date: Thu, 3 Aug 2017 22:47:53 +0530 Subject: [PATCH 11/17] remove spaces (Resolves https://github.com/jeKnowledge/slack-birthday-bot/pull/9#discussion_r130992162) --- lib/birthday_bot.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index bc0480c..bdb53eb 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -12,7 +12,7 @@ def initialize() def start! birthdays = BirthdayReader.get_birthdays(@config.birthdays_path) - puts "Checking who was born today ( #{ Time.now.to_s } )" + puts "Checking who was born today (#{Time.now.to_s})" unless birthdays.nil? users = "#{ mention birthdays[0] }" if birthdays.count > 1 From e901b71b9d96eea93bd7528f549e7f93c2fad97e Mon Sep 17 00:00:00 2001 From: shine Date: Tue, 8 Aug 2017 01:13:15 +0530 Subject: [PATCH 12/17] move birthday list filter to birthday_bot from birthday_reader --- lib/birthday_bot.rb | 6 +++++- lib/birthday_reader.rb | 7 +------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index bdb53eb..2c9c851 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -10,7 +10,11 @@ def initialize() end def start! - birthdays = BirthdayReader.get_birthdays(@config.birthdays_path) + bday_list = BirthdayReader.get_birthdays(@config.birthdays_path) + + unless bday_list.nil? || bday_list.empty? + birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] + end puts "Checking who was born today (#{Time.now.to_s})" unless birthdays.nil? diff --git a/lib/birthday_reader.rb b/lib/birthday_reader.rb index 8b0b706..572b9df 100644 --- a/lib/birthday_reader.rb +++ b/lib/birthday_reader.rb @@ -2,13 +2,8 @@ class BirthdayReader def self.get_birthdays(file_path) - birthdays = [] if File.exist?(file_path) - bday_list = YAML.load_file(file_path) - unless bday_list.nil? || bday_list.empty? - birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] - end + return YAML.load_file(file_path) end - return birthdays end end From f4dbfe8ce1c453ba78d6de2986d33a848f01c704 Mon Sep 17 00:00:00 2001 From: shine Date: Tue, 8 Aug 2017 01:16:44 +0530 Subject: [PATCH 13/17] add today = Time.now (Resolves https://github.com/jeKnowledge/slack-birthday-bot/pull/9#discussion_r120022086) --- lib/birthday_bot.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 2c9c851..5d387c8 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -12,11 +12,12 @@ def initialize() def start! bday_list = BirthdayReader.get_birthdays(@config.birthdays_path) + today = Time.now unless bday_list.nil? || bday_list.empty? - birthdays = bday_list[Time.now.month.to_s][Time.now.day.to_s] + birthdays = bday_list[today.month.to_s][today.day.to_s] end - puts "Checking who was born today (#{Time.now.to_s})" + puts "Checking who was born today (#{today.to_s})" unless birthdays.nil? users = "#{ mention birthdays[0] }" if birthdays.count > 1 From e3a0763ef0bff7c56612de46e47cf269d4d51412 Mon Sep 17 00:00:00 2001 From: shine Date: Tue, 8 Aug 2017 01:18:39 +0530 Subject: [PATCH 14/17] def build_user_list --- lib/birthday_bot.rb | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index 5d387c8..a64ef42 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -10,27 +10,13 @@ def initialize() end def start! - bday_list = BirthdayReader.get_birthdays(@config.birthdays_path) + birthdays = BirthdayReader.get_birthdays(@config.birthdays_path) today = Time.now - unless bday_list.nil? || bday_list.empty? - birthdays = bday_list[today.month.to_s][today.day.to_s] - end - puts "Checking who was born today (#{today.to_s})" - unless birthdays.nil? - users = "#{ mention birthdays[0] }" - if birthdays.count > 1 - puts "#{ birthdays.count } people were born today" - if birthdays.count == 2 - users = " #{ mention birthdays[0] } and #{ mention birthdays[1] } " - else - for i in 1..birthdays.count-2 - users.concat( ", #{ mention birthdays[i] }" ) - end - users.concat( " and #{ mention birthdays[i+1] } " ) - end - end + unless birthdays.nil? || birthdays.empty? + birthdays_today = birthdays[today.month.to_s][today.day.to_s] + users = build_user_list ( birthdays_today ) message = "#{users} #{@config.greeting_message}" HTTParty.post(@config.slack_url, body: { channel: @config.channel_name, username: @config.bot_name, @@ -41,6 +27,19 @@ def start! end end + def build_user_list ( birthdays ) + if birthdays.count == 1 + mention birthdays.first + else + users = "" + puts "#{ birthdays.count } people were born today" + birthdays.take(birthdays.count - 2).each do | birthday | + users += mention(birthday) + ', ' + end + users += "#{ mention birthdays[birthdays.count - 2] } and #{ mention birthdays[birthdays.count - 1] }" if birthdays.count > 1 + end + end + def mention ( name ) if @config.mention "<@#{ name }>" From 56c8b077be0f7d1e5bd5acac6a7163bff857e5d5 Mon Sep 17 00:00:00 2001 From: shine <4771718+shinenelson@users.noreply.github.com> Date: Thu, 21 Nov 2019 18:58:09 +0530 Subject: [PATCH 15/17] [refactor] remove httparty dependency HTTParty is the only gem dependency of the bot. I thought it was redundant to maintain a single dependency especially when it was achievable with the standard net/http library except that it would some extra lines of code. I thought the extra lines of code was worth it rather than having to maintain a single dependency. --- Gemfile | 3 --- Gemfile.lock | 17 ----------------- lib/birthday_bot.rb | 18 +++++++++++++----- 3 files changed, 13 insertions(+), 25 deletions(-) delete mode 100644 Gemfile delete mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile deleted file mode 100644 index 58ecf0b..0000000 --- a/Gemfile +++ /dev/null @@ -1,3 +0,0 @@ -source "https://rubygems.org" - -gem 'httparty' diff --git a/Gemfile.lock b/Gemfile.lock deleted file mode 100644 index 9ef3ada..0000000 --- a/Gemfile.lock +++ /dev/null @@ -1,17 +0,0 @@ -GEM - remote: https://rubygems.org/ - specs: - httparty (0.13.7) - json (~> 1.8) - multi_xml (>= 0.5.2) - json (1.8.3) - multi_xml (0.5.5) - -PLATFORMS - ruby - -DEPENDENCIES - httparty - -BUNDLED WITH - 1.12.3 diff --git a/lib/birthday_bot.rb b/lib/birthday_bot.rb index a64ef42..fe14562 100644 --- a/lib/birthday_bot.rb +++ b/lib/birthday_bot.rb @@ -1,4 +1,4 @@ -require 'httparty' +require 'net/http' require 'config_reader' require 'birthday_reader' @@ -18,10 +18,18 @@ def start! birthdays_today = birthdays[today.month.to_s][today.day.to_s] users = build_user_list ( birthdays_today ) message = "#{users} #{@config.greeting_message}" - HTTParty.post(@config.slack_url, body: { channel: @config.channel_name, - username: @config.bot_name, - text: message, - icon_emoji: @config.bot_emoji }.to_json) + payload = { channel: @config.channel_name, + username: @config.bot_name, + text: message, + icon_emoji: @config.bot_emoji }.to_json + + uri = URI.parse(@config.slack_url) + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true + + request = Net::HTTP::Post.new(uri.path) + request.body = payload + http.request(request) else puts "Today is a day that no one was born" end From e64afd9089d75897b69ba2a3f5f64f5e37efd152 Mon Sep 17 00:00:00 2001 From: shine <4771718+shinenelson@users.noreply.github.com> Date: Thu, 21 Nov 2019 17:18:27 +0530 Subject: [PATCH 16/17] [docs] replace db_path with birthdays_path --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8c1183b..6cf4417 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ The purpose of this bot is to send a message to your team's Slack when is someon 4. Make a copy of the `configurations.json.example` file naming it `configurations.json`. Update the configuration values according to your preferences ``` { - "db_path" : "birthdays.", # according to your preference + "birthdays_path" : "birthdays.", # according to your preference # birthdays.yaml for YAML and birthdays.json for JSON "mention" : , # depends on your database configuration format # if you're using real names, set this value to false From 05965169618ade46d8de6315191a37624f39f055 Mon Sep 17 00:00:00 2001 From: shine <4771718+shinenelson@users.noreply.github.com> Date: Thu, 21 Nov 2019 17:19:51 +0530 Subject: [PATCH 17/17] [readme] fix URL in contributors' list --- .gitignore | 4 +++- README.md | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index b23653a..841ede0 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,8 @@ build-iPhoneSimulator/ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this: .rvmrc -birthdays.txt +birthdays.y*ml +birthdays.json +configurations.json .DS_Store diff --git a/README.md b/README.md index 6cf4417..a4384fb 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ The purpose of this bot is to send a message to your team's Slack when is someon This project was originally created by [Tiago Botelho](https://github.com/tiagonbotelho), while he was an intern at [jeKnowledge](http://jeknowledge.pt/). -It was later revised by [Diogo Nunes](http://www.diogonunes.com/) from [EqualExperts](https://www.equalexperts.com/), [João Bernardo](http://jbernardo.me) from [jeKnowledge](http://jeknowledge.pt/) and @shinenelson. +It was later revised by [Diogo Nunes](http://www.diogonunes.com/) from [EqualExperts](https://www.equalexperts.com/), [João Bernardo](http://jbernardo.me) from [jeKnowledge](http://jeknowledge.pt/) and then refactored again by [Shine Nelson](https://www.shinenelson.com). ## License