121 lines
5.0 KiB
C#
121 lines
5.0 KiB
C#
using System.Net;
|
|
using System;
|
|
using Telegram.Bot;
|
|
using Telegram.Bot.Exceptions;
|
|
using Telegram.Bot.Polling;
|
|
using Telegram.Bot.Types;
|
|
using Telegram.Bot.Types.Enums;
|
|
using LC_Bot;
|
|
using LC_Bot.Models;
|
|
using Telegram.Bot.Types.ReplyMarkups;
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
using System.Reflection.Metadata;
|
|
|
|
internal class Program
|
|
{
|
|
//public static string token = "6964910806:AAH5EuJq9vNm0D2KwvcEnmuvPYd7_sYa5aI"; //Новый
|
|
public static string token = "7055214362:AAFL1aroXt1S00pACH6jMs7OiNQtmK8pI_Y"; //Старый
|
|
public static TelegramBotClient botClient = null;
|
|
public static string txt;
|
|
private static async Task Main(string[] args)
|
|
{
|
|
using Db db = new Db();
|
|
botClient = new TelegramBotClient(token);
|
|
using CancellationTokenSource cts = new();
|
|
ReceiverOptions receiverOptions = new()
|
|
{
|
|
AllowedUpdates = Array.Empty<UpdateType>() // receive all update types except ChatMember related updates
|
|
};
|
|
botClient.StartReceiving(updateHandler: HandleUpdateAsync, pollingErrorHandler: HandlePollingErrorAsync, receiverOptions: receiverOptions, cancellationToken: cts.Token);
|
|
var me = await botClient.GetMeAsync();
|
|
Console.WriteLine($"Start listening for @{me.Username}");
|
|
Console.ReadLine();
|
|
cts.Cancel();
|
|
}
|
|
private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
|
{
|
|
long chatId = 0;
|
|
|
|
if (update.Type == UpdateType.Message)
|
|
{
|
|
chatId = update.Message.Chat.Id;
|
|
var msg = update.Message;
|
|
if (msg.Type == MessageType.Text)
|
|
{
|
|
var textMsg = msg.Text.Split("@")[0];
|
|
Console.WriteLine($"Received a '{textMsg}' message in chat {chatId}.");
|
|
|
|
if (textMsg == "/start")
|
|
{
|
|
await botClient.SendTextMessageAsync(
|
|
chatId: chatId,
|
|
text: $"Привет, {msg.From.FirstName}!\r\nБот работает исправно!");
|
|
return;
|
|
}
|
|
|
|
if (textMsg == "/test")
|
|
{
|
|
string ip = "172.24.12.10";
|
|
string name = "ТК83";
|
|
txt = "<b>‼ НЕИСПРАВНОСТЬ ‼</b>\n" +
|
|
$"----------------------------------------------\n" +
|
|
$"<b>Объект:</b> SPB\n" +
|
|
$"<b>Устройство:</b> TEST\n" +
|
|
$"<b>Имя:</b> {name}\n" +
|
|
$"<b>Адрес:</b> {ip}\n" +
|
|
$"<b>Состояние:</b> 🔴 - не доступна\n" +
|
|
$"----------------------------------------------";
|
|
|
|
InlineKeyboardMarkup inlineKeyboard = new(new[]
|
|
{
|
|
new []
|
|
{
|
|
InlineKeyboardButton.WithCallbackData(text: "Перезагрузить", callbackData: $"reboot|{ip}")
|
|
}
|
|
});
|
|
var msgId = botClient.SendTextMessageAsync(chatId: 6882856105, parseMode: ParseMode.Html,replyMarkup:inlineKeyboard, text: txt).Result.MessageId;
|
|
Console.WriteLine(msgId);
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
else if (update.Type == UpdateType.CallbackQuery)
|
|
{
|
|
chatId = update.CallbackQuery.Message.Chat.Id;
|
|
var msgCB = update.CallbackQuery;
|
|
txt = msgCB.Message.Text;
|
|
Console.WriteLine($"Received a '{update.CallbackQuery.Data}' message in chat {chatId}.");
|
|
|
|
if (msgCB.Data.StartsWith("reboot"))
|
|
{
|
|
var m = msgCB.Data.Split("|");
|
|
var reb = new Reboot(m[1]);
|
|
if (reb.Start())
|
|
{
|
|
txt.Replace("<b>🔄 ПЕРЕЗАГРУЗКА 🔄</b>\n", "<b>🔄✅ ПЕРЕЗАГРУЖЕН ✅🔄</b>\n");
|
|
botClient.EditMessageTextAsync(chatId,msgCB.Message.MessageId, txt, parseMode: ParseMode.Html);
|
|
}
|
|
else
|
|
{
|
|
txt.Replace("<b>🔄 ПЕРЕЗАГРУЗКА 🔄</b>\n", "<b>‼️‼️ ОШИБКА БОТА ‼️‼️</b>\n");
|
|
botClient.EditMessageTextAsync(chatId, msgCB.Message.MessageId, txt, parseMode: ParseMode.Html);
|
|
}
|
|
}
|
|
}
|
|
else return;
|
|
}
|
|
|
|
private static Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
|
|
{
|
|
var ErrorMessage = exception switch
|
|
{
|
|
ApiRequestException apiRequestException
|
|
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
|
|
_ => exception.ToString()
|
|
};
|
|
|
|
//Console.WriteLine(ErrorMessage);
|
|
return Task.CompletedTask;
|
|
}
|
|
} |