telegram_meetup_bot 0.1.9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: deffbfe6227e772be37c15193f278374203f84da
4
- data.tar.gz: 63b4b27adfa76e905afc9cb8722a33864ae6adfc
3
+ metadata.gz: 2a70a4786eba6f750122f9b66d8370467d3948b2
4
+ data.tar.gz: 43ff1de5598d0597640cac69431b8e2c3c3503be
5
5
  SHA512:
6
- metadata.gz: 2191c4b91fb5c481df6df5e81bf23e404e47944631af29dd6a336078522710c41b5729919142245572d98030a1a8e35437c1ccb74c0933af478fef30de6dc21d
7
- data.tar.gz: 35f0d9970ef29706e19d62dd811efa2f9f88521e926661ef8ba00c40b54f68defaf6b73e9ee39d24da503532d51129e53e26e458911d05c3d05c8b27cf2ca4e4
6
+ metadata.gz: 02236da88268b705bfcc6a7a190b894ae87a45f5e35393671fa509e7b60c0fd8a10c7c11ebd91399eb6342e78d8570842541704ac2ed0b43b636821bf9f33c8e
7
+ data.tar.gz: f60d3a8bf23cf9e1853097132523cf0c9c2c6af3066e0b9e6c0cef241dc4aa7608f6513398eac3689d5cffa5399293e59541b342f532b99437232f98a03d0318
@@ -0,0 +1,11 @@
1
+ module TelegramMeetupBot
2
+ class CallbackQueryParser < MessageParser
3
+ private
4
+
5
+ def parse_message(&block)
6
+ if message.data && message.data[0] == '/' && message.data.length > 1
7
+ yield message.data[1..-1].split(' ')
8
+ end
9
+ end
10
+ end
11
+ end
@@ -12,7 +12,12 @@ module TelegramMeetupBot
12
12
  Telegram::Bot::Client.run(token) do |bot|
13
13
  bot.enable_botan!(botan_key) if botan_key
14
14
  bot.listen do |message|
15
- process_message(bot, message) if message.text
15
+ case message
16
+ when Telegram::Bot::Types::Message
17
+ process_message(bot, message) if message.text
18
+ when Telegram::Bot::Types::CallbackQuery
19
+ process_callback_query(bot, message) if message.data
20
+ end
16
21
  end
17
22
  end
18
23
  rescue Telegram::Bot::Exceptions::ResponseError => e
@@ -33,6 +38,17 @@ module TelegramMeetupBot
33
38
  ).process
34
39
  end
35
40
 
41
+ def process_callback_query(bot, callback_query)
42
+ messenger = Messenger.new(api: bot.api,
43
+ chat_id: callback_query.message.chat.id,
44
+ message_id: callback_query.message.message_id)
45
+
46
+ CommandsHandler.new(
47
+ callback_query: callback_query,
48
+ messenger: messenger
49
+ ).process
50
+ end
51
+
36
52
  def botan_key
37
53
  Initializers::ConfigLoader.botan_key
38
54
  end
@@ -2,10 +2,12 @@ module TelegramMeetupBot
2
2
  module Commands
3
3
  class ListCommand < Base
4
4
  def exec
5
- handle_date(date) do
5
+ response = handle_date(date) do
6
6
  users = Calendar.formated_users_for_date(date)
7
7
  list_response(list: users, date: date)
8
8
  end
9
+
10
+ [response, build_reply_markup]
9
11
  end
10
12
 
11
13
  private
@@ -15,6 +17,28 @@ module TelegramMeetupBot
15
17
  @parsed_date ||= Date.today if params.empty?
16
18
  @parsed_date
17
19
  end
20
+
21
+ def build_reply_markup
22
+ keys = [[previous_day_key, next_day_key].compact]
23
+
24
+ Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: keys)
25
+ end
26
+
27
+ def previous_day_key
28
+ return nil if date == Date.today
29
+
30
+ Telegram::Bot::Types::InlineKeyboardButton.new(
31
+ text: '⬅️',
32
+ callback_data: "/list #{(date - 1).strftime('%d.%m.%y')}"
33
+ ) rescue nil
34
+ end
35
+
36
+ def next_day_key
37
+ Telegram::Bot::Types::InlineKeyboardButton.new(
38
+ text: '➡️',
39
+ callback_data: "/list #{(date + 1).strftime('%d.%m.%y')}"
40
+ ) rescue nil
41
+ end
18
42
  end
