2018-03-20 16:31:49 -07:00
|
|
|
require "./joann-pupper-bot/*"
|
|
|
|
require "logger"
|
|
|
|
require "telepathy"
|
|
|
|
require "time"
|
|
|
|
|
|
|
|
# Chat IDs
|
|
|
|
chatid_joann = 215301902
|
|
|
|
chatid_daniel = 220888832
|
|
|
|
|
|
|
|
# Configuration
|
|
|
|
subreddit = "rarepuppers"
|
2018-07-17 21:41:15 -07:00
|
|
|
chatid = chatid_daniel
|
2018-03-20 16:31:49 -07:00
|
|
|
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|
|
2018-07-17 21:41:15 -07:00
|
|
|
url_tuple = get_reddit_post(subreddit, completed)
|
|
|
|
if url_tuple
|
|
|
|
url, title = url_tuple
|
2018-03-20 16:31:49 -07:00
|
|
|
command_chatid = update.message.as(Telepathy::Message).chat.id
|
|
|
|
logger.info "Using URL #{url} for request from #{command_chatid}"
|
2018-07-17 21:41:15 -07:00
|
|
|
bot.send_photo(command_chatid, url, title)
|
2018-03-20 16:31:49 -07:00
|
|
|
else
|
|
|
|
logger.error "Unable to find a post to send."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
spawn do
|
|
|
|
loop do
|
2019-04-14 14:59:45 -07:00
|
|
|
time = Time.now
|
2018-07-17 21:41:15 -07:00
|
|
|
url_tuple = get_reddit_post(subreddit, completed) if (active_hours.includes? time.hour)
|
|
|
|
if url_tuple
|
|
|
|
url, title = url_tuple
|
2018-03-20 16:31:49 -07:00
|
|
|
logger.info "Sending regular picture to #{chatid}."
|
2018-07-17 21:41:15 -07:00
|
|
|
bot.send_photo(chatid.to_i64, url, title)
|
2018-03-20 16:31:49 -07:00
|
|
|
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
|