Add laundry notifications to Telegram

This commit is contained in:
2022-08-18 09:55:31 -04:00
parent e4944ea5d3
commit 970f4ba59f
7 changed files with 73 additions and 7 deletions

View File

@@ -0,0 +1,40 @@
using RestSharp;
namespace Service;
public class LaundryMonitor
{
private readonly RestClient _restClient = new();
private readonly string _botToken;
private readonly string _chatId;
public LaundryMonitor(IConfiguration configuration)
{
_botToken = configuration["Telegram:BotToken"];
_chatId = configuration["Telegram:ChatId"];
}
public async Task HandleDeviceMessage(Device device)
{
try
{
if (device.Name is not ("washer" or "dryer"))
return;
var status = device.Status ? "ON" : "OFF";
var message = $"The {device.Name} is now {status}.";
var encodedMessage = Uri.EscapeDataString(message);
var restRequest = new RestRequest($"https://api.telegram.org/bot{_botToken}/sendMessage?chat_id={_chatId}&text={encodedMessage}");
await _restClient.GetAsync(restRequest);
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}