-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 19d1242
Showing
6 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.log | ||
*.lock | ||
*.DS_Store | ||
*.pstore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'telegram-bot-ruby' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'telegram/bot' | ||
require_relative './lib/kabanchiki' | ||
require_relative './lib/telegram_bot_api_patch' | ||
|
||
puts "[#{Time.now}] Bot started" | ||
Telegram::Bot::Client.run(ENV['TOKEN']) do |bot| | ||
bot.listen do |message| | ||
case message | ||
when Telegram::Bot::Types::CallbackQuery | ||
chat_id = message.message.chat.id | ||
game = Kabanchiki.games[chat_id] | ||
|
||
if message.data['🐗'] | ||
if game | ||
game.new_bet(message.from.username, message.data, message.id) | ||
else | ||
bot.api.answer_callback_query( | ||
callback_query_id: message.id, | ||
text: "Эти кабанчики уже подскочили!" | ||
) | ||
end | ||
end | ||
|
||
when Telegram::Bot::Types::Message | ||
case message.text | ||
when '/bet@KabanchikiBot' | ||
if Kabanchiki.games[message.chat.id] | ||
bot.api.send_message(chat_id: message.chat.id, text: 'Кабанчики ещё на подскоке...') | ||
else | ||
game = Kabanchiki.new(bot, message.chat.id) | ||
Thread.start{ game.countdown } | ||
end | ||
when '/top@KabanchikiBot' | ||
bot.api.send_message(chat_id: message.chat.id, text: Kabanchiki.chat_top(message.chat.id)) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# sudo cp kabanchiki_bot.service /lib/systemd/system -v | ||
# sudo systemctl enable kabanchiki_bot.service | ||
# sudo systemctl start kabanchiki_bot.service | ||
[Unit] | ||
Description=Kabanchiki bot | ||
|
||
[Service] | ||
User=ubuntu | ||
Group=ubuntu | ||
WorkingDirectory=/home/ubuntu/kabanchiki_bot | ||
Restart=always | ||
Environment="TOKEN=bot_token" | ||
ExecStart=/usr/bin/ruby kabanchiki_bot.rb | ||
|
||
[Install] | ||
WantedBy=multi-user.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
require 'pstore' | ||
|
||
class Kabanchiki | ||
|
||
class << self | ||
def games | ||
@@chats ||= {} | ||
end | ||
|
||
def store | ||
@@store ||= PStore.new("#{__dir__}/kabanchiki_bot.pstore") | ||
end | ||
|
||
def chat_top(chat_id) | ||
text = "Топ чата: \n\n" | ||
store.transaction(true) do | ||
users = (store[chat_id] || {}).sort_by{|k, v| -v} | ||
users.each{|u| text << "#{u.first} : #{u.last}\n"} unless users.empty? | ||
end | ||
text | ||
end | ||
end | ||
|
||
attr_accessor :bot, :chat_id, :bets, :kabanchiki, :started_at, :message_id, :timer | ||
|
||
def initialize(bot, chat_id) | ||
@bot = bot | ||
@chat_id = chat_id | ||
@bets = {} | ||
@kabanchiki = ['🐗 1', '🐗 2', '🐗 3'] | ||
self.class.games[chat_id] = self | ||
@message_id = nil | ||
end | ||
|
||
def countdown | ||
self.message_id = bot.api.send_message( | ||
chat_id: chat_id, | ||
text: 'Кто подскочет первым?', | ||
reply_markup: build_buttons | ||
).dig('result', 'message_id') | ||
3.downto(0).each do |t| | ||
self.timer = t | ||
bot.api.edit_message_reply_markup( | ||
chat_id: chat_id, | ||
message_id: message_id, | ||
reply_markup: build_buttons | ||
) | ||
sleep 3 | ||
end | ||
race | ||
end | ||
|
||
def new_bet(username, data, callback_query_id) | ||
bets[username] = data | ||
bot.api.answer_callback_query( | ||
callback_query_id: callback_query_id, | ||
text: "Кабанчик #{data} выбран!" | ||
) | ||
end | ||
|
||
def race | ||
result = [] | ||
3.times do |t| | ||
places = [1, 2, 3].shuffle | ||
text = "Погнали!\n" | ||
kabanchiki.each do |kaban| | ||
place = places.pop | ||
text << (' . ' * place) << kaban | ||
text << "\n" | ||
result[place - 1] = kaban | ||
end | ||
text << (" . " * (t + 1)) << "\n" | ||
bot.api.edit_message_text(chat_id: chat_id, message_id: message_id, text: text) | ||
sleep 1.5 | ||
end | ||
|
||
award result.first | ||
end | ||
|
||
def award(winner) | ||
result = "Кабанчик #{winner} подскочил первым! \n\n" | ||
right_bets = [] | ||
bets.each do |user, bet| | ||
right_bets << user if bet === winner | ||
end | ||
|
||
unless right_bets.empty? | ||
result << right_bets.join(', ') | ||
result << ' поставил(и) на правильного кабанчика!' | ||
update_chat_top(right_bets) | ||
end | ||
|
||
bot.api.send_message(chat_id: chat_id, text: result) | ||
Kabanchiki.games[chat_id] = nil | ||
end | ||
|
||
def build_buttons | ||
buttons = [] | ||
kabanchiki.each do |kaban| | ||
text = bets.size > 0 ? "#{kaban} (#{bets.values.count(kaban)})" : kaban | ||
buttons << Telegram::Bot::Types::InlineKeyboardButton.new( | ||
text: text, | ||
callback_data: kaban | ||
) | ||
end | ||
|
||
buttons << Telegram::Bot::Types::InlineKeyboardButton.new( | ||
text: "Кабанчики метнутся через #{timer}", | ||
callback_data: 'kabanchiki_timer' | ||
) | ||
Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: buttons) | ||
end | ||
|
||
def update_chat_top(users) | ||
store = self.class.store | ||
store.transaction do | ||
new_top = {} | ||
old_top = store[chat_id] || {} | ||
users.each do |user| | ||
new_top[user] = old_top[user].to_i + 1 | ||
end | ||
store[chat_id] = old_top.merge(new_top) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Telegram | ||
module Bot | ||
class Api | ||
def call(endpoint, raw_params = {}) | ||
params = build_params(raw_params) | ||
response = conn.post("/bot#{token}/#{endpoint}", params) | ||
if response.status == 200 | ||
JSON.parse(response.body) | ||
else | ||
puts "[#{Time.now}] API error: #{response.status} #{response.body}" | ||
end | ||
end | ||
end | ||
end | ||
end |