A Discord API Library for Gleam! 💫
0

Configure Feed

Select the types of activity you want to include in your feed.

move stuff around more mindfully in the example

+66 -64
+66 -64
examples/http_interactions/src/http_interactions.gleam
··· 18 18 import wisp 19 19 import wisp/wisp_mist 20 20 21 - type RequestHandlerState { 22 - RequestHandlerState( 23 - client: grom.Client, 24 - discord_public_key: String, 25 - interaction_handler_name: process.Name( 26 - factory_supervisor.Message( 27 - Interaction, 28 - process.Subject(InteractionHandlerMessage), 29 - ), 30 - ), 31 - ) 32 - } 33 - 34 21 type InteractionHandlerState { 35 22 InteractionHandlerState(client: grom.Client) 36 23 } ··· 101 88 // We're going to create a static supervisor later on, so let's use the `mist.supervised` function. 102 89 let http_server = 103 90 wisp_mist.handler( 91 + // We have to create an anonymous function here, since mist expects a fn(Request), 92 + // and our handler also has the context in its signature. 104 93 fn(request) { 105 94 handle_request( 106 95 request, 107 - RequestHandlerState(client, public_key, interaction_handler_name), 96 + RequestHandlerContext(client, public_key, interaction_handler_name), 108 97 ) 109 98 }, 110 99 secret_key_base, ··· 124 113 process.sleep_forever() 125 114 } 126 115 116 + type RequestHandlerContext { 117 + RequestHandlerContext( 118 + client: grom.Client, 119 + discord_public_key: String, 120 + interaction_handler_name: process.Name( 121 + factory_supervisor.Message( 122 + Interaction, 123 + process.Subject(InteractionHandlerMessage), 124 + ), 125 + ), 126 + ) 127 + } 128 + 129 + fn handle_request( 130 + request: wisp.Request, 131 + state: RequestHandlerContext, 132 + ) -> wisp.Response { 133 + case wisp.path_segments(request) { 134 + ["discord-interactions"] -> { 135 + use body <- wisp.require_string_body(request) 136 + 137 + let request = Request(..request, body:) 138 + 139 + interaction.handle_http_interaction_request( 140 + request, 141 + state.discord_public_key, 142 + fn(interaction) { handle_interaction_request(state, interaction) }, 143 + ) 144 + |> response.map(wisp.Text) 145 + } 146 + _ -> wisp.not_found() 147 + } 148 + } 149 + 150 + fn handle_interaction_request( 151 + state: RequestHandlerContext, 152 + interaction: Result(Interaction, interaction.HttpError), 153 + ) -> Nil { 154 + case interaction { 155 + Ok(interaction) -> { 156 + let factory = 157 + factory_supervisor.get_by_name(state.interaction_handler_name) 158 + 159 + let start_result = 160 + factory 161 + |> factory_supervisor.start_child(interaction) 162 + 163 + case start_result { 164 + Ok(_) -> logging.log(logging.Info, "Started interaction handler worker") 165 + Error(_) -> 166 + logging.log( 167 + logging.Warning, 168 + "Could not start interaction handler worker", 169 + ) 170 + } 171 + } 172 + Error(interaction.CouldNotParseInteraction(_)) -> 173 + logging.log(logging.Warning, "Could not parse interaction") 174 + Error(interaction.CouldNotValidateSecurityHeaders(_)) -> 175 + logging.log(logging.Warning, "Could not validate security headers") 176 + } 177 + } 178 + 127 179 type InteractionHandlerMessage { 128 180 InteractionCreated(interaction: Interaction) 129 181 } ··· 178 230 179 231 actor.continue(state) 180 232 } 181 - 182 - fn handle_request( 183 - request: wisp.Request, 184 - state: RequestHandlerState, 185 - ) -> wisp.Response { 186 - case wisp.path_segments(request) { 187 - ["discord-interactions"] -> { 188 - use body <- wisp.require_string_body(request) 189 - 190 - let request = Request(..request, body:) 191 - 192 - interaction.handle_http_interaction_request( 193 - request, 194 - state.discord_public_key, 195 - fn(interaction) { handle_interaction_request(state, interaction) }, 196 - ) 197 - |> response.map(wisp.Text) 198 - } 199 - _ -> wisp.not_found() 200 - } 201 - } 202 - 203 - fn handle_interaction_request( 204 - state: RequestHandlerState, 205 - interaction: Result(Interaction, interaction.HttpError), 206 - ) -> Nil { 207 - case interaction { 208 - Ok(interaction) -> { 209 - let factory = 210 - factory_supervisor.get_by_name(state.interaction_handler_name) 211 - 212 - let start_result = 213 - factory 214 - |> factory_supervisor.start_child(interaction) 215 - 216 - case start_result { 217 - Ok(_) -> logging.log(logging.Info, "Started interaction handler worker") 218 - Error(_) -> 219 - logging.log( 220 - logging.Warning, 221 - "Could not start interaction handler worker", 222 - ) 223 - } 224 - } 225 - Error(interaction.CouldNotParseInteraction(_)) -> 226 - logging.log(logging.Warning, "Could not parse interaction") 227 - Error(interaction.CouldNotValidateSecurityHeaders(_)) -> 228 - logging.log(logging.Warning, "Could not validate security headers") 229 - } 230 - }