Class: Legion::Gaia::Channels::TeamsAdapter
Constant Summary
collapse
- CAPABILITIES =
%i[rich_text adaptive_cards proactive_messaging mobile desktop mentions].freeze
- MOBILE_CAPABILITIES =
%i[rich_text adaptive_cards mobile mentions].freeze
- DESKTOP_CAPABILITIES =
%i[rich_text adaptive_cards desktop mentions file_attachment].freeze
Legion::Gaia::ChannelAdapter::DIRECT_ADDRESS_PATTERN
Instance Attribute Summary collapse
#capabilities, #channel_id
Class Method Summary
collapse
Instance Method Summary
collapse
inherited, register_adapter, #start, #started?, #stop, #supports?
Constructor Details
#initialize(app_id: nil) ⇒ TeamsAdapter
Returns a new instance of TeamsAdapter.
23
24
25
26
27
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 23
def initialize(app_id: nil)
super(channel_id: :teams, capabilities: CAPABILITIES)
@app_id = app_id
@conversation_store = Teams::ConversationStore.new
end
|
Instance Attribute Details
#app_id ⇒ Object
Returns the value of attribute app_id.
15
16
17
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 15
def app_id
@app_id
end
|
#conversation_store ⇒ Object
Returns the value of attribute conversation_store.
15
16
17
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 15
def conversation_store
@conversation_store
end
|
Class Method Details
.from_settings(settings) ⇒ Object
17
18
19
20
21
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 17
def self.from_settings(settings)
return nil unless settings&.dig(:channels, :teams, :enabled)
new(app_id: settings.dig(:channels, :teams, :app_id))
end
|
Instance Method Details
#create_proactive_conversation(user_id:, tenant_id: nil) ⇒ Object
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
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 84
def create_proactive_conversation(user_id:, tenant_id: nil)
profile = conversation_store.lookup_user_profile(user_id)
service_url = profile&.service_url
resolved_tenant = tenant_id || profile&.tenant_id
return { error: :no_service_url } unless service_url
return { error: :bot_runner_not_available } unless bot_runner_available?
bot = Legion::Extensions::MicrosoftTeams::Client.new
result = bot.create_conversation(
service_url: service_url,
bot_id: app_id,
user_id: user_id,
tenant_id: resolved_tenant
)
return result if result.is_a?(Hash) && result[:error]
conversation_id = result[:conversation_id] || result['id']
conversation_store.store(
conversation_id: conversation_id,
service_url: service_url,
tenant_id: resolved_tenant
)
conversation_id
rescue StandardError => e
if defined?(Legion::Logging)
Legion::Logging.warn("TeamsAdapter create_proactive_conversation failed: #{e.message}")
end
{ error: :create_conversation_failed, message: e.message }
end
|
#deliver(rendered_content, conversation_id: nil) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 66
def deliver(rendered_content, conversation_id: nil)
ref = conversation_id && conversation_store.lookup(conversation_id)
return { error: :no_conversation_reference } unless ref
deliver_via_bot(rendered_content, ref)
end
|
#deliver_proactive(output_frame) ⇒ Object
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 73
def deliver_proactive(output_frame)
user_id = output_frame.metadata[:target_user]
return { error: :no_target_user } unless user_id
conversation_id = resolve_proactive_conversation(user_id)
return conversation_id if conversation_id.is_a?(Hash) && conversation_id[:error]
rendered = translate_outbound(output_frame)
deliver(rendered, conversation_id: conversation_id)
end
|
#translate_inbound(activity) ⇒ Object
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
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 29
def translate_inbound(activity)
return nil unless activity.is_a?(Hash)
identity = Teams::BotFrameworkAuth.(activity)
conversation = activity['conversation'] || activity[:conversation] || {}
text = activity['text'] || activity[:text] || ''
text = strip_mention(text, activity)
conversation_store.store_from_activity(activity)
InputFrame.new(
content: text.strip,
channel_id: :teams,
content_type: detect_content_type(activity),
channel_capabilities: capabilities_for_device(activity),
device_context: build_device_context(activity),
auth_context: build_auth_context(identity, activity),
metadata: {
source_type: :human_direct,
salience: 0.9,
direct_address: direct_address?(text.strip),
conversation_id: conversation['id'] || conversation[:id],
activity_id: activity['id'] || activity[:id],
activity_type: activity['type'] || activity[:type]
}
)
end
|
#translate_outbound(output_frame) ⇒ Object
57
58
59
60
61
62
63
64
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 57
def translate_outbound(output_frame)
content = output_frame.content.to_s
if output_frame.content_type == :adaptive_card
{ type: 'adaptive_card', card: output_frame.content }
else
{ type: 'text', text: content }
end
end
|
#validate_inbound(token, allow_emulator: false) ⇒ Object
114
115
116
117
118
|
# File 'lib/legion/gaia/channels/teams_adapter.rb', line 114
def validate_inbound(token, allow_emulator: false)
return { valid: false, error: :no_app_id } unless app_id
Teams::BotFrameworkAuth.validate_token(token, app_id: app_id, allow_emulator: allow_emulator)
end
|