Class: ChatSDK::Teams::BotFrameworkClient

Inherits:
Object
  • Object
show all
Defined in:
lib/chat_sdk/teams/bot_framework_client.rb

Constant Summary collapse

TOKEN_URL =
"https://login.microsoftonline.com/botframework.com/oauth2/v2.0/token"
SCOPE =
"https://api.botframework.com/.default"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_id:, app_password:) ⇒ BotFrameworkClient

Returns a new instance of BotFrameworkClient.



11
12
13
14
15
16
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 11

def initialize(app_id:, app_password:)
  @app_id = app_id
  @app_password = app_password
  @access_token = nil
  @token_expires_at = nil
end

Instance Attribute Details

#app_idObject (readonly)

Returns the value of attribute app_id.



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

def app_id
  @app_id
end

#app_passwordObject (readonly)

Returns the value of attribute app_password.



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

def app_password
  @app_password
end

Instance Method Details

#create_conversation(service_url:, payload:) ⇒ Object



33
34
35
36
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 33

def create_conversation(service_url:, payload:)
  url = "#{service_url.chomp("/")}/v3/conversations"
  authorized_request(:post, url, payload)
end

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



28
29
30
31
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 28

def delete_activity(service_url:, conversation_id:, activity_id:)
  url = "#{service_url.chomp("/")}/v3/conversations/#{conversation_id}/activities/#{activity_id}"
  authorized_request(:delete, url)
end

#fetch_tokenObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 43

def fetch_token
  return @access_token if @access_token && @token_expires_at && Time.now < @token_expires_at

  response = token_connection.post(TOKEN_URL, {
    grant_type: "client_credentials",
    client_id: @app_id,
    client_secret: @app_password,
    scope: SCOPE
  })

  unless response.success?
    raise ChatSDK::PlatformError.new(
      "Failed to acquire Bot Framework token: #{response.status}",
      status: response.status,
      body: response.body,
      adapter_name: :teams
    )
  end

  data = JSON.parse(response.body)
  @access_token = data["access_token"]
  @token_expires_at = Time.now + (data["expires_in"].to_i - 60)
  @access_token
end

#get_conversation_members(service_url:, conversation_id:) ⇒ Object



38
39
40
41
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 38

def get_conversation_members(service_url:, conversation_id:)
  url = "#{service_url.chomp("/")}/v3/conversations/#{conversation_id}/members"
  authorized_request(:get, url)
end

#send_activity(service_url:, conversation_id:, activity:) ⇒ Object



18
19
20
21
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 18

def send_activity(service_url:, conversation_id:, activity:)
  url = "#{service_url.chomp("/")}/v3/conversations/#{conversation_id}/activities"
  authorized_request(:post, url, activity)
end

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



23
24
25
26
# File 'lib/chat_sdk/teams/bot_framework_client.rb', line 23

def update_activity(service_url:, conversation_id:, activity_id:, activity:)
  url = "#{service_url.chomp("/")}/v3/conversations/#{conversation_id}/activities/#{activity_id}"
  authorized_request(:put, url, activity)
end