public static class Bot { private static Api _bot; /// <summary> /// , /// - /// /// </summary> public static Api Get() { if (_bot != null) return _bot; _bot = new Api(Config.BotApiKey); _bot.SetWebhook(Config.WebHookUrl); return _bot; } }
public static class Config { /// <summary> /// /// </summary> private static readonly NameValueCollection Appsettings = ConfigurationManager.AppSettings; /// <summary> /// /// </summary> public static string BotApiKey { get { return Appsettings["BotApiKey"]; } } /// <summary> /// URL, /// </summary> public static string WebHookUrl { get { return Appsettings["WebHookUrl"]; } } }
/// <summary> /// "" /// </summary> public static class WakeOnLan { public static void Up(string ip, string mac, int? port = null) { var client = new UdpClient(); var data = new byte[102]; for (var i = 0; i <= 5; i++) // - data[i] = 0xff; var macDigits = GetMacDigits(mac); if (macDigits.Length != 6) throw new ArgumentException("Incorrect MAC address supplied!"); const int start = 6; for (var i = 0; i < 16; i++) // for (var x = 0; x < 6; x++) data[start + i * 6 + x] = (byte)Convert.ToInt32(macDigits[x], 16); client.Send(data, data.Length, ip, port ?? 7); // } private static string[] GetMacDigits(string mac) // MAC { return mac.Split(mac.Contains("-") ? '-' : ':'); } public static bool ValidateMac(string mac) // MAC { return GetMacDigits(mac).Length == 6; } }
/wol 1.2.3.4 01:02:03:04:05:06 7
public async void Handle(Message message) { var text = message.Text.Split(' '); if (text.First() != "/wol") return; switch (text.Count()) { case 1: case 2: await _bot.SendTextMessage(message.Chat.Id, " : /wol 1.2.3.4 01:02:03:04:05:06 7"); break; default: if (!WakeOnLan.ValidateMac(text[2])) await _bot.SendTextMessage(message.Chat.Id, " MAC "); else { try { WakeOnLan.Up(text[1], text[2], GetPort(text)); await _bot.SendTextMessage(message.Chat.Id, " !"); } catch (Exception) { await _bot.SendTextMessage(message.Chat.Id, " :("); } } break; } } /// <summary> /// /// </summary> private static int? GetPort(IReadOnlyList<string> text) { int port; if (text.Count == 4 && int.TryParse(text[3], out port)) return port; return null; }
public class MessageController : ApiController { [Route(@"api/message/wol")] public OkResult Post([FromBody]Update value) { Task.Run(() => new Handler().Handle(value.Message)); return Ok(); } }
Source: https://habr.com/ru/post/265305/
All Articles