Connecting the bot
Once Wolfringo is installed, it's time to get your bot connected. The way to do it varies whether you're using Wolfringo.Hosting package or not.
First, add following using directives to your Program.cs:
using TehGM.Wolfringo;
using TehGM.Wolfringo.Messages;
using TehGM.Wolfringo.Utilities;
Create Client
Inside of Program class, add a new variable to store your bot client in:
class Program
{
private static IWolfClient _client;
}
Modify your Main method and add a new MainAsync method:
Note: this step can be skipped if you're using C# 7.1 or later - in such case, simply change return type of Main from
void
toTask
.
static void Main(string[] args)
{
MainAsync(args).GetAwaiter().GetResult();
}
static async Task MainAsync(string[] args)
{
// other startup code will go here!
}
Now we need to do a few things - create a WolfClient instance using a builder, register event listeners, connect the bot, and prevent application from exiting. To do this, you can use following code inside of your MainAsync method:
// create client and listen to events we're interested in
_client = new WolfClientBuilder().Build();
// these will show error for now - don't worry, we'll handle that in a moment!
_client.AddMessageListener<WelcomeEvent>(OnWelcome);
_client.AddMessageListener<ChatMessage>(OnChatMessage);
// start connection and prevent the application from closing
await _client.ConnectAsync();
await Task.Delay(-1);
AddMessageListener is a method to add event listener for receiving messages and events. It takes a generic parameter and a callback as normal parameter. It'll call callback when a received message is of type specified by generic parameter - for example, in the code above, OnWelcome will be called when WelcomeEvent is received, and OnChatMessage when ChatMessage is received.
Wolfringo contains many message types - check TehGM.Wolfringo.Messages to check out others!
Note
It is recommended that you enable logging as well - check Logging guide to see how!
Make the bot reconnect automatically
We already have all code to get bot started, but it won't reconnect automatically - and WOLF protocol forces disconnection eveyr hour. Not good!
But don't worry - there's an easy way to enable automatic reconnection.
Wolfringo.Utilities package (installed by default when you install Wolfringo metapackage) has an utility class called WolfClientReconnector. This class is 'outside' of WolfClient because that makes this class implementation independent.
To enable WolfClientReconnector the easy way, you can simply add it to the builder:
_client = new WolfClientBuilder()
.WithAutoReconnection(reconnector =>
{
// by default, ReconnectorConfig will retry 5 times - here we change it to -1, which makes it infinite
reconnector.ReconnectAttempts = -1;
})
.Build();
Tip
Pro tip: Check out Reconnecting guide for more details.
Add event listeners
There are numerous events that can come from WolfClient, but the most important are when server sends "Welcome" (which is the place to login the bot) and when bot receives a chat message. To handle them, let's add 2 new event listener methods to Program class.
// this method will be called when "Server" welcomes the bot - this is where we login!
private static async void OnWelcome(WelcomeEvent message)
{
// if reusing the token, user might be already logged in, so check that before requesting login
if (message.LoggedInUser == null)
{
// note: it is recommended to not hardcode username and password, and use .gitignore-d config file instead
// see exaple project linked below for a full example!
await _client.LoginAsync("BotEmail", "BotPassword", WolfLoginType.Email);
}
await _client.SubscribeAllMessagesAsync(); // without this, bot will not receive any messages
}
// this method will be called whenever the bot receives a message in chat
// see Commands System guides to check how implement proper commands, without using this listener at all
private static async void OnChatMessage(ChatMessage message)
{
// reply only to private text messages that start with "!mybot hello"
if (message.IsText && message.Text.StartsWith("!mybot hello", StringComparison.OrdinalIgnoreCase))
{
await _client.ReplyTextAsync(message, "Hello there!!!");
}
}
Testing the bot
You're now ready to run the project. Go ahead, and send "!mybot hello" to your bot's account. It should reply!
Moving forward
While using _client.AddMessageListener<ChatMessage>(OnChatMessage);
and async void OnChatMessage(ChatMessage message)
works fine for testing, you probably want to develop fully fledged commands with your bot. Check Commands System guides to see how!
Bot is a background program by nature, and as with any background program, knowing what is going on can be useful. For this reason Wolfringo has full logging support - check Logging Guide to see how to enable it.
You can also check SimplePingBot Example (Normal Bot) or HostedPingBot Example (.NET Generic Host/ASP.NET Core) for full example on basic Wolfringo usage. Feel free to also check other example projects!