@@ -15,48 +15,18 @@ def interactions
1515 return head :unauthorized
1616 end
1717
18- body = JSON . parse ( request . body . read )
18+ body = request . body . read
19+ interaction = JSON . parse ( body , object_class : OpenStruct )
1920
20- if body [ " type" ] == 1
21+ if interaction . type == 1
2122 # Respond to Discord PING request
2223 render json : { type : 1 }
2324 else
2425 response = { type : 5 , data : { content : "Searching..." } }
2526 hijack { render json : response }
2627
27- pp request . headers
28- pp body
29-
30- # Respond to /commands
31- persona = DiscourseAi ::AiBot ::Personas ::DiscourseHelper
32- bot =
33- DiscourseAi ::AiBot ::Bot . as (
34- Discourse . system_user ,
35- persona : persona . new ,
36- model : "custom:6" ,
37- )
38-
39- query = body [ "data" ] [ "options" ] . first [ "value" ]
40- reply = ""
41- reply =
42- bot . reply ( { conversation_context : [ { type : :user , content : query } ] } ) { |a , b , c | nil }
43-
44- pp reply . last . first
45-
46- discord_reply = reply . last . first
47-
48- api_endpoint =
49- "https://discord.com/api/webhooks/#{ SiteSetting . ai_discord_app_id } /#{ body [ "token" ] } "
50-
51- conn = Faraday . new { |f | f . adapter FinalDestination ::FaradayAdapter }
52- response =
53- conn . post (
54- api_endpoint ,
55- { content : discord_reply } . to_json ,
56- { "Content-Type" => "application/json" } ,
57- )
58-
59- pp response
28+ # Respond to Discord command
29+ Jobs . enqueue ( :stream_discord_reply , interaction : body . dup )
6030 end
6131 end
6232
@@ -69,7 +39,6 @@ def verify_request!
6939 end
7040
7141 def verify_key
72- # TODO remove this gem dependency
7342 Ed25519 ::VerifyKey . new ( [ SiteSetting . ai_discord_app_public_key ] . pack ( "H*" ) ) . freeze
7443 end
7544 end
0 commit comments