fantastic_robot 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +117 -0
- data/.ruby-version +1 -0
- data/.travis.yml +19 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +48 -0
- data/LICENSE +22 -0
- data/README.md +147 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/fantastic_robot.gemspec +32 -0
- data/lib/fantastic_robot/configuration.rb +32 -0
- data/lib/fantastic_robot/connection.rb +36 -0
- data/lib/fantastic_robot/model/attachment/audio.rb +12 -0
- data/lib/fantastic_robot/model/attachment/base.rb +15 -0
- data/lib/fantastic_robot/model/attachment/contact.rb +11 -0
- data/lib/fantastic_robot/model/attachment/document.rb +16 -0
- data/lib/fantastic_robot/model/attachment/location.rb +10 -0
- data/lib/fantastic_robot/model/attachment/photo_size.rb +12 -0
- data/lib/fantastic_robot/model/attachment/sticker.rb +17 -0
- data/lib/fantastic_robot/model/attachment/video.rb +17 -0
- data/lib/fantastic_robot/model/attachment/voice.rb +12 -0
- data/lib/fantastic_robot/model/base.rb +68 -0
- data/lib/fantastic_robot/model/chat.rb +14 -0
- data/lib/fantastic_robot/model/file.rb +14 -0
- data/lib/fantastic_robot/model/message.rb +51 -0
- data/lib/fantastic_robot/model/update.rb +15 -0
- data/lib/fantastic_robot/model/user.rb +11 -0
- data/lib/fantastic_robot/models.rb +6 -0
- data/lib/fantastic_robot/request/base.rb +59 -0
- data/lib/fantastic_robot/request/forward_message.rb +18 -0
- data/lib/fantastic_robot/request/get_me.rb +9 -0
- data/lib/fantastic_robot/request/send_audio.rb +32 -0
- data/lib/fantastic_robot/request/send_chat_action.rb +18 -0
- data/lib/fantastic_robot/request/send_document.rb +32 -0
- data/lib/fantastic_robot/request/send_location.rb +17 -0
- data/lib/fantastic_robot/request/send_message.rb +17 -0
- data/lib/fantastic_robot/request/send_photo.rb +16 -0
- data/lib/fantastic_robot/request/send_sticker.rb +18 -0
- data/lib/fantastic_robot/request/send_video.rb +32 -0
- data/lib/fantastic_robot/request/send_voice.rb +32 -0
- data/lib/fantastic_robot/request/set_webhook.rb +29 -0
- data/lib/fantastic_robot/requests.rb +13 -0
- data/lib/fantastic_robot/version.rb +3 -0
- data/lib/fantastic_robot.rb +56 -0
- data/lib/generators/fantastic_robot/files/fantastic_robot.rb +12 -0
- data/lib/generators/fantastic_robot/install_generator.rb +13 -0
- metadata +197 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a voice note.
|
3
|
+
class Model::Attachment::Voice < Model::Attachment::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :duration, :mime_type
|
6
|
+
|
7
|
+
validates :file_id, :duration, presence: true
|
8
|
+
validates :duration, numericality: true
|
9
|
+
validates :file_size, numericality: true, unless: 'file_size.blank?'
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
module Model
|
3
|
+
class Base
|
4
|
+
include ::ActiveModel::Model
|
5
|
+
include ::ActiveModel::Validations
|
6
|
+
include ::ActiveModel::Serialization
|
7
|
+
include ::ActiveModel::Serializers::JSON
|
8
|
+
|
9
|
+
# As the models include a lot of different objects related,
|
10
|
+
# we build a conversion hash that indicates the type of objects
|
11
|
+
# required in special fields.
|
12
|
+
FIELD_CONVERSIONS = {}
|
13
|
+
|
14
|
+
# Initializer that converts the fields received if necessary
|
15
|
+
def initialize(attributes = {})
|
16
|
+
|
17
|
+
# Define a setter for each field that need to be converted
|
18
|
+
self.class::FIELD_CONVERSIONS.each do |field_name, field_type|
|
19
|
+
self.class.send(:define_method, "#{field_name}=") do |value|
|
20
|
+
if (value.is_a?(Array))
|
21
|
+
# If we receive an array, process each value
|
22
|
+
value.map! do |v|
|
23
|
+
# Convert each object except if it is already converted or nil
|
24
|
+
v = ((v.class == field_type || v.nil?) ? v : field_type.new(v))
|
25
|
+
end
|
26
|
+
else
|
27
|
+
# If it isn't an array, try to convert the object
|
28
|
+
value = field_type.new(value) unless (value.class == field_type || value.nil?)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Set the result
|
32
|
+
instance_variable_set("@#{field_name}", value)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
super(attributes)
|
37
|
+
end
|
38
|
+
|
39
|
+
# Attribute map for ActiveModel::Serialization.
|
40
|
+
def attributes
|
41
|
+
Hash[instance_variables.map{|attrib| [attrib.to_s[1..attrib.to_s.size], nil]}]
|
42
|
+
end
|
43
|
+
|
44
|
+
# Proxy to get a serialized version of the object.
|
45
|
+
def to_h
|
46
|
+
recursive_serialization(self)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
# Method to try to recursively seralize the objects received
|
52
|
+
def recursive_serialization object
|
53
|
+
if (object.is_a?(Array))
|
54
|
+
# If it's an array, try to serialize each element
|
55
|
+
return object.map{|o| recursive_serialization(o)}
|
56
|
+
elsif (object.is_a?(Hash))
|
57
|
+
# It's a hash, convert each key to sym and try to serialize each value
|
58
|
+
return Hash[object.map{|k,v| [k.to_sym, recursive_serialization(v)]}]
|
59
|
+
elsif (object.respond_to?(:serializable_hash))
|
60
|
+
# If it can be seralized, serialize and try to recursively convert it's attributes
|
61
|
+
return recursive_serialization(object.serializable_hash)
|
62
|
+
else
|
63
|
+
return object
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a chat.
|
3
|
+
class Model::Chat < Model::Base
|
4
|
+
|
5
|
+
VALID_TYPES = ["private", "group", "supergroup", "channel"]
|
6
|
+
|
7
|
+
attr_accessor :id, :type, :title, :username, :first_name, :last_name
|
8
|
+
|
9
|
+
validates :id, :type, presence: true
|
10
|
+
validates :id, numericality: true
|
11
|
+
validates :type, inclusion: {in: VALID_TYPES}
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a file.
|
3
|
+
class Model::File < Model::Base
|
4
|
+
|
5
|
+
attr_accessor :file_id, :file_size, :file_path
|
6
|
+
|
7
|
+
validates :file_id, presence: true
|
8
|
+
validates :file_size, numericality: true, allow_blank: true
|
9
|
+
|
10
|
+
def self.file
|
11
|
+
FantasticRobot.connection.get_by_path(self.file_path) unless self.file_path.blank?
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'fantastic_robot/model/attachment/base'
|
2
|
+
require 'fantastic_robot/model/attachment/photo_size'
|
3
|
+
require 'fantastic_robot/model/attachment/audio'
|
4
|
+
require 'fantastic_robot/model/attachment/contact'
|
5
|
+
require 'fantastic_robot/model/attachment/document'
|
6
|
+
require 'fantastic_robot/model/attachment/location'
|
7
|
+
require 'fantastic_robot/model/attachment/sticker'
|
8
|
+
require 'fantastic_robot/model/attachment/video'
|
9
|
+
require 'fantastic_robot/model/attachment/voice'
|
10
|
+
|
11
|
+
module FantasticRobot
|
12
|
+
class Model::Message < Model::Base
|
13
|
+
|
14
|
+
attr_accessor :message_id, :from, :date, :chat, :forward_from, :fordward_date,
|
15
|
+
:reply_to_message
|
16
|
+
|
17
|
+
validates :message_id, :date, :chat, presence: true
|
18
|
+
validates :message_id, :date, numericality: true
|
19
|
+
validates :fordward_date, numericality: true, unless: 'fordward_date.blank?'
|
20
|
+
|
21
|
+
attr_accessor :text, :audio, :document, :photo, :sticker, :video, :voice,
|
22
|
+
:caption, :contact, :location
|
23
|
+
|
24
|
+
attr_accessor :new_chat_participant, :left_chat_participant, :new_chat_title, :new_chat_photo,
|
25
|
+
:delete_chat_photo, :group_chat_created, :supergroup_chat_created,
|
26
|
+
:channel_chat_created, :migrate_to_chat_id, :migrate_from_chat_id
|
27
|
+
|
28
|
+
validates :migrate_to_chat_id, numericality: true, unless: 'migrate_to_chat_id.blank?'
|
29
|
+
validates :migrate_from_chat_id, numericality: true, unless: 'migrate_from_chat_id.blank?'
|
30
|
+
|
31
|
+
# Field conversions of this model
|
32
|
+
FIELD_CONVERSIONS = {
|
33
|
+
from: FantasticRobot::Model::User,
|
34
|
+
chat: FantasticRobot::Model::Chat,
|
35
|
+
forward_from: FantasticRobot::Model::User,
|
36
|
+
reply_to_message: FantasticRobot::Model::Message,
|
37
|
+
audio: FantasticRobot::Model::Attachment::Audio,
|
38
|
+
document: FantasticRobot::Model::Attachment::Document,
|
39
|
+
photo: FantasticRobot::Model::Attachment::PhotoSize,
|
40
|
+
sticker: FantasticRobot::Model::Attachment::Sticker,
|
41
|
+
video: FantasticRobot::Model::Attachment::Video,
|
42
|
+
voice: FantasticRobot::Model::Attachment::Voice,
|
43
|
+
contact: FantasticRobot::Model::Attachment::Contact,
|
44
|
+
location: FantasticRobot::Model::Attachment::Location,
|
45
|
+
new_chat_participant: FantasticRobot::Model::User,
|
46
|
+
left_chat_participant: FantasticRobot::Model::User,
|
47
|
+
new_chat_photo: FantasticRobot::Model::Attachment::PhotoSize
|
48
|
+
}
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
class Model::Update < Model::Base
|
3
|
+
|
4
|
+
attr_accessor :update_id, :message
|
5
|
+
|
6
|
+
validates :update_id, presence: true
|
7
|
+
validates :update_id, numericality: true
|
8
|
+
|
9
|
+
# Field conversions of this model
|
10
|
+
FIELD_CONVERSIONS = {
|
11
|
+
message: FantasticRobot::Model::Message
|
12
|
+
}
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a Telegram user or bot.
|
3
|
+
class Model::User < Model::Base
|
4
|
+
|
5
|
+
attr_accessor :id, :first_name, :last_name, :username
|
6
|
+
|
7
|
+
validates :id, :first_name, presence: true
|
8
|
+
validates :id, numericality: true
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
module Request
|
3
|
+
class Base
|
4
|
+
include ::ActiveModel::Model
|
5
|
+
include ::ActiveModel::Validations
|
6
|
+
include ::ActiveModel::Serialization
|
7
|
+
include ::ActiveModel::Serializers::JSON
|
8
|
+
|
9
|
+
attr_reader :method
|
10
|
+
|
11
|
+
# Initializer that assigns the fields received
|
12
|
+
def initialize(attributes = {})
|
13
|
+
super(attributes)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Attribute map for ActiveModel::Serialization.
|
17
|
+
def attributes
|
18
|
+
Hash[instance_variables.map{|attrib| [attrib.to_s[1..attrib.to_s.size], nil]}]
|
19
|
+
end
|
20
|
+
|
21
|
+
# Proxy to get a serialized version of the object.
|
22
|
+
def to_h
|
23
|
+
recursive_serialization(self)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Send request to Telegram API
|
27
|
+
def send_request
|
28
|
+
raise ArgumentError, "Method name not defined" if (self.method.blank?)
|
29
|
+
|
30
|
+
payload = self.to_h
|
31
|
+
payload.delete(:method)
|
32
|
+
|
33
|
+
payload.each do |key, value|
|
34
|
+
payload[key] = Faraday::UploadIO.new(payload[key], MIME::Types.type_for(payload[key].path).first.content_type) if (value.is_a?(File))
|
35
|
+
end
|
36
|
+
|
37
|
+
FantasticRobot.connection.api_call self.method, payload
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# Method to try to recursively seralize the objects received
|
43
|
+
def recursive_serialization object
|
44
|
+
if (object.is_a?(Array))
|
45
|
+
# If it's an array, try to serialize each element
|
46
|
+
return object.map{|o| recursive_serialization(o)}
|
47
|
+
elsif (object.is_a?(Hash))
|
48
|
+
# It's a hash, convert each key to sym and try to serialize each value
|
49
|
+
return Hash[object.map{|k,v| [k.to_sym, recursive_serialization(v)]}]
|
50
|
+
elsif (object.respond_to?(:serializable_hash))
|
51
|
+
# If it can be seralized, serialize and try to recursively convert it's attributes
|
52
|
+
return recursive_serialization(object.serializable_hash)
|
53
|
+
else
|
54
|
+
return object
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a forwardMessage request
|
3
|
+
class Request::ForwardMessage < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :chat_id, :from_chat_id, :message_id, :disable_web_page_preview, :reply_to_message_id, :reply_markup
|
6
|
+
|
7
|
+
validates :chat_id, :from_chat_id, :message_id, presence: true
|
8
|
+
validates :message_id, presence: true
|
9
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
10
|
+
validates :from_chat_id, numericality: true, unless: "from_chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
super(attributes)
|
15
|
+
@method = :forwardMessage
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendAudio request
|
3
|
+
class Request::SendAudio < Request::Base
|
4
|
+
|
5
|
+
MAX_FILE_SIZE = 50*1024*1024
|
6
|
+
|
7
|
+
attr_accessor :chat_id, :audio, :duration, :performer, :title, :reply_to_message_id, :reply_markup
|
8
|
+
|
9
|
+
validates :chat_id, :audio, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :duration, :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
validate :file_length
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
super(attributes)
|
17
|
+
@method = :sendAudio
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Function to check that the file size isn't excesive.
|
23
|
+
def file_length
|
24
|
+
if self.audio.is_a?(File) && self.audio.size > MAX_FILE_SIZE
|
25
|
+
self.errors.add :audio, "It's length is excesive. #{MAX_FILE_SIZE} is the limit."
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendChatAction request
|
3
|
+
class Request::SendChatAction < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :chat_id, :action
|
6
|
+
|
7
|
+
VALID_ACTIONS = [:typing, :upload_photo, :record_video, :upload_video, :record_audio, :upload_audio, :upload_document, :find_location]
|
8
|
+
|
9
|
+
validates :chat_id, :action, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :action, inclusion: { in: VALID_ACTIONS }
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
super(attributes)
|
15
|
+
@method = :sendChatAction
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendDocument request
|
3
|
+
class Request::SendDocument < Request::Base
|
4
|
+
|
5
|
+
MAX_FILE_SIZE = 50*1024*1024
|
6
|
+
|
7
|
+
attr_accessor :chat_id, :document, :reply_to_message_id, :reply_markup
|
8
|
+
|
9
|
+
validates :chat_id, :document, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
validate :file_length
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
super(attributes)
|
17
|
+
@method = :sendDocument
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Function to check that the file size isn't excesive.
|
23
|
+
def file_length
|
24
|
+
if self.document.is_a?(File) && self.document.size > MAX_FILE_SIZE
|
25
|
+
self.errors.add :document, "It's length is excesive. #{MAX_FILE_SIZE} is the limit."
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendLocation request
|
3
|
+
class Request::SendLocation < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :chat_id, :latitude, :longitude, :reply_to_message_id, :reply_markup
|
6
|
+
|
7
|
+
validates :chat_id, :latitude, :longitude, presence: true
|
8
|
+
validates :latitude, :longitude, numericality: true
|
9
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
10
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
11
|
+
|
12
|
+
def initialize(attributes = {})
|
13
|
+
super(attributes)
|
14
|
+
@method = :sendLocation
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendMessage request
|
3
|
+
class Request::SendMessage < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :chat_id, :text, :parse_mode, :disable_web_page_preview, :reply_to_message_id, :reply_markup
|
6
|
+
|
7
|
+
validates :chat_id, :text, presence: true
|
8
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
9
|
+
validates :disable_web_page_preview, inclusion: {in: [true, false]}, allow_blank: true
|
10
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
11
|
+
|
12
|
+
def initialize(attributes = {})
|
13
|
+
super(attributes)
|
14
|
+
@method = :sendMessage
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendPhoto request
|
3
|
+
class Request::SendPhoto < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :chat_id, :photo, :caption, :reply_to_message_id, :reply_markup
|
6
|
+
|
7
|
+
validates :chat_id, :photo, presence: true
|
8
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
9
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
10
|
+
|
11
|
+
def initialize(attributes = {})
|
12
|
+
super(attributes)
|
13
|
+
@method = :sendPhoto
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendSticker request
|
3
|
+
class Request::SendSticker < Request::Base
|
4
|
+
|
5
|
+
MAX_FILE_SIZE = 50*1024*1024
|
6
|
+
|
7
|
+
attr_accessor :chat_id, :sticker, :reply_to_message_id, :reply_markup
|
8
|
+
|
9
|
+
validates :chat_id, :sticker, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
def initialize(attributes = {})
|
14
|
+
super(attributes)
|
15
|
+
@method = :sendSticker
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendVideo request
|
3
|
+
class Request::SendVideo < Request::Base
|
4
|
+
|
5
|
+
MAX_FILE_SIZE = 50*1024*1024
|
6
|
+
|
7
|
+
attr_accessor :chat_id, :video, :duration, :caption, :reply_to_message_id, :reply_markup
|
8
|
+
|
9
|
+
validates :chat_id, :video, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :duration, :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
validate :file_length
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
super(attributes)
|
17
|
+
@method = :sendVideo
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Function to check that the file size isn't excesive.
|
23
|
+
def file_length
|
24
|
+
if self.video.is_a?(File) && self.video.size > MAX_FILE_SIZE
|
25
|
+
self.errors.add :video, "It's length is excesive. #{MAX_FILE_SIZE} is the limit."
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a sendVoice request
|
3
|
+
class Request::SendVoice < Request::Base
|
4
|
+
|
5
|
+
MAX_FILE_SIZE = 50*1024*1024
|
6
|
+
|
7
|
+
attr_accessor :chat_id, :voice, :duration, :reply_to_message_id, :reply_markup
|
8
|
+
|
9
|
+
validates :chat_id, :voice, presence: true
|
10
|
+
validates :chat_id, numericality: true, unless: "chat_id.to_s[0,1] == '@'"
|
11
|
+
validates :duration, :reply_to_message_id, numericality: true, allow_blank: true
|
12
|
+
|
13
|
+
validate :file_length
|
14
|
+
|
15
|
+
def initialize(attributes = {})
|
16
|
+
super(attributes)
|
17
|
+
@method = :sendVoice
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# Function to check that the file size isn't excesive.
|
23
|
+
def file_length
|
24
|
+
if self.voice.is_a?(File) && self.voice.size > MAX_FILE_SIZE
|
25
|
+
self.errors.add :voice, "It's length is excesive. #{MAX_FILE_SIZE} is the limit."
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
|
29
|
+
return true
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module FantasticRobot
|
2
|
+
# This object represents a setWebhook request
|
3
|
+
class Request::SetWebhook < Request::Base
|
4
|
+
|
5
|
+
attr_accessor :url
|
6
|
+
|
7
|
+
validates :url, presence: true
|
8
|
+
validate :https_url
|
9
|
+
|
10
|
+
def initialize(attributes = {})
|
11
|
+
super(attributes)
|
12
|
+
@method = :setWebhook
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
# Check that the URL is a HTTPS one
|
18
|
+
def https_url
|
19
|
+
return false if (url.blank?)
|
20
|
+
|
21
|
+
unless (url[0,8] == "https://")
|
22
|
+
errors.add :url, "Must be an HTPPS URL."
|
23
|
+
return false
|
24
|
+
end
|
25
|
+
|
26
|
+
return true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "fantastic_robot/request/base"
|
2
|
+
require "fantastic_robot/request/forward_message"
|
3
|
+
require "fantastic_robot/request/get_me"
|
4
|
+
require "fantastic_robot/request/send_audio"
|
5
|
+
require "fantastic_robot/request/send_chat_action"
|
6
|
+
require "fantastic_robot/request/send_document"
|
7
|
+
require "fantastic_robot/request/send_location"
|
8
|
+
require "fantastic_robot/request/send_message"
|
9
|
+
require "fantastic_robot/request/send_photo"
|
10
|
+
require "fantastic_robot/request/send_sticker"
|
11
|
+
require "fantastic_robot/request/send_video"
|
12
|
+
require "fantastic_robot/request/send_voice"
|
13
|
+
require "fantastic_robot/request/set_webhook"
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "active_model"
|
2
|
+
require "faraday"
|
3
|
+
require "fantastic_robot/models"
|
4
|
+
require "fantastic_robot/requests"
|
5
|
+
require "fantastic_robot/configuration"
|
6
|
+
require "fantastic_robot/connection"
|
7
|
+
require "fantastic_robot/version"
|
8
|
+
|
9
|
+
module FantasticRobot
|
10
|
+
class << self
|
11
|
+
attr_accessor :configuration
|
12
|
+
attr_reader :connection
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.initialize!
|
16
|
+
raise ArgumentError, "API Key is needed." if (self.configuration.api_key.blank?)
|
17
|
+
|
18
|
+
if (self.configuration.delivery_method == :webhook)
|
19
|
+
unless (self.configuration.callback_url.blank?)
|
20
|
+
# Register the webhook against Telegram
|
21
|
+
register_webhook
|
22
|
+
else
|
23
|
+
raise ArgumentError, 'Webhook method requires a callback URL'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.configure
|
29
|
+
self.configuration ||= Configuration.new
|
30
|
+
yield(configuration)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.configuration
|
34
|
+
@configuration ||= Configuration.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.connection
|
38
|
+
@connection ||= Connection.new
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Function to register the configured webhook against the Telegram API.
|
43
|
+
#
|
44
|
+
def self.register_webhook
|
45
|
+
FantasticRobot::Request::SetWebhook.new(url: self.configuration.callback_url).send_request
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
# Method to respond to an update with a request.
|
50
|
+
# It can manage a Request object or a hash.
|
51
|
+
#
|
52
|
+
def self.response_json(request)
|
53
|
+
return nil unless request.is_a?(Hash) || request.is_a?(FantasticRobot::Request::Base)
|
54
|
+
request.to_h
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
FantasticRobot.configure do |config|
|
2
|
+
# Secret token of the API
|
3
|
+
config.api_key = 'your_bot_api_token'
|
4
|
+
|
5
|
+
# Delivery method (can be :polling or :webhook)
|
6
|
+
config.delivery_method = :webhook
|
7
|
+
|
8
|
+
# Callback URL of the webhook
|
9
|
+
config.callback_url = 'http://your_app.dev/receive'
|
10
|
+
end
|
11
|
+
|
12
|
+
FantasticRobot.initialize! if(defined?(::Thin) || defined?(::Unicorn) || defined?(::Passenger) || defined?(::Puma))
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
|
3
|
+
module FantasticRobot
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("../files", __FILE__)
|
7
|
+
|
8
|
+
def copy_config_files
|
9
|
+
copy_file "fantastic_robot.rb", "config/initializers/fantastic_robot.rb"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|