bot_framework 0.1.0beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +60 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/bot_framework.gemspec +31 -0
  13. data/examples/echo/bot.rb +8 -0
  14. data/examples/echo/config.ru +5 -0
  15. data/examples/stock/bot.rb +38 -0
  16. data/examples/stock/config.ru +5 -0
  17. data/lib/bot_framework.rb +48 -0
  18. data/lib/bot_framework/api_base.rb +31 -0
  19. data/lib/bot_framework/bot.rb +48 -0
  20. data/lib/bot_framework/bot_state.rb +89 -0
  21. data/lib/bot_framework/connector.rb +34 -0
  22. data/lib/bot_framework/conversation.rb +33 -0
  23. data/lib/bot_framework/errors.rb +4 -0
  24. data/lib/bot_framework/models/activity.rb +106 -0
  25. data/lib/bot_framework/models/api_response.rb +23 -0
  26. data/lib/bot_framework/models/attachment.rb +52 -0
  27. data/lib/bot_framework/models/attachment_data.rb +26 -0
  28. data/lib/bot_framework/models/attachment_info.rb +41 -0
  29. data/lib/bot_framework/models/attachment_view.rb +31 -0
  30. data/lib/bot_framework/models/base.rb +187 -0
  31. data/lib/bot_framework/models/bot_data.rb +15 -0
  32. data/lib/bot_framework/models/card_action.rb +43 -0
  33. data/lib/bot_framework/models/card_image.rb +36 -0
  34. data/lib/bot_framework/models/channel_account.rb +18 -0
  35. data/lib/bot_framework/models/conversation_account.rb +22 -0
  36. data/lib/bot_framework/models/conversation_parameters.rb +46 -0
  37. data/lib/bot_framework/models/entity.rb +26 -0
  38. data/lib/bot_framework/models/fact.rb +29 -0
  39. data/lib/bot_framework/models/geo_coordinates.rb +49 -0
  40. data/lib/bot_framework/models/hero_card.rb +62 -0
  41. data/lib/bot_framework/models/object.rb +19 -0
  42. data/lib/bot_framework/models/place.rb +56 -0
  43. data/lib/bot_framework/models/receipt_card.rb +78 -0
  44. data/lib/bot_framework/models/receipt_item.rb +60 -0
  45. data/lib/bot_framework/models/resource_response.rb +13 -0
  46. data/lib/bot_framework/models/signin_card.rb +35 -0
  47. data/lib/bot_framework/models/thumbnail_card.rb +62 -0
  48. data/lib/bot_framework/server.rb +56 -0
  49. data/lib/bot_framework/token_validator.rb +87 -0
  50. data/lib/bot_framework/util.rb +19 -0
  51. data/lib/bot_framework/version.rb +3 -0
  52. metadata +206 -0
