Created
September 29, 2016 02:27
-
-
Save dillonhafer/afd8808d6c8a1ba724274422704e371e to your computer and use it in GitHub Desktop.
Slack bot to get featured geography fact
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'nokogiri' | |
require 'open-uri' | |
require 'json' | |
require 'net/http' | |
def geo_fact | |
page = Nokogiri::HTML(open("https://en.wikipedia.org/wiki/Portal:Geography")) | |
column = page.css('.portal-column-left') | |
if column.css('#Featured_article').text == "Featured article" | |
paragraph = column.css('div')[3].text.gsub('\n', '').strip | |
sentence = paragraph.match(/^[^\.\?!]*/) | |
"#{sentence}." | |
end | |
end | |
def post(fact:, url:) | |
request = Net::HTTP::Post.new(url) | |
request.body = { | |
'username' => 'GeographyBot', | |
'icon_emoji' => ':world_map:', | |
'text' => fact | |
}.to_json | |
uri = URI.parse("https://hooks.slack.com") | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
response = http.request(request) | |
puts response.code | |
puts response.body | |
end | |
url = ENV.fetch('SLACK_URL') | |
post(fact: geo_fact, url: url) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment