-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
almost have chat send message working
- Loading branch information
Showing
3 changed files
with
102 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import gleam/dynamic.{type Decoder} | ||
import gleam/option.{type Option} | ||
import gleam/result | ||
import gleam/json.{type DecodeError, type Json} | ||
import gleam/http/response.{type Response} | ||
import gleam/http/request | ||
import glitch/api/client.{type Client} | ||
import glitch/extended/json_ext | ||
import glitch/api/api_response | ||
|
||
pub type Message { | ||
Message(message_id: String, is_sent: Bool) | ||
} | ||
|
||
fn message_decoder() -> Decoder(Message) { | ||
dynamic.decode2( | ||
Message, | ||
dynamic.field("message_id", dynamic.string), | ||
dynamic.field("is_sent", dynamic.bool), | ||
) | ||
} | ||
|
||
pub fn message_from_json(json_string: String) -> Result(Message, DecodeError) { | ||
json.decode(json_string, message_decoder()) | ||
} | ||
|
||
pub type SendMessageRequest { | ||
SendMessageRequest( | ||
broadcaster_id: String, | ||
sender_id: String, | ||
message: String, | ||
reply_parent_message_id: Option(String), | ||
) | ||
} | ||
|
||
fn send_message_request_to_json(request: SendMessageRequest) -> Json { | ||
json.object([ | ||
#("broadcaster_id", json.string(request.broadcaster_id)), | ||
#("sender_id", json.string(request.sender_id)), | ||
#("message", json.string(request.message)), | ||
#( | ||
"reply_parent_message_id", | ||
json_ext.option(request.reply_parent_message_id, json.string), | ||
), | ||
]) | ||
} | ||
|
||
pub type SendMessageError { | ||
DecodeError(DecodeError) | ||
RequestError | ||
} | ||
|
||
pub fn send_message( | ||
client: Client, | ||
request: SendMessageRequest, | ||
) -> Result(Response(List(Message)), SendMessageError) { | ||
let request = | ||
request.new() | ||
|> request.set_body(send_message_request_to_json(request)) | ||
|> request.set_path("chat/messages") | ||
|
||
use response <- result.try( | ||
client | ||
|> client.post(request) | ||
|> result.replace_error(RequestError), | ||
) | ||
|
||
response | ||
|> response.try_map(api_response.from_json(_, of: message_decoder())) | ||
|> result.try(api_response.get_data_from_response) | ||
|> result.map_error(DecodeError) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters