Initial commit.
This commit is contained in:
65
src/joann-pupper-bot.cr
Normal file
65
src/joann-pupper-bot.cr
Normal file
@@ -0,0 +1,65 @@
|
||||
require "./joann-pupper-bot/*"
|
||||
require "logger"
|
||||
require "telepathy"
|
||||
require "time"
|
||||
|
||||
# Chat IDs
|
||||
chatid_joann = 215301902
|
||||
chatid_daniel = 220888832
|
||||
|
||||
# Configuration
|
||||
subreddit = "rarepuppers"
|
||||
chatid = chatid_joann
|
||||
delay = 1.hours
|
||||
active_hours = 7..24
|
||||
bot_token = "599474797:AAEmjQNO32uqurI16blS9FT4OoO7GdUZ6h0"
|
||||
|
||||
# Setup
|
||||
completed = [] of String
|
||||
logger = Logger.new(STDOUT)
|
||||
bot = Telepathy::Bot.new bot_token
|
||||
|
||||
# Commands.
|
||||
bot.command "ping" do |update, args|
|
||||
bot.send_message(update.message.as(Telepathy::Message).chat.id, "pong")
|
||||
end
|
||||
|
||||
bot.command "pupper" do |update, args|
|
||||
url = get_reddit_post(subreddit, completed)
|
||||
if url
|
||||
command_chatid = update.message.as(Telepathy::Message).chat.id
|
||||
logger.info "Using URL #{url} for request from #{command_chatid}"
|
||||
bot.send_photo(command_chatid, url)
|
||||
else
|
||||
logger.error "Unable to find a post to send."
|
||||
end
|
||||
end
|
||||
|
||||
spawn do
|
||||
loop do
|
||||
time = Time.now
|
||||
url = get_reddit_post(subreddit, completed) if (active_hours.includes? time.hour)
|
||||
if url
|
||||
logger.info "Sending regular picture to #{chatid}."
|
||||
bot.send_photo(chatid.to_i64, url)
|
||||
else
|
||||
logger.error "Unable to find a post to send. (Or it's quiet hours)"
|
||||
end
|
||||
sleep delay
|
||||
end
|
||||
end
|
||||
|
||||
# Code to stop the bot on time.
|
||||
end_channel = Channel(Nil).new(1)
|
||||
|
||||
bot.poll_end do
|
||||
end_channel.send nil
|
||||
end
|
||||
|
||||
Signal::INT.trap do
|
||||
logger.info "Shutting down bot..."
|
||||
bot.end_poll
|
||||
end
|
||||
|
||||
bot.poll
|
||||
end_channel.receive
|
||||
26
src/joann-pupper-bot/reddit.cr
Normal file
26
src/joann-pupper-bot/reddit.cr
Normal file
@@ -0,0 +1,26 @@
|
||||
require "http/client"
|
||||
require "json"
|
||||
|
||||
def get_reddit_json(subreddit)
|
||||
request_url = "https://www.reddit.com/r/#{subreddit}/hot.json?limit=30"
|
||||
response = HTTP::Client.get(request_url, headers: HTTP::Headers {
|
||||
"User-agent" => "Joann-Pupper-Bot"
|
||||
})
|
||||
response.body?.try { |body| JSON.parse(body) }
|
||||
end
|
||||
|
||||
def filter_reddit_json(json, completed)
|
||||
json["data"]["children"]
|
||||
.map(&.["data"])
|
||||
.select do |it|
|
||||
url = it["url"].as_s
|
||||
name = it["name"].as_s
|
||||
!completed.includes?(name) && (url.ends_with?(".png") || url.ends_with?(".jpg"))
|
||||
end
|
||||
end
|
||||
|
||||
def get_reddit_post(subreddit, completed)
|
||||
json = get_reddit_json(subreddit)
|
||||
post = json.try { |json| filter_reddit_json(json, completed).first? }
|
||||
post.try { |post| completed.push(post["name"].as_s); post["url"].as_s }
|
||||
end
|
||||
Reference in New Issue
Block a user