登录时提示 Error when processing the event: Lagrange.Core.Internal.Event.Login.LoginEvent #341
-
非常抱歉(((
报错日志:
|
Beta Was this translation helpful? Give feedback.
Answered by
TheSnowfield
Apr 28, 2024
Replies: 2 comments 7 replies
-
先 FetchQrCode() |
Beta Was this translation helpful? Give feedback.
7 replies
-
@NoNameGMM 參考 kagami 的實現如下 public static async Task Main()
{
Console.OutputEncoding = Encoding.UTF8;
// Create bot
_bot = BotFactory.Create(GetConfig(), GetDevice(), GetKeyStore());
{
// Print the log
_bot.Invoker.OnBotLogEvent += (_, e) =>
Console.WriteLine($"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] " +
$"[{e.Level}] [{e.Tag}] {e.EventMessage}");
// Handle the captcha
_bot.Invoker.OnBotCaptchaEvent += (s, e) =>
{
Console.WriteLine($"Captcha: {e.Url}");
Console.WriteLine("Please input ticket and randomString:");
var ticket = Console.ReadLine();
var randomString = Console.ReadLine();
if (ticket != null && randomString != null) s.SubmitCaptcha(ticket, randomString);
};
// Handle group messages
_bot.Invoker.OnGroupMessageReceived += GroupMessageHandler;
// Update the keystore
_bot.Invoker.OnBotOnlineEvent += (bot, _) =>
{
UpdateKeystore(bot.ContextCollection.Keystore);
Console.WriteLine("Bot keystore updated.");
};
// 在不修改 lgr 的情況下無法直接訪問Keystore
// 所以如果你有更好的"判斷首次登錄"的辦法,請不要使用它
if (_bot.ContextCollection.Keystore.Session.D2.Length == 0)
{
var qrCode = await _bot.FetchQrCode();
if (qrCode != null)
{
QrCodeHelper.Output(_bot.ContextCollection.Keystore.Session.QrUrl!);
Console.WriteLine(_bot.ContextCollection.Keystore.Session.QrUrl!);
await _bot.LoginByQrCode();
}
}
else await _bot.LoginByPassword();
// 從這裏開始,請不要寫額外的 while(true) 或者任何讓 主線程阻塞的代碼
// 否則會造成 lgr 假死,症狀爲登錄上但收不到任何群消息
}
}
/// <summary>
/// Load or create device
/// </summary>
/// <returns></returns>
private static BotDeviceInfo? GetDevice()
{
// Read the device from config
if (File.Exists("device.json"))
{
return JsonSerializer.Deserialize
<BotDeviceInfo>(File.ReadAllText("device.json"));
}
// Create new one
var device = BotDeviceInfo.GenerateInfo();
{
var deviceJson = JsonSerializer.Serialize(device,
new JsonSerializerOptions {WriteIndented = true});
File.WriteAllText("device.json", deviceJson);
}
return device;
}
/// <summary>
/// Load or create configuration
/// </summary>
/// <returns></returns>
private static BotConfig? GetConfig()
{
// Read the device from config
if (File.Exists("config.json"))
{
return JsonSerializer.Deserialize
<BotConfig>(File.ReadAllText("config.json"));
}
// Create new one
var config = new BotConfig
{
AutoReconnect = true,
Protocol = Protocols.Linux,
};
// Write to file
var configJson = JsonSerializer.Serialize(config,
new JsonSerializerOptions {WriteIndented = true});
File.WriteAllText("config.json", configJson);
return config;
}
/// <summary>
/// Load or create keystore
/// </summary>
/// <returns></returns>
private static BotKeystore? GetKeyStore()
{
// Read the device from config
if (File.Exists("keystore.json"))
{
return JsonSerializer.Deserialize
<BotKeystore>(File.ReadAllText("keystore.json"));
}
// Create new one
Console.WriteLine("Bot created.");
return UpdateKeystore(new BotKeystore());
}
/// <summary>
/// Update keystore
/// </summary>
/// <param name="keystore"></param>
/// <returns></returns>
private static BotKeystore UpdateKeystore(BotKeystore keystore)
{
var keystoreJson = JsonSerializer.Serialize(keystore,
new JsonSerializerOptions {WriteIndented = true});
File.WriteAllText("keystore.json", keystoreJson);
return keystore;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
TheSnowfield
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@NoNameGMM 參考 kagami 的實現如下