telegram-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/CHANGELOG.md +4 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +97 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/examples/api/inline_keyboard_markup.rb +17 -0
  13. data/examples/api/send_message.rb +5 -0
  14. data/examples/client/handler.rb +19 -0
  15. data/lib/telegram/bot.rb +29 -0
  16. data/lib/telegram/bot/api.rb +261 -0
  17. data/lib/telegram/bot/client.rb +54 -0
  18. data/lib/telegram/bot/client_accessors.rb +5 -0
  19. data/lib/telegram/bot/configuration.rb +11 -0
  20. data/lib/telegram/bot/exceptions.rb +13 -0
  21. data/lib/telegram/bot/types.rb +47 -0
  22. data/lib/telegram/bot/types/animation.rb +17 -0
  23. data/lib/telegram/bot/types/audio.rb +14 -0
  24. data/lib/telegram/bot/types/callback_game.rb +4 -0
  25. data/lib/telegram/bot/types/callback_query.rb +23 -0
  26. data/lib/telegram/bot/types/chat.rb +25 -0
  27. data/lib/telegram/bot/types/chat_member.rb +28 -0
  28. data/lib/telegram/bot/types/chat_photo.rb +10 -0
  29. data/lib/telegram/bot/types/chosen_inline_result.rb +21 -0
  30. data/lib/telegram/bot/types/contact.rb +12 -0
  31. data/lib/telegram/bot/types/document.rb +17 -0
  32. data/lib/telegram/bot/types/file.rb +15 -0
  33. data/lib/telegram/bot/types/force_reply.rb +9 -0
  34. data/lib/telegram/bot/types/game.rb +26 -0
  35. data/lib/telegram/bot/types/inline_keyboard_button.rb +19 -0
  36. data/lib/telegram/bot/types/inline_keyboard_markup.rb +20 -0
  37. data/lib/telegram/bot/types/inline_query.rb +21 -0
  38. data/lib/telegram/bot/types/invoice.rb +13 -0
  39. data/lib/telegram/bot/types/keyboard_button.rb +11 -0
  40. data/lib/telegram/bot/types/location.rb +10 -0
  41. data/lib/telegram/bot/types/mask_position.rb +12 -0
  42. data/lib/telegram/bot/types/message.rb +145 -0
  43. data/lib/telegram/bot/types/message_entity.rb +17 -0
  44. data/lib/telegram/bot/types/order_info.rb +16 -0
  45. data/lib/telegram/bot/types/photo_size.rb +12 -0
  46. data/lib/telegram/bot/types/pre_checkout_query.rb +23 -0
  47. data/lib/telegram/bot/types/reply_keyboard_markup.rb +22 -0
  48. data/lib/telegram/bot/types/reply_keyboard_remove.rb +9 -0
  49. data/lib/telegram/bot/types/shipping_address.rb +14 -0
  50. data/lib/telegram/bot/types/shipping_query.rb +20 -0
  51. data/lib/telegram/bot/types/sticker.rb +24 -0
  52. data/lib/telegram/bot/types/sticker_set.rb +16 -0
  53. data/lib/telegram/bot/types/successful_payment.rb +19 -0
  54. data/lib/telegram/bot/types/update.rb +54 -0
  55. data/lib/telegram/bot/types/user.rb +14 -0
  56. data/lib/telegram/bot/types/user_profile_photos.rb +14 -0
  57. data/lib/telegram/bot/types/venue.rb +16 -0
  58. data/lib/telegram/bot/types/video.rb +19 -0
  59. data/lib/telegram/bot/types/video_note.rb +17 -0
  60. data/lib/telegram/bot/types/voice.rb +12 -0
  61. data/lib/telegram/bot/types/webhook_info.rb +15 -0
  62. data/lib/telegram/version.rb +3 -0
  63. data/telegram-ruby.gemspec +29 -0
  64. metadata +176 -0
