Class: ChatSDK::Teams::Adapter

Inherits:
Adapter::Base
  • Object
show all
Defined in:
lib/chat_sdk/teams/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id: nil, app_password: nil, tenant_id: nil, graph_client_id: nil, graph_client_secret: nil, graph_tenant_id: nil) ⇒ Adapter

Returns a new instance of Adapter.

Raises:

  • (ChatSDK::ConfigurationError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/chat_sdk/teams/adapter.rb', line 11

def initialize(app_id: nil, app_password: nil, tenant_id: nil,
  graph_client_id: nil, graph_client_secret: nil, graph_tenant_id: nil)
  @app_id = app_id || ENV["TEAMS_APP_ID"]
  @app_password = app_password || ENV["TEAMS_APP_PASSWORD"]
  @tenant_id = tenant_id || ENV["TEAMS_TENANT_ID"]

  raise ChatSDK::ConfigurationError, "Teams app_id required" unless @app_id
  raise ChatSDK::ConfigurationError, "Teams app_password required" unless @app_password

  @graph_client_id = graph_client_id || ENV["TEAMS_GRAPH_CLIENT_ID"]
  @graph_client_secret = graph_client_secret || ENV["TEAMS_GRAPH_CLIENT_SECRET"]
  @graph_tenant_id = graph_tenant_id || ENV["TEAMS_GRAPH_TENANT_ID"]
  @graph_client = GraphClient.new(@graph_client_id, @graph_client_secret, @graph_tenant_id) if @graph_client_id

  @client = BotFrameworkClient.new(app_id: @app_id, app_password: @app_password)
  @jwt_verifier = JwtVerifier.new(app_id: @app_id)
  @renderer = AdaptiveCardRenderer.new
  @service_urls = {}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/chat_sdk/teams/adapter.rb', line 9

def client
  @client
end

Instance Method Details

#delete_message(channel_id:, message_id:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/chat_sdk/teams/adapter.rb', line 99

def delete_message(channel_id:, message_id:)
  require_capability!(:delete_messages)
  service_url = service_url_for(channel_id)

  @client.delete_activity(
    service_url: service_url,
    conversation_id: channel_id,
    activity_id: message_id
  )
end

#edit_message(channel_id:, message_id:, message:) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chat_sdk/teams/adapter.rb', line 83

def edit_message(channel_id:, message_id:, message:)
  require_capability!(:edit_messages)
  msg = ChatSDK::PostableMessage.from(message)
  service_url = service_url_for(channel_id)

  activity = build_activity(msg)
  activity["id"] = message_id

  @client.update_activity(
    service_url: service_url,
    conversation_id: channel_id,
    activity_id: message_id,
    activity: activity
  )
end

#fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/chat_sdk/teams/adapter.rb', line 186

def fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50)
  require_capability!(:message_history)
  unless @graph_client
    raise ChatSDK::ConfigurationError,
      "Teams message history requires Graph API credentials " \
      "(TEAMS_GRAPH_CLIENT_ID, TEAMS_GRAPH_CLIENT_SECRET, TEAMS_GRAPH_TENANT_ID)"
  end

  result = @graph_client.fetch_messages(chat_id: channel_id, limit: limit)
  messages = (result["value"] || []).map { |m| parse_graph_message(m, channel_id) }
  next_link = result["@odata.nextLink"]
  [messages, next_link]
end

#mention(user_id) ⇒ Object



173
174
175
# File 'lib/chat_sdk/teams/adapter.rb', line 173

def mention(user_id)
  "<at>#{user_id}</at>"
end

#nameObject



31
32
33
# File 'lib/chat_sdk/teams/adapter.rb', line 31

def name
  :teams
end

#open_dm(user_id) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/chat_sdk/teams/adapter.rb', line 138

def open_dm(user_id)
  require_capability!(:direct_messages)
  # To open a DM, we need a service URL. Use the first cached one.
  service_url = @service_urls.values.first
  unless service_url
    raise ChatSDK::PlatformError.new(
      "No service URL available. A Teams activity must be received first.",
      adapter_name: :teams
    )
  end

  payload = {
    "bot" => {"id" => @app_id},
    "members" => [{"id" => user_id}],
    "isGroup" => false
  }

  result = @client.create_conversation(service_url: service_url, payload: payload)
  conversation_id = result["id"]
  @service_urls[conversation_id] = service_url
  conversation_id
