Skip to content

Commit

Permalink
Restrict robot to specific chat (#7)
Browse files Browse the repository at this point in the history
* Add devcontainer

* Restrict bot to chat. Closes #6

* Added null value ignoring
  • Loading branch information
mattiascibien authored Jul 31, 2019
1 parent 35340f5 commit 0eab96e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
2 changes: 2 additions & 0 deletions Config/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public class BotConfig
public List<string> Responses { get; set; }

public List<string> Keywords { get; set; }

public long? ChatId { get; set; }
}
}
13 changes: 12 additions & 1 deletion ImgBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"))
{
Expand Down Expand Up @@ -97,6 +105,9 @@ public void Run()
{
_bot.StartReceiving(Array.Empty<UpdateType>());
Console.WriteLine($"@{_me.Username}: started");

if(_config.ChatId != null)
Console.WriteLine($"@{_me.Username}: restricted to chat {_config.ChatId}");
}

public void Stop()
Expand Down
3 changes: 2 additions & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ private static void CreateAndRunBot(string config)
var cfg = JsonConvert.DeserializeObject<RootConfig>(contents,
new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
ContractResolver = new CamelCasePropertyNamesContractResolver(),
NullValueHandling = NullValueHandling.Ignore
});

Assert.Check(cfg != null, "Root configuration must be set");
Expand Down

0 comments on commit 0eab96e

Please sign in to comment.