Added cameras menu: one can get image from camera
This commit is contained in:
@@ -0,0 +1,111 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Net;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Args;
|
||||||
|
using Telegram.Bot.Types.InputFiles;
|
||||||
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
using ZaoBot.OpenHab;
|
||||||
|
|
||||||
|
namespace ZaoBot
|
||||||
|
{
|
||||||
|
class CameraConfigData
|
||||||
|
{
|
||||||
|
public string name;
|
||||||
|
public string url;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CamerasMenuUserState : IUserState
|
||||||
|
{
|
||||||
|
public const string CMD_BACK = "Назад";
|
||||||
|
|
||||||
|
ITelegramBotClient botClient;
|
||||||
|
private JsonDocument config;
|
||||||
|
List<CameraConfigData> cameras;
|
||||||
|
OpenHabClient openhab;
|
||||||
|
|
||||||
|
public CamerasMenuUserState(ITelegramBotClient botClient, JsonDocument config, OpenHabClient openhab)
|
||||||
|
{
|
||||||
|
this.botClient = botClient;
|
||||||
|
this.config = config;
|
||||||
|
this.openhab = openhab;
|
||||||
|
cameras = new List<CameraConfigData>();
|
||||||
|
foreach (var element in config.RootElement.GetProperty("cameras").EnumerateObject())
|
||||||
|
{
|
||||||
|
var cam = new CameraConfigData()
|
||||||
|
{
|
||||||
|
name = element.Name,
|
||||||
|
url = element.Value.GetString(),
|
||||||
|
};
|
||||||
|
cameras.Add(cam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IUserState> ProcessMessage(IUserState previous, MessageEventArgs e)
|
||||||
|
{
|
||||||
|
if (e.Message.Text == CMD_BACK)
|
||||||
|
{
|
||||||
|
var newState = new MainMenuUserState(botClient, config, openhab);
|
||||||
|
await newState.ProcessMessage(this, e);
|
||||||
|
return newState;
|
||||||
|
}
|
||||||
|
var response = "Выберите камеру";
|
||||||
|
|
||||||
|
foreach (var cam in cameras)
|
||||||
|
{
|
||||||
|
if (e.Message.Text == cam.name)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
SendCameraImage(e.Message.Chat, cam);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await botClient.SendTextMessageAsync(chatId: e.Message.Chat,
|
||||||
|
text: "Error: " + ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var buttons = new KeyboardButton[cameras.Count + 1][];
|
||||||
|
for (int i = 0; i < cameras.Count; i++)
|
||||||
|
{
|
||||||
|
buttons[i] = new KeyboardButton[] { cameras[i].name };
|
||||||
|
}
|
||||||
|
buttons[cameras.Count] = new KeyboardButton[] { CMD_BACK };
|
||||||
|
|
||||||
|
var replyKeyboardMarkup = new ReplyKeyboardMarkup(
|
||||||
|
buttons,
|
||||||
|
resizeKeyboard: true
|
||||||
|
);
|
||||||
|
|
||||||
|
await botClient.SendTextMessageAsync(
|
||||||
|
chatId: e.Message.Chat,
|
||||||
|
text: response,
|
||||||
|
replyMarkup: replyKeyboardMarkup
|
||||||
|
);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async void SendCameraImage(Telegram.Bot.Types.Chat chat, CameraConfigData cam)
|
||||||
|
{
|
||||||
|
using (WebClient client = new WebClient())
|
||||||
|
{
|
||||||
|
await client.DownloadFileTaskAsync(
|
||||||
|
new System.Uri(cam.url),
|
||||||
|
"temp.jpg"
|
||||||
|
);
|
||||||
|
using (Stream stream = File.OpenRead("temp.jpg"))
|
||||||
|
{
|
||||||
|
await botClient.SendDocumentAsync(
|
||||||
|
chatId: chat,
|
||||||
|
document: new InputOnlineFile(content: stream, fileName: cam.name + ".jpg")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ namespace ZaoBot
|
|||||||
const string CMD_STATUS = "Статус";
|
const string CMD_STATUS = "Статус";
|
||||||
const string CMD_LEFT_GATE = "Открой левые ворота";
|
const string CMD_LEFT_GATE = "Открой левые ворота";
|
||||||
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
const string CMD_RIGHT_GATE = "Открой правые ворота";
|
||||||
|
const string CMD_CAMERAS = "Камеры";
|
||||||
|
|
||||||
ITelegramBotClient botClient;
|
ITelegramBotClient botClient;
|
||||||
private JsonDocument config;
|
private JsonDocument config;
|
||||||
@@ -64,6 +65,12 @@ namespace ZaoBot
|
|||||||
await newState.ProcessMessage(this, e);
|
await newState.ProcessMessage(this, e);
|
||||||
return newState;
|
return newState;
|
||||||
}
|
}
|
||||||
|
else if (e.Message.Text == CMD_CAMERAS)
|
||||||
|
{
|
||||||
|
var newState = new CamerasMenuUserState(botClient, config, openhab);
|
||||||
|
await newState.ProcessMessage(this, e);
|
||||||
|
return newState;
|
||||||
|
}
|
||||||
else if (e.Message.Text == "")
|
else if (e.Message.Text == "")
|
||||||
{
|
{
|
||||||
response = "Превед!";
|
response = "Превед!";
|
||||||
@@ -81,6 +88,7 @@ namespace ZaoBot
|
|||||||
new KeyboardButton[] { CMD_WEATHER },
|
new KeyboardButton[] { CMD_WEATHER },
|
||||||
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
|
new KeyboardButton[] { CMD_LEFT_GATE, CMD_RIGHT_GATE },
|
||||||
new KeyboardButton[] { CMD_STATUS },
|
new KeyboardButton[] { CMD_STATUS },
|
||||||
|
new KeyboardButton[] { CMD_CAMERAS },
|
||||||
},
|
},
|
||||||
resizeKeyboard: true
|
resizeKeyboard: true
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Net.Http.Headers;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -10,11 +11,17 @@ namespace ZaoBot.OpenHab
|
|||||||
class OpenHabClient
|
class OpenHabClient
|
||||||
{
|
{
|
||||||
private string url;
|
private string url;
|
||||||
|
private string token;
|
||||||
HttpClient http = new HttpClient();
|
HttpClient http = new HttpClient();
|
||||||
|
|
||||||
public OpenHabClient(string url)
|
public OpenHabClient(string url, string token = "")
|
||||||
{
|
{
|
||||||
this.url = url;
|
this.url = url;
|
||||||
|
this.token = token;
|
||||||
|
if (!string.IsNullOrEmpty(token))
|
||||||
|
{
|
||||||
|
http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", token + ":");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ItemState> GetItemState(string item)
|
public async Task<ItemState> GetItemState(string item)
|
||||||
|
|||||||
Reference in New Issue
Block a user