docs: Add Webhook example. (#1241)
* Add Webhook example. * Fix spacing. * Update to use webhook URL ctor.
This commit is contained in:
committed by
Christopher F
parent
4eb958dcee
commit
655a0069a1
13
samples/04_webhook_client/04_webhook_client.csproj
Normal file
13
samples/04_webhook_client/04_webhook_client.csproj
Normal file
@@ -0,0 +1,13 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||
<RootNamespace>_04_webhook_client</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Discord.Net.Webhook\Discord.Net.Webhook.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
34
samples/04_webhook_client/Program.cs
Normal file
34
samples/04_webhook_client/Program.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using Discord;
|
||||
using Discord.Webhook;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace _04_webhook_client
|
||||
{
|
||||
// This is a minimal example of using Discord.Net's Webhook Client
|
||||
// Webhooks are send-only components of Discord that allow you to make a POST request
|
||||
// To a channel specific URL to send a message to that channel.
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
=> new Program().MainAsync().GetAwaiter().GetResult();
|
||||
|
||||
public async Task MainAsync()
|
||||
{
|
||||
// The webhook url follows the format https://discordapp.com/api/webhooks/{id}/{token}
|
||||
// Because anyone with the webhook URL can use your webhook
|
||||
// you should NOT hard code the URL or ID + token into your application.
|
||||
using (var client = new DiscordWebhookClient("https://discordapp.com/api/webhooks/123/abc123"))
|
||||
{
|
||||
var embed = new EmbedBuilder
|
||||
{
|
||||
Title = "Test Embed",
|
||||
Description = "Test Description"
|
||||
};
|
||||
|
||||
// Webhooks are able to send multiple embeds per message
|
||||
// As such, your embeds must be passed as a collection.
|
||||
await client.SendMessageAsync(text: "Send a message to this webhook!", embeds: new[] { embed.Build() });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user