19
43
  end
20
44
  end
@@ -1,18 +1,32 @@
1
1
  module TelegramMeetupBot
2
2
  class CommandsHandler
3
- attr_reader :command, :botan, :messenger
3
+ attr_reader :command, :botan, :messenger, :mode
4
4
 
5
5
  def initialize(args)
6
- parser = MessageParser.new(args.fetch(:message))
6
+ parser = build_parser(args)
7
7
  @command = TelegramMeetupBot::Commands::Factory.build(parser)
8
8
  @messenger = args.fetch(:messenger)
9
9
  @botan = args[:botan]
10
10
  end
11
11
 
12
12
  def process
13
- if command
14
- messenger.send_text(command.exec)
13
+ if command && mode == :send
14
+ messenger.send_text(*command.exec)
15
15
  botan.track(command.command) if botan
16
+ elsif command && mode == :edit
17
+ messenger.edit_text(*command.exec)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def build_parser(args)
24
+ if args[:message]
25
+ @mode = :send
26
+ MessageParser.new(args[:message])
27
+ elsif args[:callback_query]
28
+ @mode = :edit
29
+ CallbackQueryParser.new(args[:callback_query])
16
30
  end
17
31
  end
18
32
  end
@@ -1,22 +1,24 @@
1
1
  module TelegramMeetupBot
2
2
  class Messenger
3
- attr_reader :api, :chat_id
3
+ attr_reader :api, :chat_id, :message_id
4
4
 
5
5
  def initialize(args)
6
6
  @api = args.fetch(:api)
7
7
  @chat_id = args.fetch(:chat_id)
8
+ @message_id = args[:message_id]
8
9
  end
9
10
 
10
- def send_text(text)
11
- api.send_message(chat_id: chat_id, text: text, reply_markup: keyboard)
11
+ def send_text(text, markup = nil)
12
+ return if chat_id.nil?
13
+
14
+ api.send_message(chat_id: chat_id, text: text, reply_markup: markup)
12
15
  end
13
16
 
14
- private
17
+ def edit_text(text, markup = nil)
18
+ return if chat_id.nil? || message_id.nil?
15
19
 
16
- def keyboard
17
- Telegram::Bot::Types::ReplyKeyboardHide.new(
18
- hide_keyboard: true
19
- )
20
+ api.edit_message_text(chat_id: chat_id, message_id: message_id,
21
+ text: text, reply_markup: markup) rescue nil
20
22
  end
21
23
  end
22
24
  end
@@ -1,3 +1,3 @@
1
1
  module TelegramMeetupBot
2
- VERSION = "0.1.9"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,6 +7,7 @@ require 'telegram_meetup_bot/initializers'
7
7
  require 'telegram_meetup_bot/client'
8
8
  require 'telegram_meetup_bot/messenger'
9
9
  require 'telegram_meetup_bot/message_parser'
10
+ require 'telegram_meetup_bot/callback_query_parser'
10
11
  require 'telegram_meetup_bot/storage'
11
12
  require 'telegram_meetup_bot/calendar'
12
13
  require 'telegram_meetup_bot/commands_handler'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: telegram_meetup_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Timur Yanberdin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-26 00:00:00.000000000 Z
11
+ date: 2016-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,6 +128,7 @@ files:
128
128
  - lib/telegram_meetup_bot.rb
129
129
  - lib/telegram_meetup_bot/botan.rb
130
130
  - lib/telegram_meetup_bot/calendar.rb
131
+ - lib/telegram_meetup_bot/callback_query_parser.rb
131
132
  - lib/telegram_meetup_bot/client.rb
132
133
  - lib/telegram_meetup_bot/commands.rb
133
134
  - lib/telegram_meetup_bot/commands/base.rb