From 0eab96e27ab12833c81adf09533a5f19209bcc5f Mon Sep 17 00:00:00 2001 From: Mattias Cibien Date: Wed, 31 Jul 2019 11:17:07 +0200 Subject: [PATCH] Restrict robot to specific chat (#7) * Add devcontainer * Restrict bot to chat. Closes #6 * Added null value ignoring --- .devcontainer/Dockerfile | 24 ++++++++++++++++++++++++ .devcontainer/devcontainer.json | 18 ++++++++++++++++++ Config/BotConfig.cs | 2 ++ ImgBot.cs | 13 ++++++++++++- Program.cs | 3 ++- 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..e6db3c0 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,24 @@ +#------------------------------------------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information. +#------------------------------------------------------------------------------------------------------------- + +FROM mcr.microsoft.com/dotnet/core/sdk:2.2 + +# Avoid warnings by switching to noninteractive +ENV DEBIAN_FRONTEND=noninteractive + +# Configure apt and install packages +RUN apt-get update \ + && apt-get -y install --no-install-recommends apt-utils 2>&1 \ + # + # Verify git, process tools, lsb-release (common in install instructions for CLIs) installed + && apt-get -y install git procps lsb-release \ + # + # Clean up + && apt-get autoremove -y \ + && apt-get clean -y \ + && rm -rf /var/lib/apt/lists/* + +# Switch back to dialog for any ad-hoc use of apt-get +ENV DEBIAN_FRONTEND=dialog \ No newline at end of file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..191d39d --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,18 @@ +// See https://aka.ms/vscode-remote/devcontainer.json for format details. +{ + "name": "C# (.NET Core 2.2)", + "dockerFile": "Dockerfile", + + // Uncomment the next line if you want to publish any ports. + // "appPort": [], + + // Uncomment the next line if you want to add in default container specific settings.json values + // "settings": { "workbench.colorTheme": "Quiet Light" }, + + // Uncomment the next line to run commands after the container is created. + // "postCreateCommand": "dotnet restore" + + "extensions": [ + "ms-vscode.csharp" + ] +} \ No newline at end of file diff --git a/Config/BotConfig.cs b/Config/BotConfig.cs index 09e3d9f..119e093 100644 --- a/Config/BotConfig.cs +++ b/Config/BotConfig.cs @@ -12,5 +12,7 @@ public class BotConfig public List Responses { get; set; } public List Keywords { get; set; } + + public long? ChatId { get; set; } } } \ No newline at end of file diff --git a/ImgBot.cs b/ImgBot.cs index 597fb45..8aeb155 100644 --- a/ImgBot.cs +++ b/ImgBot.cs @@ -58,7 +58,15 @@ private async void BotOnMessageReceived(object sender, MessageEventArgs e) { var message = e.Message; - var isPrivate = e.Message.Chat.Type == ChatType.Private; + // restrict the robot to a specific chat + if(_config.ChatId != null && message.Chat.Id != _config.ChatId.Value) + { + await _bot.SendTextMessageAsync(message.Chat.Id, + "This chat is not authorized to use this robot"); + return; + } + + var isPrivate = message.Chat.Type == ChatType.Private; if (isPrivate || message.Text.Contains($"@{_me.Username}")) { @@ -97,6 +105,9 @@ public void Run() { _bot.StartReceiving(Array.Empty()); Console.WriteLine($"@{_me.Username}: started"); + + if(_config.ChatId != null) + Console.WriteLine($"@{_me.Username}: restricted to chat {_config.ChatId}"); } public void Stop() diff --git a/Program.cs b/Program.cs index 219c5a6..4c1b3ae 100644 --- a/Program.cs +++ b/Program.cs @@ -61,7 +61,8 @@ private static void CreateAndRunBot(string config) var cfg = JsonConvert.DeserializeObject(contents, new JsonSerializerSettings() { - ContractResolver = new CamelCasePropertyNamesContractResolver() + ContractResolver = new CamelCasePropertyNamesContractResolver(), + NullValueHandling = NullValueHandling.Ignore }); Assert.Check(cfg != null, "Root configuration must be set");