Whatsmeow.Extensions.Hosting
1.0.0-rc.4
dotnet add package Whatsmeow.Extensions.Hosting --version 1.0.0-rc.4
NuGet\Install-Package Whatsmeow.Extensions.Hosting -Version 1.0.0-rc.4
<PackageReference Include="Whatsmeow.Extensions.Hosting" Version="1.0.0-rc.4" />
<PackageVersion Include="Whatsmeow.Extensions.Hosting" Version="1.0.0-rc.4" />
<PackageReference Include="Whatsmeow.Extensions.Hosting" />
paket add Whatsmeow.Extensions.Hosting --version 1.0.0-rc.4
#r "nuget: Whatsmeow.Extensions.Hosting, 1.0.0-rc.4"
#:package Whatsmeow.Extensions.Hosting@1.0.0-rc.4
#addin nuget:?package=Whatsmeow.Extensions.Hosting&version=1.0.0-rc.4&prerelease
#tool nuget:?package=Whatsmeow.Extensions.Hosting&version=1.0.0-rc.4&prerelease
Whatsmeow.Extensions.Hosting
Dependency injection and a generic host for Whatsmeow.Client.
AddWhatsmeow() binds the options from the Whatsmeow configuration section, starts the sidecar with the host and stops it after every hosted service, declares sessions as keyed IWhatsAppClient services, and runs IWhatsAppEventHandler<TEvent> handlers in a DI scope per event.
This is not an official WhatsApp client and has nothing to do with WhatsApp or Meta.
Quick start
Two sessions that answer every text with the same text:
using Microsoft.Extensions.DependencyInjection;
using Whatsmeow.Events;
using Whatsmeow.Extensions.Hosting;
using Whatsmeow.History;
namespace EchoBot;
/// <summary>The echo bot's setup: one sidecar for the host, two sessions, their handlers, and pairing on the console.</summary>
public static class EchoBotServices
{
/// <summary>The first session's id, which is also its service key.</summary>
public const string Alice = "alice";
/// <summary>The second session's id, which is also its service key.</summary>
public const string Bob = "bob";
public static WhatsmeowBuilder AddEchoBot(this IServiceCollection services)
{
var whatsmeow = services
.AddWhatsmeow()
.AddSession(Alice)
.AddSession(Bob)
.AddHandler<MessageReceived, EchoHandler>()
.AddHandler<SessionStateChanged, StateLog>()
.AddHandler<HistoryReceived, HistoryLog>();
_ = services.AddHostedService<QrPairing>();
return whatsmeow;
}
}
using System.Security.Cryptography;
using System.Text;
using Microsoft.Extensions.Logging;
using Whatsmeow;
using Whatsmeow.Events;
using Whatsmeow.Extensions.Hosting;
namespace EchoBot;
/// <summary>Answers each text a session receives with the same text, in the same chat.</summary>
public sealed partial class EchoHandler(ILogger<EchoHandler> logger) : IWhatsAppEventHandler<MessageReceived>
{
private const string Prefix = "echo: ";
public async ValueTask HandleAsync(MessageReceived e, IWhatsAppClient client, CancellationToken cancellationToken)
{
// Never a status (status@broadcast), which cannot be answered; never the account's own texts, nor an echo: two
// sessions that text each other would echo each other forever.
if (IsBroadcast(e.Source.Chat) || e.Source.IsFromMe || e.Content is not TextContent { Text: var text } || text.StartsWith(Prefix, StringComparison.Ordinal))
return;
// A message can come again, after a stop during the send or a crash before its ack; the echo's id is the same
// each time, and a send of an id already sent is never sent again, so each text is echoed once.
_ = await client.SendTextAsync(e.Source.Chat, Prefix + text, EchoId(e), cancellationToken: cancellationToken).ConfigureAwait(false);
Echoed(logger, e.Id.Value, e.SessionId.Value, e.Timestamp);
}
private static bool IsBroadcast(Jid chat) => string.Equals(chat.Server, "broadcast", StringComparison.Ordinal);
/// <summary>The echo's id, from the session, the chat and the message's id, in the only format a send takes: <c>3EB0</c> and 18 uppercase hex digits.</summary>
private static MessageId EchoId(MessageReceived e) =>
MessageId.From("3EB0" + Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes($"{e.SessionId.Value}\n{e.Source.Chat}\n{e.Id.Value}")))[..18]);
[LoggerMessage(1, LogLevel.Information, "Echoed {MessageId} of session {SessionId}, sent at {Timestamp:O}")]
private static partial void Echoed(ILogger logger, string messageId, string sessionId, DateTimeOffset timestamp);
}
The full sample, with its QR code pairing and OpenTelemetry, is dotnet/samples/EchoBot.
Hosting and testing covers configuration, keyed sessions and the host's lifecycle.
License: MPL-2.0.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Google.Protobuf (>= 3.36.2)
- Grpc.Core.Api (>= 2.85.0)
- Grpc.HealthCheck (>= 2.85.0)
- Grpc.Net.Client (>= 2.85.0)
- Grpc.StatusProto (>= 2.85.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 11.0.0)
- Vogen (>= 8.0.7)
- Whatsmeow.Client (= 1.0.0-rc.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Whatsmeow.Extensions.Hosting:
| Package | Downloads |
|---|---|
|
Whatsmeow.Testing
FakeWhatsAppClient, an in-memory IWhatsAppClient to test Whatsmeow.NET handlers without WhatsApp, and UseFakeClient for a host. |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated | |
|---|---|---|---|
| 1.0.0-rc.4 | 81 | 11/12/2026 | |
| 1.0.0-rc.2 | 84 | 11/11/2026 | |
| 1.0.0-rc.1 | 96 | 11/11/2026 |