public static async Task<string> SendMessageToChannel(string message)
{
string token = (await GetToken()).ToString();
if (token != string.Empty)
{
string channelId = "bf157781716f42d19e151a7cf7c63f85";
var builder = WebApplication.CreateBuilder();
var configuration = builder.Configuration;
HttpClient _client = new HttpClient();
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
UriBuilder _uri = new UriBuilder("https://api.zoom.us/v2/chat/users/P6kvmalrQMK9pceHN8RMYw/messages");
try
{
string json1 = "{\"message\":\"" + message + "\",\"to_channel\":\"" + channelId + "\"}";
string json2 = $@"{{
""message"": ""{message}"",
""at_items"": [
{{
""at_contact"": ""<email>"",
""at_type"": 1,
""end_position"": ""<integer>"",
""start_position"": ""<integer>""
}},
{{
""at_contact"": ""<email>"",
""at_type"": 1,
""end_position"": ""<integer>"",
""start_position"": ""<integer>""
}}
],
""rich_text"": [
{{
""start_position"": ""<integer>"",
""end_position"": ""<integer>"",
""format_type"": ""Italic"",
""format_attr"": ""<string>""
}},
{{
""start_position"": ""<integer>"",
""end_position"": ""<integer>"",
""format_type"": ""LeftIndent"",
""format_attr"": ""<string>""
}}
],
""file_ids"": [
""<string>"",
""<string>""
],
""reply_main_message_id"": ""<string>"",
""to_channel"": ""{channelId}"",
""to_contact"": ""<email>""
}}";
var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = _uri.Uri,
Content = new StringContent(
json1,
Encoding.UTF8,
MediaTypeNames.Application.Json),
};
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token);
var response = await _client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
var responseBody = await response.Content.ReadAsStringAsync();
//var x= JsonDocument.Parse(responseBody).RootElement.GetProperty("access_token").ToString();
return responseBody;
}
catch (Exception e)
{
return string.Empty;
}
}
return string.Empty;
}
Can you help me with this code, as it only returns Bad Request. I’ve already used content json1 and json2, it generates the same error. Which fields are mandatory, do they have a correct sequence?