@@ -0,0 +1,54 @@
1
+ module Telegram::Bot
2
+ PAYLOAD_KEYS = %w(message edited_message channel_post edited_channel_post).freeze
3
+
4
+ class Client
5
+ attr_reader :update, :message, :command
6
+
7
+ def initialize(handler)
8
+ @handler = handler.new
9
+ @handler.api = Telegram::Bot::Api.new
10
+ end
11
+
12
+ def handle(params)
13
+ parse_request(params)
14
+ invoke
15
+ end
16
+
17
+ def invoke
18
+ @handler.update = @update
19
+ @handler.message = @message
20
+ @handler.command = @command
21
+
22
+ if !@command.nil? && @handler.respond_to?("cmd_#{@command}")
23
+ @handler.public_send("cmd_#{@command}")
24
+ else
25
+ @handler.public_send(:cmd_fallback) if @handler.respond_to?(:cmd_fallback)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def parse_request(params)
32
+ @update = Types::Update.new(params)
33
+
34
+ if params.has_key? :callback_query
35
+ @message = @update.callback_query.message
36
+ @command = pull_command(@update.callback_query.data)
37
+ else
38
+ message_key = PAYLOAD_KEYS.find { |key| params.has_key?(key.to_sym) }
39
+
40
+ if !message_key.nil? && @update.respond_to?(message_key)
41
+ @message = @update.send(message_key)
42
+ @command = pull_command(@message.text)
43
+ @command = pull_command(@message.reply_to_message.text) if !@command && @message.reply_to_message
44
+ end
45
+ end
46
+
47
+ raise Exceptions::ParseError.new('Can not parse request') unless @message
48
+ end
49
+
50
+ def pull_command(text)
51
+ !text.nil? && text.index('/') == 0 ? text.downcase.split(' ').first.gsub('/', '') : nil
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ module Telegram::Bot
2
+ module ClientAccessors
3
+ attr_accessor :api, :update, :message, :command
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module Telegram
2
+ module Bot
3
+ class Configuration
4
+ attr_accessor :token, :raise_exceptions
5
+
6
+ def initialize
7
+ @raise_exceptions = true
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Telegram
2
+ module Bot
3
+ module Exceptions
4
+ class Error < StandardError; end
5
+
6
+ class ResponseError < Error; end
7
+
8
+ class ParseError < Error; end
9
+
10
+ class KeyboardMarkupError < Error; end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,47 @@
1
+ require 'telegram/bot/types/audio'
2
+ require 'telegram/bot/types/animation'
3
+ require 'telegram/bot/types/sticker'
4
+ require 'telegram/bot/types/game'
5
+ require 'telegram/bot/types/photo_size'
6
+ require 'telegram/bot/types/mask_position'
7
+ require 'telegram/bot/types/video'
8
+ require 'telegram/bot/types/video_note'
9
+ require 'telegram/bot/types/chat'
10
+ require 'telegram/bot/types/chat_member'
11
+ require 'telegram/bot/types/chat_photo'
12
+ require 'telegram/bot/types/contact'
13
+ require 'telegram/bot/types/location'
14
+ require 'telegram/bot/types/venue'
15
+ require 'telegram/bot/types/invoice'
16
+ require 'telegram/bot/types/successful_payment'
17
+ require 'telegram/bot/types/order_info'
18
+ require 'telegram/bot/types/shipping_address'
19
+ require 'telegram/bot/types/file'
20
+ require 'telegram/bot/types/voice'
21
+ require 'telegram/bot/types/user'
22
+ require 'telegram/bot/types/message'
23
+ require 'telegram/bot/types/message_entity'
24
+ require 'telegram/bot/types/webhook_info'
25
+ require 'telegram/bot/types/update'
26
+ require 'telegram/bot/types/inline_query'
27
+ require 'telegram/bot/types/chosen_inline_result'
28
+ require 'telegram/bot/types/callback_query'
29
+ require 'telegram/bot/types/shipping_query'
30
+ require 'telegram/bot/types/pre_checkout_query'
31
+ require 'telegram/bot/types/user_profile_photos'
32
+ require 'telegram/bot/types/sticker_set'
33
+ require 'telegram/bot/types/inline_keyboard_markup'
34
+ require 'telegram/bot/types/inline_keyboard_button'
35
+ require 'telegram/bot/types/callback_game'
36
+ require 'telegram/bot/types/reply_keyboard_markup'
37
+ require 'telegram/bot/types/keyboard_button'
38
+ require 'telegram/bot/types/reply_keyboard_remove'
39
+ require 'telegram/bot/types/force_reply'
40
+
41
+ module Telegram
42
+ module Bot
43
+ module Types
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,17 @@
1
+ module Telegram::Bot::Types
2
+ class Animation
3
+ attr_accessor :file_id,
4
+ :thumb,
5
+ :file_name,
6
+ :mime_type,
7
+ :file_size
8
+
9
+ def initialize(attributes)
10
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
11
+ end
12
+
13
+ def thumb=(attributes)
14
+ @thumb = PhotoSize.new(attributes)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module Telegram::Bot::Types
2
+ class Audio
3
+ attr_accessor :file_id,
4
+ :duration,
5
+ :performer,
6
+ :title,
7
+ :mime_type,
8
+ :file_size
9
+
10
+ def initialize(attributes)
11
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,4 @@
1
+ module Telegram::Bot::Types
2
+ class CallbackGame
3
+ end
4
+ end
@@ -0,0 +1,23 @@
1
+ module Telegram::Bot::Types
2
+ class CallbackQuery
3
+ attr_accessor :id,
4
+ :from,
5
+ :message,
6
+ :inline_message_id,
7
+ :chat_instance,
8
+ :data,
9
+ :game_short_name
10
+
11
+ def initialize(attributes)
12
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
13
+ end
14
+
15
+ def from=(attributes)
16
+ @from = User.new(attributes)
17
+ end
18
+
19
+ def message=(attributes)
20
+ @message = Message.new(attributes)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module Telegram::Bot::Types
2
+ class Chat
3
+ attr_accessor :id,
4
+ :type,
5
+ :title,
6
+ :username,
7
+ :first_name,
8
+ :last_name,
9
+ :all_members_are_administrators,
10
+ :photo,
11
+ :description,
12
+ :invite_link,
13
+ :pinned_message,
14
+ :sticker_set_name,
15
+ :can_set_sticker_set
16
+
17
+ def initialize(attributes)
18
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
19
+ end
20
+
21
+ def photo=(attributes)
22
+ @photo = ChatPhoto.new(attributes)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module Telegram::Bot::Types
2
+ class ChatMember
3
+ attr_accessor :user,
4
+ :status,
5
+ :until_date,
6
+ :can_be_edited,
7
+ :can_change_info,
8
+ :can_post_messages,
9
+ :can_edit_messages,
10
+ :can_delete_messages,
11
+ :can_invite_users,
12
+ :can_restrict_members,
13
+ :can_pin_messages,
14
+ :can_promote_members,
15
+ :can_send_messages,
16
+ :can_send_media_messages,
17
+ :can_send_other_messages,
18
+ :can_add_web_page_previews
19
+
20
+ def initialize(attributes)
21
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
22
+ end
23
+
24
+ def user=(attributes)
25
+ @user = User.new(attributes)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,10 @@
1
+ module Telegram::Bot::Types
2
+ class ChatPhoto
3
+ attr_accessor :small_file_id,
4
+ :big_file_id
5
+
6
+ def initialize(attributes)
7
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ module Telegram::Bot::Types
2
+ class ChosenInlineResult
3
+ attr_accessor :result_id,
4
+ :from,
5
+ :location,
6
+ :inline_message_id,
7
+ :query
8
+
9
+ def initialize(attributes)
10
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
11
+ end
12
+
13
+ def from=(attributes)
14
+ @from = User.new(attr)
15
+ end
16
+
17
+ def location=(attributes)
18
+ @location = Location.new(attr)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Telegram::Bot::Types
2
+ class Contact
3
+ attr_accessor :phone_number,
4
+ :first_name,
5
+ :last_name,
6
+ :user_id
7
+
8
+ def initialize(attributes)
9
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ module Telegram::Bot::Types
2
+ class Document
3
+ attr_accessor :file_id,
4
+ :thumb,
5
+ :file_name,
6
+ :mime_type,
7
+ :file_size
8
+
9
+ def initialize(attributes)
10
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
11
+ end
12
+
13
+ def thumb=(attributes)
14
+ @thumb = PhotoSize.new(attributes)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module Telegram::Bot::Types
2
+ class File
3
+ attr_accessor :file_id,
4
+ :file_size,
5
+ :file_path
6
+
7
+ def initialize(attributes)
8
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
9
+ end
10
+
11
+ def file_path=(file_path)
12
+ @file_path = file_path
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ module Telegram::Bot::Types
2
+ class ForceReply
3
+ attr_accessor :force_reply, :selective
4
+
5
+ def initialize(attributes)
6
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ module Telegram::Bot::Types
2
+ class Game
3
+ attr_accessor :title,
4
+ :description,
5
+ :photo,
6
+ :text,
7
+ :text_entities,
8
+ :animation
9
+
10
+ def initialize(attributes)
11
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
12
+ end
13
+
14
+ def photo=(attributes)
15
+ @photo = attributes.map {|attr| PhotoSize.new(attr) }
16
+ end
17
+
18
+ def text_entities=(attributes)
19
+ @text_entities = MessageEntity.new(attributes)
20
+ end
21
+
22
+ def animation=(attributes)
23
+ @animation = Animation.new(attributes)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,19 @@
1
+ module Telegram::Bot::Types
2
+ class InlineKeyboardButton
3
+ attr_accessor :text,
4
+ :url,
5
+ :callback_data,
6
+ :switch_inline_query,
7
+ :switch_inline_query_current_chat,
8
+ :callback_game,
9
+ :pay
10
+
11
+ def initialize(attributes)
12
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
13
+ end
14
+
15
+ def callback_game=(attributes)
16
+ @callback_game = CallbackGame.new(attributes)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Telegram::Bot::Types
2
+ class InlineKeyboardMarkup
3
+ attr_accessor :inline_keyboard
4
+
5
+ def initialize(attributes)
6
+ @inline_keyboard = attributes.map do |v|
7
+ v.map do |b|
8
+ if b.is_a?(InlineKeyboardButton)
9
+ b
10
+ elsif b.is_a?(Hash)
11
+ InlineKeyboardButton.new(b)
12
+ else
13
+ raise Telegram::Bot::Exceptions::KeyboardMarkupError
14
+ .new('Attributes must be Array of Array of Telegram::Bot::Types::InlineKeyboardButton or Hash')
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module Telegram::Bot::Types
2
+ class InlineQuery
3
+ attr_accessor :id,
4
+ :from,
5
+ :location,
6
+ :query,
7
+ :offset
8
+
9
+ def initialize(attributes)
10
+ attributes.each { |k, v| self.send("#{k}=", v) if self.respond_to? k }
11
+ end
12
+
13
+ def from=(attributes)
14
+ @from = User.new(attr)
15
+ end
16
+
17
+ def location=(attributes)
18
+ @location = Location.new(attr)
19
+ end
20
+ end
21
+ end