|
|
|
@ -5,70 +5,111 @@ require "time" |
|
|
|
|
require "sqlite3" |
|
|
|
|
require "cron_scheduler" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Chat IDs |
|
|
|
|
chatid_joann = 215301902 |
|
|
|
|
chatid_daniel = 220888832 |
|
|
|
|
|
|
|
|
|
# Configuration |
|
|
|
|
database = "sqlite3://./data.sqlite" |
|
|
|
|
subreddit = "rarepuppers" |
|
|
|
|
chatid = chatid_daniel |
|
|
|
|
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 |
|
|
|
|
CHATID_JOANN = 215301902_i64 |
|
|
|
|
CHATID_DANIEL = 220888832_i64 |
|
|
|
|
DATABASE_URL = "sqlite3://./data.sqlite" |
|
|
|
|
BOT_TOKEN = "599474797:AAEmjQNO32uqurI16blS9FT4OoO7GdUZ6h0" |
|
|
|
|
EXTENSIONS = ["png", "jpeg", "jpg"] |
|
|
|
|
LOGGER = Logger.new(STDOUT) |
|
|
|
|
|
|
|
|
|
# Commands. |
|
|
|
|
bot.command "ping" do |update, args| |
|
|
|
|
bot.send_message(update.message.as(Telepathy::Message).chat.id, "pong") |
|
|
|
|
end |
|
|
|
|
class BotConfiguration |
|
|
|
|
JSON.mapping( |
|
|
|
|
subreddits: Array(String), |
|
|
|
|
send_cron: String, |
|
|
|
|
refresh_cron: String, |
|
|
|
|
recepients: Array(Int64)) |
|
|
|
|
|
|
|
|
|
# Tasks |
|
|
|
|
def update_database(db) |
|
|
|
|
def initialize |
|
|
|
|
@subreddits = [ "rarepuppers" ] |
|
|
|
|
@send_cron = "00 8-21 * * *" |
|
|
|
|
@refresh_cron = "*/15 * * * *" |
|
|
|
|
@recepients = [ CHATID_DANIEL ] |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
bot.command "pupper" do |update, args| |
|
|
|
|
url_tuple = get_reddit_post(subreddit, completed) |
|
|
|
|
if url_tuple |
|
|
|
|
url, title = url_tuple |
|
|
|
|
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, title) |
|
|
|
|
else |
|
|
|
|
logger.error "Unable to find a post to send." |
|
|
|
|
class PupperBot |
|
|
|
|
@db : DB::Database |
|
|
|
|
|
|
|
|
|
def initialize(@configuration : BotConfiguration) |
|
|
|
|
@telegram_bot = Telepathy::Bot.new BOT_TOKEN |
|
|
|
|
@db = DB.open DATABASE_URL |
|
|
|
|
initialize_db |
|
|
|
|
update_database |
|
|
|
|
initialize_timers |
|
|
|
|
initialize_telegram |
|
|
|
|
send_broadcast |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def initialize |
|
|
|
|
initialize BotConfiguration.new |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def initialize_db |
|
|
|
|
@db.exec "create table if not exists posts(id integer primary key, title text, url text unique)" |
|
|
|
|
@db.exec "create table if not exists recepient_posts(recepient integer, post integer, foreign key(post) references posts(id))" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
private def initialize_timers |
|
|
|
|
CronScheduler.define do |
|
|
|
|
at(@configuration.send_cron) { send_broadcast } |
|
|
|
|
at(@configuration.refresh_cron) { update_database } |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
spawn do |
|
|
|
|
loop do |
|
|
|
|
time = Time.now |
|
|
|
|
url_tuple = get_reddit_post(subreddit, completed) if (active_hours.includes? time.hour) |
|
|
|
|
if url_tuple |
|
|
|
|
url, title = url_tuple |
|
|
|
|
logger.info "Sending regular picture to #{chatid}." |
|
|
|
|
bot.send_photo(chatid.to_i64, url, title) |
|
|
|
|
else |
|
|
|
|
logger.error "Unable to find a post to send. (Or it's quiet hours)" |
|
|
|
|
end |
|
|
|
|
sleep delay |
|
|
|
|
private def initialize_telegram |
|
|
|
|
@telegram_bot.command "ping" do |update, args| |
|
|
|
|
@telegram_bot.send_message(update.message.as(Telepathy::Message).chat.id, "pong") |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# Code to stop the bot on time. |
|
|
|
|
end_channel = Channel(Nil).new(1) |
|
|
|
|
@telegram_bot.command "pupper" do |update, args| |
|
|
|
|
command_chatid = update.message.as(Telepathy::Message).chat.id |
|
|
|
|
send_single command_chatid |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
bot.poll_end do |
|
|
|
|
end_channel.send nil |
|
|
|
|
end |
|
|
|
|
@telegram_bot.poll |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
Signal::INT.trap do |
|
|
|
|
logger.info "Shutting down bot..." |
|
|
|
|
bot.end_poll |
|
|
|
|
def send_single(chatid) |
|
|
|
|
unsent_query = "select id, title, url from posts where not exists (select * from recepient_posts where recepient=? and post=id) limit 1" |
|
|
|
|
|
|
|
|
|
unless to_send = @db.query_one? unsent_query, chatid, as: { Int64, String, String } |
|
|
|
|
LOGGER.info "Unable to find a post to send to #{chatid}." |
|
|
|
|
return |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
id, title, url = to_send |
|
|
|
|
LOGGER.info "Using URL #{url} for request from #{chatid}" |
|
|
|
|
@db.exec "insert into recepient_posts(recepient, post) values(?, ?)", chatid, id |
|
|
|
|
@telegram_bot.send_photo(chatid, url, title) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def send_broadcast |
|
|
|
|
@configuration.recepients.each do |recepient| |
|
|
|
|
send_single recepient |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def update_database |
|
|
|
|
unless response = RedditResponse.from_subreddits(@configuration.subreddits) |
|
|
|
|
LOGGER.info "Unable to find more posts for the database" |
|
|
|
|
return |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
posts = response.data.posts_matching { |post| EXTENSIONS.any? { |it| post.url.ends_with? it } } |
|
|
|
|
posts.each do |post| |
|
|
|
|
LOGGER.info "Trying to save post #{post.title} #{post.url}" |
|
|
|
|
begin |
|
|
|
|
@db.exec "insert into posts(title, url) values(?, ?)", post.title, post.url |
|
|
|
|
rescue e |
|
|
|
|
puts e |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
bot.poll |
|
|
|
|
end_channel.receive |
|
|
|
|
botc = PupperBot.new |
|
|
|
|
|
|
|
|
|
# Commands. |
|
|
|
|
sleep |
|
|
|
|