Skip to content

Commit

Permalink
feat: bot version 0.1.0, add simple jobs, hard setupschedule, setup c…
Browse files Browse the repository at this point in the history
…onfig, setup simple commands
  • Loading branch information
kkkiikkk committed Sep 15, 2024
1 parent 574567e commit 86f83b6
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -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'
67 changes: 67 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions bin/reminder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/reminder'

Reminder.start
41 changes: 41 additions & 0 deletions lib/reminder.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/reminder/commands/bye_command.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/reminder/commands/command.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Reminder
class Command
def execute
raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
end
end
12 changes: 12 additions & 0 deletions lib/reminder/commands/start_command.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/reminder/config.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions lib/reminder/helpers.rb
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions lib/reminder/jobs.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 86f83b6

Please sign in to comment.