@@ -0,0 +1,34 @@
1
+ module BotFramework
2
+ # Connector class
3
+ class Connector
4
+ # include HTTParty
5
+ attr_accessor :app_id, :app_secret, :token
6
+ CONFIG_URI = 'https://api.aps.skype.com/v1/.well-known/openidconfiguration'.freeze
7
+
8
+ def initialize(options = {})
9
+ @app_id = options[:app_id]
10
+ @app_secret = options[:app_secret]
11
+ yield(self) if block_given?
12
+ end
13
+
14
+ def client
15
+ OAuth2::Client.new(app_id, app_secret,
16
+ authorize_url: '/common/oauth2/v2.0/authorize',
17
+ token_url: '/common/oauth2/v2.0/token',
18
+ raise_errors: true,
19
+ site: 'https://login.microsoftonline.com')
20
+ end
21
+
22
+ def token
23
+ @token = nil if @token && @token.expired?
24
+ @token ||= get_token
25
+ @token
26
+ end
27
+
28
+ def get_token
29
+ client.client_credentials.get_token(scope: 'https://graph.microsoft.com/.default',
30
+ client_id: app_id,
31
+ client_secret: app_secret)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,33 @@
1
+ module BotFramework
2
+ class Conversation < ApiBase
3
+ def create(attributes)
4
+ uri = '/v3/conversations'
5
+ api_post(uri, attributes)
6
+ end
7
+
8
+ def get_activity_members(conversation_id, activity_id, opts = {})
9
+ uri = "/v3/conversations/#{conversation_id}/activities/#{activity_id}/members"
10
+ api_get(uri, opts)
11
+ end
12
+
13
+ def get_conversation_members(conversation_id)
14
+ uri = "/v3/conversations/#{conversation_id}/activities/members"
15
+ api_get(uri, opts)
16
+ end
17
+
18
+ def send(conversation_id, activity)
19
+ uri = "/v3/conversations/#{conversation_id}/activities"
20
+ api_post(uri, activity.as_json)
21
+ end
22
+
23
+ def upload_attachment(conversation_id, opts = {})
24
+ uri = "/v3/conversations/#{conversation_id}/attachments"
25
+ api_post(uri, opts)
26
+ end
27
+
28
+ def reply_to_activity(conversation_id, activity_id, new_activity)
29
+ uri = "/v3/conversations/#{conversation_id}/activities/#{activity_id}"
30
+ api_post(uri, new_activity.to_hash)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ module BotFramework
2
+ class InvalidToken < StandardError
3
+ end
4
+ end
@@ -0,0 +1,106 @@
1
+ module BotFramework
2
+ class Activity < Base
3
+ # The type of the activity [message|contactRelationUpdate|converationUpdate|typing]
4
+ attr_accessor :type
5
+
6
+ # Id for the activity
7
+ attr_accessor :id
8
+
9
+ # Time when message was sent
10
+ attr_accessor :timestamp
11
+
12
+ # Service endpoint
13
+ attr_accessor :service_url
14
+
15
+ # ChannelId the activity was on
16
+ attr_accessor :channel_id
17
+
18
+ # Sender address
19
+ attr_accessor :from
20
+
21
+ # Conversation
22
+ attr_accessor :conversation
23
+
24
+ # (Outbound to bot only) Bot's address that received the message
25
+ attr_accessor :recipient
26
+
27
+ # Format of text fields [plain|markdown] Default:markdown
28
+ attr_accessor :text_format
29
+
30
+ # AttachmentLayout - hint for how to deal with multiple attachments Values: [list|carousel] Default:list
31
+ attr_accessor :attachment_layout
32
+
33
+ # Array of address added
34
+ attr_accessor :members_added
35
+
36
+ # Array of addresses removed
37
+ attr_accessor :members_removed
38
+
39
+ # Conversations new topic name
40
+ attr_accessor :topic_name
41
+
42
+ # the previous history of the channel was disclosed
43
+ attr_accessor :history_disclosed
44
+
45
+ # The language code of the Text field
46
+ attr_accessor :locale
47
+
48
+ # Content for the message
49
+ attr_accessor :text
50
+
51
+ # Text to display if you can't render cards
52
+ attr_accessor :summary
53
+
54
+ # Attachments
55
+ attr_accessor :attachments
56
+
57
+ # Collection of Entity objects, each of which contains metadata about this activity. Each Entity object is typed.
58
+ attr_accessor :entities
59
+
60
+ # Channel specific payload
61
+ attr_accessor :channel_data
62
+
63
+ # ContactAdded/Removed action
64
+ attr_accessor :action
65
+
66
+ # the original id this message is a response to
67
+ attr_accessor :reply_to_id
68
+
69
+ # Attribute type mapping.
70
+ def self.swagger_types
71
+ {
72
+ type: :String,
73
+ id: :String,
74
+ timestamp: :DateTime,
75
+ service_url: :String,
76
+ channel_id: :String,
77
+ from: :ChannelAccount,
78
+ conversation: :ConversationAccount,
79
+ recipient: :ChannelAccount,
80
+ text_format: :String,
81
+ attachment_layout: :String,
82
+ members_added: :'Array<ChannelAccount>',
83
+ members_removed: :'Array<ChannelAccount>',
84
+ topic_name: :String,
85
+ history_disclosed: :BOOLEAN,
86
+ locale: :String,
87
+ text: :String,
88
+ summary: :String,
89
+ attachments: :'Array<Attachment>',
90
+ entities: :'Array<Entity>',
91
+ channel_data: :Object,
92
+ action: :String,
93
+ reply_to_id: :String
94
+ }
95
+ end
96
+
97
+ def reply(message)
98
+ return false if type != 'message'
99
+ new_activity = Activity.new(type: 'message',
100
+ locale: 'en',
101
+ text: message,
102
+ from: recipient.to_hash)
103
+ Conversation.new(service_url).reply_to_activity(conversation.id, id, new_activity)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,23 @@
1
+ module BotFramework
2
+ class APIResponse < Base
3
+ attr_accessor :message
4
+
5
+ # Attribute type mapping.
6
+ def self.swagger_types
7
+ {
8
+ message: :String
9
+ }
10
+ end
11
+
12
+ # Initializes the object
13
+ # @param [Hash] attributes Model attributes in the form of hash
14
+ def initialize(attributes = {})
15
+ return unless attributes.is_a?(Hash)
16
+
17
+ # convert string to symbol for hash key
18
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
19
+
20
+ self.message = attributes[:message] if attributes.key?(:message)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ module BotFramework
2
+ class Attachment < Base
3
+ # mimetype/Contenttype for the file
4
+ attr_accessor :content_type
5
+
6
+ # Content Url
7
+ attr_accessor :content_url
8
+
9
+ # Embedded content
10
+ attr_accessor :content
11
+
12
+ # (OPTIONAL) The name of the attachment
13
+ attr_accessor :name
14
+
15
+ # (OPTIONAL) Thumbnail associated with attachment
16
+ attr_accessor :thumbnail_url
17
+
18
+ # Attribute type mapping.
19
+ def self.swagger_types
20
+ {
21
+ content_type: :String,
22
+ content_url: :String,
23
+ content: :Object,
24
+ name: :String,
25
+ thumbnail_url: :String
26
+ }
27
+ end
28
+
29
+ # Initializes the object
30
+ # @param [Hash] attributes Model attributes in the form of hash
31
+ def initialize(attributes = {})
32
+ return unless attributes.is_a?(Hash)
33
+
34
+ # convert string to symbol for hash key
35
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
36
+
37
+ if attributes.key?(:contentType)
38
+ self.content_type = attributes[:contentType]
39
+ end
40
+
41
+ self.content_url = attributes[:contentUrl] if attributes.key?(:contentUrl)
42
+
43
+ self.content = attributes[:content] if attributes.key?(:content)
44
+
45
+ self.name = attributes[:name] if attributes.key?(:name)
46
+
47
+ if attributes.key?(:thumbnailUrl)
48
+ self.thumbnail_url = attributes[:thumbnailUrl]
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,26 @@
1
+ module BotFramework
2
+ # Attachment data
3
+ class AttachmentData < Base
4
+ # content type of the attachmnet
5
+ attr_accessor :type
6
+
7
+ # Name of the attachment
8
+ attr_accessor :name
9
+
10
+ # original content
11
+ attr_accessor :original_base64
12
+
13
+ # Thumbnail
14
+ attr_accessor :thumbnail_base64
15
+
16
+ # Attribute type mapping.
17
+ def self.swagger_types
18
+ {
19
+ type: :String,
20
+ name: :String,
21
+ original_base64: :String,
22
+ thumbnail_base64: :String
23
+ }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ module BotFramework
2
+ # Metdata for an attachment
3
+ class AttachmentInfo < Base
4
+ # Name of the attachment
5
+ attr_accessor :name
6
+
7
+ # ContentType of the attachment
8
+ attr_accessor :type
9
+
10
+ # attachment views
11
+ attr_accessor :views
12
+
13
+ # Attribute type mapping.
14
+ def self.swagger_types
15
+ {
16
+ name: :String,
17
+ type: :String,
18
+ views: :'Array<AttachmentView>'
19
+ }
20
+ end
21
+
22
+ # Initializes the object
23
+ # @param [Hash] attributes Model attributes in the form of hash
24
+ def initialize(attributes = {})
25
+ return unless attributes.is_a?(Hash)
26
+
27
+ # convert string to symbol for hash key
28
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
29
+
30
+ self.name = attributes[:name] if attributes.key?(:name)
31
+
32
+ self.type = attributes[:type] if attributes.key?(:type)
33
+
34
+ if attributes.key?(:views)
35
+ if (value = attributes[:views]).is_a?(Array)
36
+ self.views = value
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,31 @@
1
+ module BotFramework
2
+ # Attachment View name and size
3
+ class AttachmentView < Base
4
+ # content type of the attachmnet
5
+ attr_accessor :view_id
6
+
7
+ # Name of the attachment
8
+ attr_accessor :size
9
+
10
+ # Attribute type mapping.
11
+ def self.swagger_types
12
+ {
13
+ view_id: :String,
14
+ size: :Integer
15
+ }
16
+ end
17
+
18
+ # Initializes the object
19
+ # @param [Hash] attributes Model attributes in the form of hash
20
+ def initialize(attributes = {})
21
+ return unless attributes.is_a?(Hash)
22
+
23
+ # convert string to symbol for hash key
24
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
25
+
26
+ self.view_id = attributes[:viewId] if attributes.key?(:viewId)
27
+
28
+ self.size = attributes[:size] if attributes.key?(:size)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,187 @@
1
+ module BotFramework
2
+ class Base
3
+ class <<self
4
+ attr_accessor :attributes
5
+ end
6
+
7
+ def self.attr_accessor(*args)
8
+ @attributes ||= []
9
+ @attributes.concat args
10
+ super(*args)
11
+ end
12
+
13
+ def attributes
14
+ self.class.attributes
15
+ end
16
+
17
+ def attributes_hash
18
+ attributes.map { |attribute| { attribute => send(attribute) } }.reduce({}, :merge)
19
+ end
20
+
21
+ def compact_attributes_hash
22
+ to_hash
23
+ end
24
+
25
+ def as_json
26
+ to_hash
27
+ end
28
+
29
+ def to_json
30
+ as_json.to_json
31
+ end
32
+
33
+ def initialize(options = {})
34
+ return unless options.is_a?(Hash)
35
+ options.each do |key, value|
36
+ instance_variable_set("@#{key}", value)
37
+ end
38
+ end
39
+
40
+ # Attribute mapping from ruby-style variable name to JSON key.
41
+ def self.attribute_map
42
+ # FIXME: using class level caching breaks inheritance
43
+ @attribute_map ||= attributes.map do |attribute|
44
+ { attribute => BotFramework::Util.camel_case_lower(attribute).to_sym }
45
+ end.reduce({}, :merge)
46
+ end
47
+
48
+ # Show invalid properties with the reasons. Usually used together with valid?
49
+ # @return Array for valid properies with the reasons
50
+ def list_invalid_properties
51
+ invalid_properties = []
52
+ invalid_properties
53
+ end
54
+
55
+ # Check to see if the all the properties in the model are valid
56
+ # @return true if the model is valid
57
+ def valid?
58
+ true
59
+ end
60
+
61
+ # Checks equality by comparing each attribute.
62
+ # @param [Object] Object to be compared
63
+ def ==(o)
64
+ return true if equal?(o)
65
+ self.class = o.class && attributes_hash == o.attributes_hash
66
+ end
67
+
68
+ # @see the `==` method
69
+ # @param [Object] Object to be compared
70
+ def eql?(o)
71
+ self == o
72
+ end
73
+
74
+ # Calculates hash code according to all attributes.
75
+ # @return [Fixnum] Hash code
76
+ def hash
77
+ attributes_hash
78
+ end
79
+
80
+ # Builds the object from hash
81
+ # @param [Hash] attributes Model attributes in the form of hash
82
+ # @return [Object] Returns the model itself
83
+ def build_from_hash(attributes)
84
+ # convert string to symbol for hash key
85
+ attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
86
+
87
+ return nil unless attributes.is_a?(Hash)
88
+ self.class.swagger_types.each_pair do |key, type|
89
+ if type =~ /^Array<(.*)>/i
90
+ # check to ensure the input is an array given that the the attribute
91
+ # is documented as an array but the input is not
92
+ if attributes[self.class.attribute_map[key]].is_a?(Array)
93
+ send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize(Regexp.last_match(1), v) })
94
+ end
95
+ elsif !attributes[self.class.attribute_map[key]].nil?
96
+ send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
97
+ end # or else data not found in attributes(hash), not an issue as the data can be optional
98
+ end
99
+
100
+ self
101
+ end
102
+
103
+ # Deserializes the data based on type
104
+ # @param string type Data type
105
+ # @param string value Value to be deserialized
106
+ # @return [Object] Deserialized data
107
+ def _deserialize(type, value)
108
+ case type.to_sym
109
+ when :DateTime
110
+ DateTime.parse(value)
111
+ when :Date
112
+ Date.parse(value)
113
+ when :String
114
+ value.to_s
115
+ when :Integer
116
+ value.to_i
117
+ when :Float
118
+ value.to_f
119
+ when :BOOLEAN
120
+ if value.to_s =~ /^(true|t|yes|y|1)$/i
121
+ true
122
+ else
123
+ false
124
+ end
125
+ when :Object
126
+ # generic object (usually a Hash), return directly
127
+ value
128
+ when /\AArray<(?<inner_type>.+)>\z/
129
+ inner_type = Regexp.last_match[:inner_type]
130
+ value.map { |v| _deserialize(inner_type, v) }
131
+ when /\AHash<(?<k_type>.+), (?<v_type>.+)>\z/
132
+ k_type = Regexp.last_match[:k_type]
133
+ v_type = Regexp.last_match[:v_type]
134
+ {}.tap do |hash|
135
+ value.each do |k, v|
136
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
137
+ end
138
+ end
139
+ else # model
140
+ temp_model = BotFramework.const_get(type).new
141
+ temp_model.build_from_hash(value)
142
+ end
143
+ end
144
+
145
+ # Returns the string representation of the object
146
+ # @return [String] String presentation of the object
147
+ def to_s
148
+ to_hash.to_s
149
+ end
150
+
151
+ # to_body is an alias to to_hash (backward compatibility)
152
+ # @return [Hash] Returns the object in the form of hash
153
+ def to_body
154
+ to_hash
155
+ end
156
+
157
+ # Returns the object in the form of hash
158
+ # @return [Hash] Returns the object in the form of hash
159
+ def to_hash
160
+ hash = {}
161
+ self.class.attribute_map.each_pair do |attr, param|
162
+ value = send(attr)
163
+ next if value.nil?
164
+ hash[param] = _to_hash(value)
165
+ end
166
+ hash
167
+ end
168
+
169
+ # Outputs non-array value in the form of hash
170
+ # For object, use to_hash. Otherwise, just return the value
171
+ # @param [Object] value Any valid value
172
+ # @return [Hash] Returns the value in the form of hash
173
+ def _to_hash(value)
174
+ if value.is_a?(Array)
175
+ value.compact.map { |v| _to_hash(v) }
176
+ elsif value.is_a?(Hash)
177
+ {}.tap do |hash|
178
+ value.each { |k, v| hash[k] = _to_hash(v) }
179
+ end
180
+ elsif value.respond_to? :to_hash
181
+ value.to_hash
182
+ else
183
+ value
184
+ end
185
+ end
186
+ end
187
+ end