This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
using LC_Bot.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LC_Bot;
|
||||
|
||||
public partial class Db : DbContext
|
||||
{
|
||||
public virtual DbSet<Cam> Cams { get; set; }
|
||||
public virtual DbSet<Error> Errors { get; set; }
|
||||
public Db() => Database.EnsureCreated();
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
#if DEBUG
|
||||
optionsBuilder.UseSqlite("Data Source=Error.db");
|
||||
#else
|
||||
optionsBuilder.UseSqlite(@"Data Source=C:\Users\mastankov\Desktop\ForMonitoring\Soft\LC_Api\Error.db");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
OnModelCreatingPartial(modelBuilder);
|
||||
}
|
||||
|
||||
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 253 KiB |
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<ApplicationIcon>LCS_logo.ico</ApplicationIcon>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="LCS_logo.ico" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.6" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
|
||||
<PackageReference Include="Selenium.WebDriver" Version="4.21.0" />
|
||||
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,21 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LC_Bot.Models
|
||||
{
|
||||
public class Cam
|
||||
{
|
||||
[Key]
|
||||
public string? Id { get; set; }
|
||||
public string? Type { get; set; }
|
||||
public string? Ip { get; set; }
|
||||
public string? Access { get; set; }
|
||||
public string? ImgUrl { get; set; }
|
||||
public string? RecPoint { get; set; }
|
||||
public string? ChannelRec { get; set; }
|
||||
public string? SwitchConnect { get; set; }
|
||||
public string? SwitchPort { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Map { get; set; }
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LC_Bot.Models
|
||||
{
|
||||
public class Error
|
||||
{
|
||||
[Key]
|
||||
public Guid Id { get; set; }
|
||||
public string? TypeDev { get; set; }
|
||||
public int MsgId { get; set; }
|
||||
public string? Status { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? Ip { get; set; }
|
||||
public string? Map { get; set; }
|
||||
public DateTime? DataError { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
using OpenQA.Selenium;
|
||||
using OpenQA.Selenium.Edge;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace LC_Bot
|
||||
{
|
||||
internal class Reboot
|
||||
{
|
||||
private readonly string _ip;
|
||||
private int _id;
|
||||
|
||||
EdgeDriver driver;
|
||||
|
||||
public Reboot(string ip)
|
||||
{
|
||||
_ip = ip;
|
||||
var options = new EdgeOptions();
|
||||
//options.AddArgument("-headless");
|
||||
var service = EdgeDriverService.CreateDefaultService();
|
||||
service.HideCommandPromptWindow = true;
|
||||
driver = new EdgeDriver(options);
|
||||
driver.Manage().Window.Minimize();
|
||||
}
|
||||
public bool Start()
|
||||
{
|
||||
using Db db = new Db();
|
||||
var o = db.Cams.FirstOrDefault(c => c.Ip == _ip);
|
||||
_id = db.Errors.Where(c => c.Ip == _ip && c.Status == "off").First().MsgId;
|
||||
if (o != null)
|
||||
{
|
||||
int port = Convert.ToInt32(o.SwitchPort);
|
||||
driver.Navigate().GoToUrl($"http://{o.SwitchConnect}/doc/page/login.asp");
|
||||
Thread.Sleep(500);
|
||||
try
|
||||
{
|
||||
var retext = Program.txt.Replace("<b>‼ НЕИСПРАВНОСТЬ ‼</b>\n", "<b>🔄 ПЕРЕЗАГРУЗКА 🔄</b>\n");
|
||||
Program.botClient.EditMessageTextAsync(6882856105, _id, retext, parseMode: Telegram.Bot.Types.Enums.ParseMode.Html);
|
||||
var ch = driver.FindElement(By.Id("details-button"));
|
||||
if (ch != null)
|
||||
{
|
||||
driver.FindElement(By.Id("details-button")).Click();
|
||||
driver.FindElement(By.Id("proceed-link")).Click();
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
driver.FindElement(By.Id("username")).SendKeys("admin");
|
||||
driver.FindElement(By.Id("password")).SendKeys("4NUDZhJ7");
|
||||
driver.FindElement(By.XPath("//button[@type='button']")).Click();
|
||||
Thread.Sleep(500);
|
||||
driver.Navigate().GoToUrl($"https://{o.SwitchConnect}/doc/page/switchConfig.asp");
|
||||
Thread.Sleep(1000);
|
||||
var ofnik = driver.FindElement(By.XPath($"//div[contains(@ng-click,'changeSwitch({port - 1}, 4, 4)')]"));
|
||||
ofnik.Click();
|
||||
Thread.Sleep(1000);
|
||||
driver.FindElement(By.XPath("//button[contains(@class,'btn-save')]")).Click();
|
||||
Thread.Sleep(1000);
|
||||
ofnik.Click();
|
||||
Thread.Sleep(1000);
|
||||
driver.FindElement(By.XPath("//button[contains(@class,'btn-save')]")).Click();
|
||||
Thread.Sleep(500);
|
||||
driver.Quit();
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user