Update send_whisper function

Correctly use API
Display more explicit messages in error case
This commit is contained in:
Wathis 2023-12-16 14:34:07 +01:00
parent 2531c6f446
commit 1cc126f34b

View File

@ -107,19 +107,28 @@ func send_whisper(from_user_id : String, to_user_id : String, message : String)
"Client-Id: %s" % id_conn.last_token.last_client_id, "Client-Id: %s" % id_conn.last_token.last_client_id,
"Content-Type: application/json" "Content-Type: application/json"
] ]
var response = await(request(HTTPClient.METHOD_POST, "/helix/eventsub/whispers?from_user_id=%s&to_user_id=%s", headers, JSON.stringify({"message": message}))) var params: String = "?"
match (client.get_response_code()): params += "from_user_id=%s" % from_user_id
204: params += "&to_user_id=%s" % to_user_id
var response: Dictionary = await(
request(
HTTPClient.METHOD_POST,
"/helix/whispers" + params,
headers,
JSON.stringify({"message": message})
)
)
var response_code: int = client.get_response_code()
match (response_code):
# 200 is returned even if Twitch documentation says only 204
200, 204:
print("Success! The whisper was sent.")
return true return true
400: # Complete list of error codes according to Twitch documentation
print("Bad Request! Check the data you specified.") 400, 401, 403, 404, 429:
print("[Error (send_whisper)] %s - %s" % [response["status"], response["message"]])
return false return false
403: # Fallback for unknown response codes
print("Forbidden! The account that's sending the message doesn't allow sending whispers.") _:
print("[Default (send_whisper)] %s " % response_code)
return false return false
404:
print("Not Found! The ID in to_user_id was not found.")
429:
print("Too Many Requests! The sending user exceeded the number of whisper requests that they may make.")
return false
return false