Class: Teams::Api::ConversationClient

Inherits:
Object
  • Object
show all
Defined in:
lib/teams/api/conversation_client.rb

Constant Summary collapse

TARGETED_PARAMS =
{ "isTargetedActivity" => "true" }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service_url:, http:, logger: nil) ⇒ ConversationClient

Returns a new instance of ConversationClient.



12
13
14
15
16
17
# File 'lib/teams/api/conversation_client.rb', line 12

def initialize(service_url:, http:, logger: nil)
  @service_url = service_url.sub(%r{/+\z}, "")
  @http = http
  @logger = logger
  @reactions = ReactionClient.new(service_url: @service_url, http:)
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



10
11
12
# File 'lib/teams/api/conversation_client.rb', line 10

def http
  @http
end

#service_urlObject (readonly)

Returns the value of attribute service_url.



10
11
12
# File 'lib/teams/api/conversation_client.rb', line 10

def service_url
  @service_url
end

Instance Method Details

#add_reaction(conversation_id, activity_id, reaction_type) ⇒ Object



120
121
122
# File 'lib/teams/api/conversation_client.rb', line 120

def add_reaction(conversation_id, activity_id, reaction_type)
  reactions.add(conversation_id, activity_id, reaction_type)
end

#create(members: nil, tenant_id: nil, activity: nil, channel_data: nil, service_url: nil) ⇒ Object

Creates a conversation (or returns the pre-existing one for the same members). isGroup/bot/topicName are omitted: the SDKs deprecate them for removal, and Python never had them.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/teams/api/conversation_client.rb', line 22

def create(members: nil, tenant_id: nil, activity: nil, channel_data: nil, service_url: nil)
  body = {}
  body["members"] = members.map { |member| (member) } if members
  body["tenantId"] = tenant_id if tenant_id
  body["activity"] = activity_to_h(activity) if activity
  body["channelData"] = Common::Hashes.deep_stringify_keys(channel_data) if channel_data

  url = absolute("/v3/conversations", service_url:)
  @logger&.debug("Teams API POST #{url}")
  ConversationResource.new(http.post(url, json: body))
end

#create_activity(conversation_id, activity, service_url: nil) ⇒ Object



34
35
36
37
38
39
# File 'lib/teams/api/conversation_client.rb', line 34

def create_activity(conversation_id, activity, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API POST #{url}")
  http.post(url, json: activity_to_h(activity))
end

#create_targeted_activity(conversation_id, activity, service_url: nil) ⇒ Object



64
65
66
67
68
69
# File 'lib/teams/api/conversation_client.rb', line 64

def create_targeted_activity(conversation_id, activity, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API POST #{url} (targeted)")
  http.post(url, json: activity_to_h(activity), params: TARGETED_PARAMS)
end

#delete_activity(conversation_id, activity_id, service_url: nil) ⇒ Object



57
58
59
60
61
62
# File 'lib/teams/api/conversation_client.rb', line 57

def delete_activity(conversation_id, activity_id, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API DELETE #{url}")
  http.delete(url)
end

#delete_reaction(conversation_id, activity_id, reaction_type) ⇒ Object



124
125
126
# File 'lib/teams/api/conversation_client.rb', line 124

def delete_reaction(conversation_id, activity_id, reaction_type)
  reactions.delete(conversation_id, activity_id, reaction_type)
end

#delete_targeted_activity(conversation_id, activity_id, service_url: nil) ⇒ Object



78
79
80
81
82
83
# File 'lib/teams/api/conversation_client.rb', line 78

def delete_targeted_activity(conversation_id, activity_id, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API DELETE #{url} (targeted)")
  http.delete(url, params: TARGETED_PARAMS)
end

#get_activity_members(conversation_id, activity_id, service_url: nil) ⇒ Object



113
114
115
116
117
118
# File 'lib/teams/api/conversation_client.rb', line 113

def get_activity_members(conversation_id, activity_id, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}/members"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API GET #{url}")
  Array(http.get(url)).map { |member| Account.new(member) }
end

#get_member_by_id(conversation_id, member_id, service_url: nil) ⇒ Object



95
96
97
98
99
100
# File 'lib/teams/api/conversation_client.rb', line 95

def get_member_by_id(conversation_id, member_id, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/members/#{escape(member_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API GET #{url}")
  Account.new(http.get(url))
end

#get_members(conversation_id, service_url: nil) ⇒ Object

The backend returns objectId instead of aadObjectId on some member endpoints; Api::Account reads both, so the accounts here are already normalized like the other SDKs' TeamsChannelAccount.



88
89
90
91
92
93
# File 'lib/teams/api/conversation_client.rb', line 88

def get_members(conversation_id, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/members"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API GET #{url}")
  Array(http.get(url)).map { |member| Account.new(member) }
end

#get_paged_members(conversation_id, page_size: nil, continuation_token: nil, service_url: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/teams/api/conversation_client.rb', line 102

def get_paged_members(conversation_id, page_size: nil, continuation_token: nil, service_url: nil)
  params = {}
  params["pageSize"] = page_size if page_size
  params["continuationToken"] = continuation_token if continuation_token

  path = "/v3/conversations/#{escape(conversation_id)}/pagedMembers"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API GET #{url}")
  PagedMembersResult.new(http.get(url, params: params.empty? ? nil : params))
end

#reply_to_activity(conversation_id, activity_id, activity, service_url: nil) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/teams/api/conversation_client.rb', line 41

def reply_to_activity(conversation_id, activity_id, activity, service_url: nil)
  body = activity_to_h(activity)
  body = body.merge("replyToId" => activity_id) if body.is_a?(Hash)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API POST #{url}")
  http.post(url, json: body)
end

#update_activity(conversation_id, activity_id, activity, service_url: nil) ⇒ Object



50
51
52
53
54
55
# File 'lib/teams/api/conversation_client.rb', line 50

def update_activity(conversation_id, activity_id, activity, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API PUT #{url}")
  http.put(url, json: activity_to_h(activity))
end

#update_targeted_activity(conversation_id, activity_id, activity, service_url: nil) ⇒ Object



71
72
73
74
75
76
# File 'lib/teams/api/conversation_client.rb', line 71

def update_targeted_activity(conversation_id, activity_id, activity, service_url: nil)
  path = "/v3/conversations/#{escape(conversation_id)}/activities/#{escape(activity_id)}"
  url = absolute(path, service_url:)
  @logger&.debug("Teams API PUT #{url} (targeted)")
  http.put(url, json: activity_to_h(activity), params: TARGETED_PARAMS)
end