-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram_bot.rb
196 lines (155 loc) · 5.1 KB
/
telegram_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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
require_relative "credentials"
require "telegram/bot"
WIN_COMBINATIONS = [
[:A1, :A2, :A3], [:B1, :B2, :B3], [:C1, :C2, :C3], # Horizontal
[:A1, :B1, :C1], [:A2, :B2, :C2], [:A3, :B3, :C3], # Vertical
[:A1, :B2, :C3], [:A3, :B2, :C1] # Diagonal
].freeze
class TelegramBot
attr_accessor :game, :user_value, :bot_value, :winner
def run
@game = new_game
@user_value = nil
@bot_value = nil
bot.listen do |message|
@message = message
new_message(message, @game, @user_value, @bot_value)
end
end
def bot
Telegram::Bot::Client.run(TELEGRAM_TOKEN) { |bot| return bot }
end
def new_message(message, game, user_value, bot_value)
return if message.text.nil?
my_message = message.text.include?("⬜️") ? "⬜️" : message.text
case my_message
when "/start"
bot.api.send_message(chat_id: message.chat.id, text: "Hi #{message.from.first_name}, let's play the game")
menu
when "Start a new game"
@game = new_game
select_fighter(message)
when "I'll start: ✖️"
pin_sides(my_message)
next_step
when "You first: ⚫️"
pin_sides(my_message)
bot_choose(game, bot_value)
next_step
when "⬜️"
my_value = message.text[-3..-2].to_sym
@game[my_value] = user_value
if game_over?(game)
finish_game
else
if @game.values.include?('⬜️')
bot_choose(game, bot_value)
if game_over?(game)
finish_game
else
@game.values.include?('⬜️') ? next_step : nobody_win
end
else
nobody_win
end
end
when "I'm out"
kb = Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: [[{ text: "/start" }]], one_time_keyboard: true)
bot.api.send_message(chat_id: message.chat.id, text: 'Sorry to see you go :(', reply_markup: kb)
@game = nil
else
@user_value.nil? ? menu : invalid_input
end
end
def new_game
{
'A1': "⬜️" , 'A2': "⬜️" , 'A3': "⬜️" , 'next_1': "\n",
'B1': "⬜️" , 'B2': "⬜️" , 'B3': "⬜️" , 'next_2': "\n",
'C1': "⬜️" , 'C2': "⬜️" , 'C3': "⬜️"
}
end
def select_fighter(message)
answers =
Telegram::Bot::Types::ReplyKeyboardMarkup.new(
keyboard: [
[{ text: "I'll start: ✖️" }, { text: "You first: ⚫️" }],
],
one_time_keyboard: true
)
bot.api.send_message(chat_id: message.chat.id, text: 'Choose your fighter?', reply_markup: answers)
end
def collect_keyboard(game)
new_keyboard = []
line_hash = []
@game.each do |key, value|
if key[0] != "n"
line_hash << {text: "#{value} (#{key})"}
else
new_keyboard << line_hash
line_hash = []
end
end
new_keyboard << line_hash
Telegram::Bot::Types::ReplyKeyboardMarkup.new(keyboard: new_keyboard, one_time_keyboard: true)
end
def bot_choose(game, bot_value)
available = []
@game.map do |key,value|
if value == "⬜️"
available << key
end
end
@game[available[rand(available.length)]] = @bot_value
end
def game_over?(game)
@winner = nil
WIN_COMBINATIONS.each do |combo|
values = combo.map { |position| game[position] }
if values.uniq.length == 1 && values[0] != "⬜️"
@winner = values[0]
break
end
end
if @winner.nil?
false
else
@winner_message = (user_value == @winner) ? "You win, great 🏆" : "Bot wins, better luck next time 😔"
end
end
def menu
answers =
Telegram::Bot::Types::ReplyKeyboardMarkup.new(
keyboard: [
[{ text: "Start a new game" }, { text: "I'm out" }],
],
one_time_keyboard: true
)
bot.api.send_message(chat_id: @message.chat.id, text: 'What you want to do?', reply_markup: answers)
@user_value = nil
@bot_value = nil
end
def nobody_win
bot.api.send_message(chat_id: @message.chat.id, text: @game.values.join(''))
bot.api.send_message(chat_id: @message.chat.id, text: 'No one wins, great game')
menu
end
def finish_game
bot.api.send_message(chat_id: @message.chat.id, text: @game.values.join(''))
bot.api.send_message(chat_id: @message.chat.id, text: @winner_message)
menu
end
def next_step
bot.api.send_message(chat_id: @message.chat.id, text: 'Select one:')
bot.api.send_message(chat_id: @message.chat.id, text: @game.values.join(''),reply_markup: collect_keyboard(@game))
end
def pin_sides(side)
@user_value = (side.include?("✖️")) ? "✖️" : "⚫️"
@bot_value = (side.include?("✖️")) ? "⚫️" : "✖️"
pinned = bot.api.send_message(chat_id: @message.chat.id, text: "Bot:#{@bot_value}\nYou:#{@user_value}")
bot.api.pin_chat_message(chat_id: @message.chat.id, message_id: pinned['result']['message_id'])
end
def invalid_input
bot.api.send_message(chat_id: @message.chat.id, text: "It's not correct")
bot.api.send_message(chat_id: @message.chat.id, text: @game.values.join(''),reply_markup: collect_keyboard(game))
end
end