-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.rb
84 lines (75 loc) · 3.66 KB
/
bot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require_relative 'system/boot'
require 'yaml'
token = ENV.fetch('BOT_TOKEN')
TAG = 'spring2019'.freeze
DATES = (Date.parse(ENV.fetch('DATE_START'))..Date.parse(ENV.fetch('DATE_END'))).freeze
# def rom
# @rom ||= RomContainer.instance
# end
def user_repo
@user_repo ||= Repositories::UserRepo.new
end
def challenge_comments_repo
@challenge_comments_repo ||= Repositories::ChallengeCommentRepo.new
end
def help_text
texts = YAML.load_file("#{__dir__}/locales/ru.yml")
texts.fetch('ru').fetch('help')
end
Telegram::Bot::Client.run(token) do |bot|
bot.listen do |message|
next unless message.chat.id.to_s == ENV.fetch('CHAT_ID').to_s || !message.chat.id.to_s.start_with?('-')
case message.text
when '/start', '/start@days_of_code_bot'
response = "Hello, @#{message.from.username}"
bot.api.send_message(chat_id: message.chat.id, text: response)
when '/table_stats', '/table_stats@days_of_code_bot'
next unless user_repo.by_telegram_id(message.from.id)
response = "```\n#{TablePrint::Printer.new(user_repo.stats).table_print}\n```"
bot.api.send_message(chat_id: message.chat.id, text: response, parse_mode: 'markdown')
when '/my_stats', '/my_stats@days_of_code_bot'
user = user_repo.by_telegram_id(message.from.id)
if user.nil?
response = 'Похоже, ты ещё не записалась на марафон'
else
comment_dates = challenge_comments_repo.stats_by_user(user.id)
table = DATES.each_with_object([]) do |date, arr|
arr.push({ date: date.strftime('%d.%m'), value: (comment_dates.include?(date) ? '+' : '-') })
end
response = "```\n#{TablePrint::Printer.new(table).table_print}\n```"
end
bot.api.send_message(chat_id: message.chat.id, text: response, parse_mode: 'markdown')
when "/recent", '/recent@days_of_code_bot'
# response = ChallengeCommentRepo.new(rom).recent.map(&:text).join("\n---------------------\n")
comments = challenge_comments_repo.recent
response = "```\n#{TablePrint::Printer.new(comments).table_print}\n```"
bot.api.send_message(chat_id: message.chat.id, text: response, parse_mode: 'markdown')
when "/reg", '/reg@days_of_code_bot'
# next unless message.chat.id.to_s == ENV.fetch('CHAT_ID').to_s
result = RegisterUser.call(message.from)
response = result.success? ? "Спасибо, #{message.from.first_name}, записываю 📝" : "#{message.from.first_name}, похоже, ты уже была записана"
bot.api.send_message(chat_id: message.chat.id, text: response)
when "/users", '/users@days_of_code_bot'
response = user_repo.all.to_a.map(&:fullname).join("\n")
response = 'Пока никого нет' if response.to_s.empty?
bot.api.send_message(chat_id: message.chat.id, text: response)
when '/help', '/help@days_of_code_bot'
bot.api.send_message(chat_id: message.chat.id, text: help_text)
when /^(.*)\#spring2019(.*)$/
user = user_repo.by_telegram_id(message.from.id)
if user.nil?
response = 'Похоже, ты ещё не записалась на марафон'
elsif message.text.strip.length == 11
response = 'Нужно всё-таки что-то сделать'
else
result = SaveComment.call(message.text, user.id)
response = result.nil? ? 'Ошибка =(' : "Молодец, #{message.from.first_name}"
end
bot.api.send_message(chat_id: message.chat.id, text: response)
when /^(\s*)\@days_of_code_bot(.+)$/
bot.api.send_message(chat_id: message.chat.id, text: "Спасибо, #{message.from.first_name} 💙")
else
p message.text
end
end
end