0
Hello, I have a code that integrates Jira and Discord from a webhook. It works correctly, however I would like to add more than one webhook. But I’m not quite sure how to do this.
using System;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
static HttpClient client = new HttpClient();
private static string jiraBaseURL = "http://tananam.atlassian.net/browse/";
//private static string discordURL = "https://discord.com/api/webhooks/796417413351473162/w0VgRSQfVMqa2PdhNNB40oQahdVodKzhrqH94mF43xxJ5eK3XePKw1CKGXvfX28qjp1s";
private static string botName = "JIRA";
// POST api/<JiraToDiscordMessage>
[HttpPost, Route("Id")]
public async Task<IActionResult> PostAsync([FromBody] JiraEvent request, int Id)
{
var discordMesage = FromJiraToDiscordMessage(request);
if (discordMesage == null) { return BadRequest(); }
await SendToDiscordAsync(discordMesage);
return Ok();
}
public DiscordMessage FromJiraToDiscordMessage(JiraEvent evt)
{
var eventType = evt.webhookEvent;
var issueKey = evt.issue.key;
var issueDescription = evt.issue.fields.summary;
string authorName, avatarUrl, userUrl;
string link;
string updatedFieldName, updatedFieldOldValue, updatedFieldNewValue;
if (eventType.Contains("comment"))
{
var commentId = evt.comment.id;
authorName = evt.comment.author.displayName;
avatarUrl = evt.comment.author.avatarUrls._48x48;
userUrl = evt.comment.author.self;
link = jiraBaseURL + issueKey + "?focusedCommentId=" + commentId;
updatedFieldName = "Comment";
updatedFieldOldValue = string.Empty;
updatedFieldNewValue = evt.comment.body;
}
else
{
authorName = evt.user.displayName;
avatarUrl = evt.user.avatarUrls._48x48;
userUrl = evt.user.self;
link = jiraBaseURL + issueKey;
var changelogItem = evt.changelog.items.Last();
updatedFieldName = changelogItem.field;
updatedFieldOldValue = changelogItem.fromString;
updatedFieldNewValue = changelogItem.toString;
}
if (!string.IsNullOrEmpty(issueDescription))
{
issueDescription = issueDescription.Replace("{{", "```\n");
issueDescription = issueDescription.Replace("}}", "\n```");
issueDescription = issueDescription.Replace("{code}\r\n", "```\n");
issueDescription = issueDescription.Replace("\r\n{code}", "\n```");
issueDescription = issueDescription.Replace("*", "");
if (issueDescription.Length > 250)
{
issueDescription = issueDescription.Substring(0, 100);
int numTags = Regex.Matches(issueDescription, "```").Count;
if (numTags % 2 != 0)
{
issueDescription += " ...\n```" + "\n([see full description](" + link + "))";
}
else
{
issueDescription += " ... ([see full description](" + link + "))";
}
}
}
Console.WriteLine("Got webhook event '" + eventType + "' [" + issueDescription + "]" + " by " + authorName);
return new DiscordMessage(
botName,
issueKey,
issueDescription,
eventType,
link,
authorName,
avatarUrl,
userUrl,
updatedFieldName,
updatedFieldOldValue,
updatedFieldNewValue);
}
async Task SendToDiscordAsync(DiscordMessage discordMessage)
{
var discordMessageJson = JsonSerializer.Serialize(discordMessage, new JsonSerializerOptions { WriteIndented = true });
await client.PostAsync(discordURL, new StringContent(discordMessageJson, Encoding.UTF8, "application/json"));
}
}
}
This is the code of my controller, in appsettings.json I created a list with the addresses of the other webhooks, at first the idea and call the url and id referring to the webhook I want. But not how to get these addresses.
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"Webhooks": {
"Default": "https://discordapp.com/api/webhooks/804475940507746375/61vi3eGVRvMklBxdx5FpSUZBxXBCg4Jv4szVShxncgcSvm2u3VQ7X8nsoQT8BVcfNu6w",
"FromJira": [
{
"Id": "abc",
"ToDiscord": "https://discordapp.com/api/webhooks/813470697765863495/0dqKpYj1Xx3t_tcHYjShG8ijsh7c7jP2wRBnqj4jmPe1bMw0AKsa8J9XGY_jO4kgAxXp"
},
{
"Id": "xyz",
"ToDiscord": "https://discordapp.com/api/webhooks/813470824765718549/eeTuTgZ8-UrshaTC9X4mmjQ-XniZ3thsfGwKk48UcoPFCGq8MZUKzJhVYSiVfLGzKbps"
}
]
}
}