Class: ChatSDK::Teams::Adapter
- Inherits:
-
Adapter::Base
- Object
- Adapter::Base
- ChatSDK::Teams::Adapter
- Defined in:
- lib/chat_sdk/teams/adapter.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #delete_message(channel_id:, message_id:) ⇒ Object
- #edit_message(channel_id:, message_id:, message:) ⇒ Object
- #fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) ⇒ Object
-
#initialize(app_id: nil, app_password: nil, tenant_id: nil) ⇒ Adapter
constructor
A new instance of Adapter.
- #mention(user_id) ⇒ Object
- #name ⇒ Object
- #open_dm(user_id) ⇒ Object
- #open_modal(trigger_id:, modal:) ⇒ Object
- #parse_events(rack_request) ⇒ Object
- #post_ephemeral(channel_id:, user_id:, message:, thread_id: nil) ⇒ Object
-
#post_message(channel_id:, message:, thread_id: nil) ⇒ Object
Outbound.
-
#register_service_url(conversation_id, service_url) ⇒ Object
Store a service URL for a conversation (useful when sending proactive messages).
- #remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object
- #render(postable_message) ⇒ Object
- #start_typing(channel_id:, thread_id: nil) ⇒ Object
- #upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
-
#verify_request!(rack_request) ⇒ Object
Inbound.
Constructor Details
#initialize(app_id: nil, app_password: nil, tenant_id: nil) ⇒ Adapter
Returns a new instance of Adapter.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/chat_sdk/teams/adapter.rb', line 11 def initialize(app_id: nil, app_password: nil, 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 @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
#client ⇒ Object (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
#add_reaction(channel_id:, message_id:, emoji:) ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/chat_sdk/teams/adapter.rb', line 132 def add_reaction(channel_id:, message_id:, emoji:) require_capability!(:reactions) # Teams Bot Framework API does not support adding reactions programmatically # The capability is declared because inbound reactions are parsed raise ChatSDK::PlatformError.new( "Teams Bot Framework API does not support adding reactions programmatically", adapter_name: :teams ) end |
#delete_message(channel_id:, message_id:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/chat_sdk/teams/adapter.rb', line 93 def (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: ) end |
#edit_message(channel_id:, message_id:, message:) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/chat_sdk/teams/adapter.rb', line 77 def (channel_id:, message_id:, message:) require_capability!(:edit_messages) msg = ChatSDK::PostableMessage.from() service_url = service_url_for(channel_id) activity = build_activity(msg) activity["id"] = @client.update_activity( service_url: service_url, conversation_id: channel_id, activity_id: , activity: activity ) end |
#fetch_messages(channel_id:, thread_id: nil, cursor: nil, limit: 50) ⇒ Object
173 174 175 176 177 178 |
# File 'lib/chat_sdk/teams/adapter.rb', line 173 def (channel_id:, thread_id: nil, cursor: nil, limit: 50) require_capability!(:message_history) # Teams Bot Framework doesn't provide a message history API # Return empty to satisfy the interface [[], nil] end |
#mention(user_id) ⇒ Object
188 189 190 |
# File 'lib/chat_sdk/teams/adapter.rb', line 188 def mention(user_id) "<at>#{user_id}</at>" end |
#name ⇒ Object
25 26 27 |
# File 'lib/chat_sdk/teams/adapter.rb', line 25 def name :teams end |
#open_dm(user_id) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/chat_sdk/teams/adapter.rb', line 150 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
180 181 182 |
# File 'lib/chat_sdk/teams/adapter.rb', line 180 def open_modal(trigger_id:, modal:) super # raises NotSupportedError end |
#parse_events(rack_request) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/chat_sdk/teams/adapter.rb', line 39 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
104 105 106 |
# File 'lib/chat_sdk/teams/adapter.rb', line 104 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/chat_sdk/teams/adapter.rb', line 53 def (channel_id:, message:, thread_id: nil) msg = ChatSDK::PostableMessage.from() 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)
202 203 204 |
# File 'lib/chat_sdk/teams/adapter.rb', line 202 def register_service_url(conversation_id, service_url) @service_urls[conversation_id] = service_url end |
#remove_reaction(channel_id:, message_id:, emoji:) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/chat_sdk/teams/adapter.rb', line 142 def remove_reaction(channel_id:, message_id:, emoji:) require_capability!(:reactions) raise ChatSDK::PlatformError.new( "Teams Bot Framework API does not support removing reactions programmatically", adapter_name: :teams ) end |
#render(postable_message) ⇒ Object
192 193 194 195 196 197 198 199 |
# File 'lib/chat_sdk/teams/adapter.rb', line 192 def render() msg = ChatSDK::PostableMessage.from() if msg.card? @renderer.render(msg.card) else msg.text end end |
#start_typing(channel_id:, thread_id: nil) ⇒ Object
184 185 186 |
# File 'lib/chat_sdk/teams/adapter.rb', line 184 def start_typing(channel_id:, thread_id: nil) super # raises NotSupportedError end |
#upload_file(channel_id:, io:, filename:, thread_id: nil, comment: nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/chat_sdk/teams/adapter.rb', line 108 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
30 31 32 33 34 35 36 37 |
# File 'lib/chat_sdk/teams/adapter.rb', line 30 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 |