diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..b164bd6 --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gem 'telegram-bot-ruby', '~> 2.0' +gem 'rufus-scheduler', '~> 3.9', '>= 3.9.2' +gem 'dotenv' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..962f82f --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,67 @@ +GEM + remote: https://rubygems.org/ + specs: + bigdecimal (3.1.8) + concurrent-ruby (1.3.4) + dotenv (3.1.2) + dry-core (1.0.1) + concurrent-ruby (~> 1.0) + zeitwerk (~> 2.6) + dry-inflector (1.1.0) + dry-logic (1.5.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0, < 2) + zeitwerk (~> 2.6) + dry-struct (1.6.0) + dry-core (~> 1.0, < 2) + dry-types (>= 1.7, < 2) + ice_nine (~> 0.11) + zeitwerk (~> 2.6) + dry-types (1.7.2) + bigdecimal (~> 3.0) + concurrent-ruby (~> 1.0) + dry-core (~> 1.0) + dry-inflector (~> 1.0) + dry-logic (~> 1.4) + zeitwerk (~> 2.6) + et-orbi (1.2.11) + tzinfo + faraday (2.11.0) + faraday-net_http (>= 2.0, < 3.4) + logger + faraday-multipart (1.0.4) + multipart-post (~> 2) + faraday-net_http (3.3.0) + net-http + fugit (1.11.1) + et-orbi (~> 1, >= 1.2.11) + raabro (~> 1.4) + ice_nine (0.11.2) + logger (1.6.1) + multipart-post (2.4.1) + net-http (0.4.1) + uri + raabro (1.4.0) + rufus-scheduler (3.9.2) + fugit (~> 1.1, >= 1.11.1) + telegram-bot-ruby (2.0.0) + dry-struct (~> 1.6) + faraday (~> 2.0) + faraday-multipart (~> 1.0) + zeitwerk (~> 2.6) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + uri (0.13.1) + zeitwerk (2.6.18) + +PLATFORMS + arm64-darwin-23 + ruby + +DEPENDENCIES + dotenv + rufus-scheduler (~> 3.9, >= 3.9.2) + telegram-bot-ruby (~> 2.0) + +BUNDLED WITH + 2.5.17 diff --git a/bin/reminder b/bin/reminder new file mode 100755 index 0000000..ac53196 --- /dev/null +++ b/bin/reminder @@ -0,0 +1,5 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true +require_relative '../lib/reminder' + +Reminder.start \ No newline at end of file diff --git a/lib/reminder.rb b/lib/reminder.rb new file mode 100644 index 0000000..925abb5 --- /dev/null +++ b/lib/reminder.rb @@ -0,0 +1,41 @@ +require 'telegram/bot' +require 'dotenv/load' +require_relative './reminder/jobs' +require_relative './reminder/config' +require_relative './reminder/helpers' +require_relative './reminder/commands/command' +require_relative './reminder/commands/start_command' +require_relative './reminder/commands/bye_command' + +module Reminder + class << self + def start + jobs_thread = Thread.new do + jobs = Reminder::Jobs.new + jobs.perform + end + + bot_thread = Thread.new do + Telegram::Bot::Client.run(Reminder::Config::BOT_TOKEN) do |bot| + Reminder::Config.new(bot) + Reminder::Config.bot.listen do |msg| + if msg.is_a?(Telegram::Bot::Types::Message) + case msg.text + when '/start' + start_command = Reminder::StartCommand.new(msg) + start_command.execute + when '/stop' + bye_command = Reminder::ByeCommand.new(msg) + bye_command.execute + end + else + puts "Received non-message update: #{msg.class}" + end + end + end + end + + [jobs_thread, bot_thread].each(&:join) + end + end +end diff --git a/lib/reminder/commands/bye_command.rb b/lib/reminder/commands/bye_command.rb new file mode 100644 index 0000000..71fb699 --- /dev/null +++ b/lib/reminder/commands/bye_command.rb @@ -0,0 +1,11 @@ +module Reminder + class ByeCommand < Reminder::Command + def initialize(msg) + @msg = msg + end + + def execute + Reminder::Config.bot.api.send_message(chat_id: @msg.chat.id, text: "Bye, #{@msg.from.first_name}") + end + end +end \ No newline at end of file diff --git a/lib/reminder/commands/command.rb b/lib/reminder/commands/command.rb new file mode 100644 index 0000000..1f74c4a --- /dev/null +++ b/lib/reminder/commands/command.rb @@ -0,0 +1,7 @@ +module Reminder + class Command + def execute + raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'" + end + end +end \ No newline at end of file diff --git a/lib/reminder/commands/start_command.rb b/lib/reminder/commands/start_command.rb new file mode 100644 index 0000000..f969c9d --- /dev/null +++ b/lib/reminder/commands/start_command.rb @@ -0,0 +1,12 @@ +module Reminder + class StartCommand < Reminder::Command + def initialize(msg) + @msg = msg + end + + def execute + puts @msg.chat.id + Reminder::Config.bot.api.send_message(chat_id: @msg.chat.id, text: "INIT") + end + end +end \ No newline at end of file diff --git a/lib/reminder/config.rb b/lib/reminder/config.rb new file mode 100644 index 0000000..2601eb8 --- /dev/null +++ b/lib/reminder/config.rb @@ -0,0 +1,15 @@ +module Reminder + class Config + GROUP_ID = ENV.fetch('GROUP_ID') + CHAT_ID = ENV.fetch('CHAT_ID') + BOT_TOKEN = ENV.fetch('BOT_TOKEN') + + class << self + attr_accessor :bot + end + + def initialize(bot) + self.class.bot = bot + end + end +end diff --git a/lib/reminder/helpers.rb b/lib/reminder/helpers.rb new file mode 100644 index 0000000..31a2c97 --- /dev/null +++ b/lib/reminder/helpers.rb @@ -0,0 +1,8 @@ +module Reminder + class Helpers + def self.parse_time_to_cron(schedule_time) + hour, min = schedule_time.split(':').map(&:to_i) + Time.new(Time.now.year, Time.now.month, Time.now.day, hour, min) + end + end +end \ No newline at end of file diff --git a/lib/reminder/jobs.rb b/lib/reminder/jobs.rb new file mode 100644 index 0000000..4c16afc --- /dev/null +++ b/lib/reminder/jobs.rb @@ -0,0 +1,40 @@ +require 'rufus-scheduler' + +module Reminder + class Jobs + def initialize + @scheduler = Rufus::Scheduler.new + + @schedule = [ + { + time: '22:40', + title: "Комп'ютерна логіка", + link: 'https://meet.google.com/ecq-furq-hbq' + }, + { + time: '09:25', + title: "Об'єктно-орієнтоване програмування", + link: 'https://meet.google.com/mcg-icgj-fnq' + } + ] + end + + def perform + @schedule.each do |item| + reminder_time = Reminder::Helpers.parse_time_to_cron(item[:time]) + + @scheduler.at(reminder_time) do + send_reminder(item[:title], item[:link]) + end + end + + @scheduler.join + end + + def send_reminder(title, link) + message = "#{title} \n#{link}" + + Reminder::Config.bot.api.send_message(chat_id: Reminder::Config::CHAT_ID, text: message) + end + end +end