end

#open_modal(trigger_id:, modal:) ⇒ Object



161
162
163
# File 'lib/chat_sdk/teams/adapter.rb', line 161

def open_modal(trigger_id:, modal:)
  super
end

#parse_events(rack_request) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/chat_sdk/teams/adapter.rb', line 45

def parse_events(rack_request)
  activity = read_json_body(rack_request)

  # Cache the service URL for this conversation
  if activity["serviceUrl"] && activity.dig("conversation", "id")
    @service_urls[activity.dig("conversation", "id")] = activity["serviceUrl"]
  end

  ActivityParser.parse(activity, bot_app_id: @app_id)
rescue JSON::ParserError
  []
end

#post_ephemeral(channel_id:, user_id:, message:, thread_id: nil) ⇒ Object



110
111
112
# File 'lib/chat_sdk/teams/adapter.rb', line 110

def post_ephemeral(channel_id:, user_id:, message:, thread_id: nil)
  super # raises NotSupportedError
end

#post_message(channel_id:, message:, thread_id: nil) ⇒ Object

Outbound



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chat_sdk/teams/adapter.rb', line 59

def post_message(channel_id:, message:, thread_id: nil)
  msg = ChatSDK::PostableMessage.from(message)
  service_url = service_url_for(channel_id)

  activity = build_activity(msg)
  activity["replyToId"] = thread_id if thread_id

  result = @client.send_activity(
    service_url: service_url,
    conversation_id: channel_id,
    activity: activity
  )

  ChatSDK::Message.new(
    id: result["id"],
    text: msg.text || "",
    author: ChatSDK::Author.new(id: @app_id, name: "bot", platform: :teams, bot: true),
    thread_id: thread_id || result["id"],
    channel_id: channel_id,
    platform: :teams,
    raw: result
  )
end

#register_service_url(conversation_id, service_url) ⇒ Object

Store a service URL for a conversation (useful when sending proactive messages)



201
202
203
# File 'lib/chat_sdk/teams/adapter.rb', line 201

def register_service_url(conversation_id, service_url)
  @service_urls[conversation_id] = service_url
end

#render(postable_message) ⇒ Object



177
178
179
180
181
182
183
184
# File 'lib/chat_sdk/teams/adapter.rb', line 177

def render(postable_message)
  msg = ChatSDK::PostableMessage.from(postable_message)
  if msg.card?
    @renderer.render(msg.card)
  else
    msg.text
  end
end

#start_typing(channel_id:, thread_id: nil) ⇒ Object



165
166
167
168
169
170
171
# File 'lib/chat_sdk/teams/adapter.rb', line 165

def start_typing(channel_id:, thread_id: nil)
  service_url = service_url_for(channel_id)
  @client.send_typing(
    service_url: service_url,
    conversation_id: channel_id
  )
end

#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chat_sdk/teams/adapter.rb', line 114

def upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil)
  require_capability!(:file_uploads)
  # Teams file uploads are done via attachments in activities
  service_url = service_url_for(channel_id)
  content = Base64.strict_encode64(io.read)

  activity = {
    "type" => "message",
    "text" => comment || "",
    "attachments" => [{
      "contentType" => "application/octet-stream",
      "name" => filename,
      "contentUrl" => "data:application/octet-stream;base64,#{content}"
    }]
  }
  activity["replyToId"] = thread_id if thread_id

  @client.send_activity(
    service_url: service_url,
    conversation_id: channel_id,
    activity: activity
  )
end

#verify_request!(rack_request) ⇒ Object

Inbound

Raises:

  • (ChatSDK::SignatureVerificationError)


36
37
38
39
40
41
42
43
# File 'lib/chat_sdk/teams/adapter.rb', line 36

def verify_request!(rack_request)
  auth_header = rack_request.env["HTTP_AUTHORIZATION"]
  raise ChatSDK::SignatureVerificationError, "Missing authorization header" unless auth_header

  token = auth_header.sub(/^Bearer\s+/i, "")
  @jwt_verifier.verify!(token)
  true
end