Ловим exceptions, если случились - рестартуем бота
This commit is contained in:
+30
-1
@@ -24,20 +24,41 @@ namespace ZaoBot
|
|||||||
const string CMD_LEFT_GATE = "Открой левые ворота";
|
const string CMD_LEFT_GATE = "Открой левые ворота";
|
||||||
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
||||||
|
|
||||||
|
static bool shouldRestart = false;
|
||||||
|
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
|
||||||
config = LoadConfig();
|
config = LoadConfig();
|
||||||
var token = config.RootElement.GetProperty("token").GetString();
|
var token = config.RootElement.GetProperty("token").GetString();
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
shouldRestart = false;
|
||||||
botClient = new TelegramBotClient(token);
|
botClient = new TelegramBotClient(token);
|
||||||
var me = botClient.GetMeAsync().Result;
|
var me = botClient.GetMeAsync().Result;
|
||||||
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
|
Console.WriteLine($"Hello, World! I am user {me.Id} and my name is {me.FirstName}.");
|
||||||
botClient.OnMessage += OnBotMessage;
|
botClient.OnMessage += OnBotMessage;
|
||||||
botClient.StartReceiving();
|
botClient.StartReceiving();
|
||||||
while (true) Thread.Sleep(10000);
|
while (!shouldRestart)
|
||||||
|
{
|
||||||
|
Thread.Sleep(1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// Тут ловим всё, при ошибках просто заново поднимаем бота
|
||||||
|
Console.WriteLine("Exception: " + e.Message);
|
||||||
|
Console.WriteLine(e.StackTrace);
|
||||||
|
Console.WriteLine("Restarting bot");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async void OnBotMessage(object sender, MessageEventArgs e)
|
private static async void OnBotMessage(object sender, MessageEventArgs e)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
if (e.Message.Text != null)
|
if (e.Message.Text != null)
|
||||||
{
|
{
|
||||||
@@ -90,6 +111,14 @@ namespace ZaoBot
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Exception: " + ex.Message);
|
||||||
|
Console.WriteLine(ex.StackTrace);
|
||||||
|
Console.WriteLine("Restarting bot");
|
||||||
|
shouldRestart = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static async Task<string> ShowWeather()
|
private static async Task<string> ShowWeather()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user