A tool to moderate language and contents in several chat platforms.
View the live demo (or download it)!
Or go to PPT.
Developed for Cloud Computing exam at the UniversitΓ degli Studi di Salerno and for AzureChamp 2021 by:
Everyday thousands of messages are sent through different instant messaging platform. With this amount of messages, take control over the chat is so stressful for chat moderators. Users that exchange messages in unappropriate way are a problem not only for moderators, but for users too.
For the reasons explained before, we have implemented ModBot, a tool that can help moderators to manage their chats. Modbot can:
- Detect and remove hurtful messages
- Detect and remove sensible images (containing violence or pornography)
- Detect personal information exchanged in chat
- Detect message flooding
- Support multi languages and different chat platforms
- Warn and ban users who misbehave
About the ban, it isn't implemented in traditional way that kick-out users from chat, instead a banned user can still read messages exchanged in the chat, but can't interact in any other ways.
ModBot can work in all officially supported channels listed in the Channel Reference. For other platforms
In the image above, the Modbot architecture is illustrated. Modbot can receive messages from different channels which are divided into two main categories:
- Channels officially supported by the Bot Service
- Channels that aren't offically supported.
For the first one, no special intervention is required. For the second one, however, it's necessary to implement your own Direct Line mechanism, as done for discord and twitch, both hosted on App Service.
When a message coming from the channels, it's collected by the bot service that:
-
Check on CosmosDB if the user is banned. If it's true, the user somehow managed to write on the channel even though he shouldn't have. ModBot will simply delete any messages sent by this user until his ban expires.
-
The latest messages (including the one just received) are analyzed to check if the user is flooding the chat
-
The content of the message is sent to the Content Moderator who is responsible for checking that there are no offenses (direct and indirect) or that the image sent is not intended for an adult audience.
-
If misconduct is detected in 2 or 3:
- A warning message is sent in the same language in which the user wrote the message
- In the event that the user has exceeded the maximum number of warnings, a message will be sent to inform user that he has been banned.
For this project we have used severals tools provided by Azure:
- Cosmos DB: for reading / storing data about users and channels in which they talk.
- App Service: for hosting ModBot and direct lines servers.
- Azure Functions: HTTP triggered for message management and ban mechanism, Time Triggered for unban mechanism.
- Bot Service: The core of the bot
- Content Moderator: processes text and images to detect inappropriate content.
- Text Analytics: detect personal information exchanged in the chat
In this section a tutorial for the creation of all the required Azure resources is proposed. In order to maintain the cost low as much as possible will be chosen the free tier when available.
First thing first an Azure Resource Group is required, this is pretty straightforward to do using the Azure Portal and can also be done dinamically while creating the first resource. ATTENTION The selected region must be the same for all the remaining resources.
Now let's start with other tools!
Cosmos DB is used for reading / storing data about users and channels in which they talk.
- Create a new resource and using the search bar find
Azure Cosmos DB
. - Provide the details for the Subscription, Resource Group (if you don't have one, here you can create a new one), chose a name and select your favourite Location.
- For capacity mode you can choose Provisioned throughput or Serverless. We suggests the second one for best pricing.
- Leave all the others fields as default and press Review + create. When the resource is been correctly deployed go to resource.
- In the lateral menu choose 'Keys'.
- Search for the 'URI', copy it and save it in the file .env in the main folder of the project in
CosmosDbEndpoint
field. Copy it and save it also in the file local.settings.json into functions folder inCosmosDbEndpoint
field. - Search for the 'PRIMARY KEY', copy it and save it in the file .env in the main folder of the project in
CosmosDbKey
field. Copy it and save it also in the file local.settings.json into functions folder inCosmosDbKey
field. - Search for the 'PRIMARY CONNECTION STRING'. Copy it and save it also in the file local.settings.json into functions folder in
CONNECTION_STRING
field.
Web App Bot is the easiest way to deploy your Bot Service and hosting it on an App Service (used in this project for hosting ModBot and direct lines servers).
- Create a new resource and using the search bar find 'Bot'.
- From drop-down list select Web App Bot.
- Provide the details for the Subscription, Resource Group, Location and the name.
- For Pricing Tier select F0 Plan (it's free).
- As Bot Template, select Node.js as SDK Language and choose "Echo Bot" as template. This template will be replaced with our code. We explain how to do it later.
- Select an App Service Plan or create a new one if you don't have it.
- Leave all the others fields as default and press Create.
- When the resource is been correctly deployed go to resource.
- In the lateral menu choose 'Channels'.
- Enable all the channels you needed.
- Go to functions folder and set
BOT_ENDPOINT
field in this way: https://.<YOUR_BOT_NAME>.azurewebsites.net
Using custom channels Discord and Twitch at this moment aren't officially supported by Azure Bot Service. What you need is to add Direct Line as channel. Then, for both Discord and Twitch you must:
- Click on 'Edit' on the right of 'Direct Line'
- Click on 'Add New Site'
- Choose a name for your new direct line channel. (For instance: Discord or Twitch)
- Copy Secret Keys in corrispective .env file.
The fields are different for Discord and Twitch. For Discord:
- Copy Secret Keys in
DiscordDirectLineSecret
field - Remember to set
AzureBotName
field too, based on the name choosed before.
For Twitch:
- Copy Secret Keys in
TwitchDirectLineSecret
field - Remember to set
AzureBotName
field too, based on the name choosed before.
In this project, HTTP triggered Azure Functions are used for message management and ban mechanism. We use also Time Triggered Functions for unban mechanism.
You can deploy directly from VSCode using this guide
In ModBot Content Moderator service processes text and images to detect inappropriate content.
- Create a new resource and using the search bar find 'Cognitive Services'.
- Press on Create
- Search in the searchbar avaible in 'Content Moderator' in Get Started Categories
- Provide the details for the Subscription, Resource Group, Region and the name.
- For Pricing Tier you can use Free F0 for testing purpose, but we suggest Standard S0 in production
- Press Review + Create. Once the resource is ready, go to resource.
- From the left-side menu, select 'Keys and Endpoint'.
- Search for 'KEY 1', copy it and save it in the file .env in the main folder of the project in
ContentModeratorKey
field. - Search for 'Endpoint', copy it and save it in the file .env in the main folder of the project in
ContentModeratorEndpoint
field.
In ModBot, Text Analytics is used to detect personal information exchanged in the chat.
- Create a new resource and using the search bar find 'Cognitive Services'.
- Press on Create
- Search 'Text Analytics' in the searchbar avaible in Get Started Categories. Select the 'Text Analytics' provided by Microsoft.
- Provide the details for the Subscription, Resource Group, Region and the name.
- For Pricing Tier you can use Free F0.
- Press Review + Create. Once the resource is ready, go to resource.
- From the left-side menu, select 'Keys and Endpoint'.
- Search for 'KEY 1', copy it and save it in the file .env in the main folder of the project in
TextAnalyticsKey
field. - Search for 'Endpoint', copy it and save it in the file .env in the main folder of the project in
TextAnalyticsEndpoint
field.
-
Node.js version 10.14.1 or higher
# determine node version node --version
-
Azure CLI version 2.18.0 or higher. You can install it from here
Make sure you have setted all enviroment variables in .env
file.
Then install modules
npm install
β οΈ WARNING! Image detection mechanism does not work in local enviroment.β οΈ
Bot Framework Emulator is a desktop application that allows bot developers to test and debug their bots on localhost or running remotely through a tunnel.
- Install the Bot Framework Emulator version 4.9.0 or greater from here
-
Start the bot
npm start
-
Launch Bot Framework Emulator
-
File -> Open Bot
-
Enter a Bot URL of
http://localhost:3978/api/messages
-
Enjoy!
In this section we learn how to deploy the bot on azure.
Before you start with deploying, make sure that you have started the services listed into π§ Azure Tools section.
Before you deploy the bot, you must generate web.config
file.
az bot prepare-deploy --code-dir "<PATH_TO_THIS_PROJECT>" --lang Javascript
In alternative, you can use the web.config
file provided here.
There are several ways to deploy but we'll focus on manual deploy. Make a .zip file containing:
π node_modules
π services
π .env
π .eslintrc.js
π bot.js
π index.js
π locales.js
π web.config
Now go on https://YOUR_BOT_SERVICE_NAME.scm.azurewebsites.net/ZipDeployUI
and:
- delete all files listed there
- Drag and drop your zip file
- Wait (up to 10 minutes)
πTADA!π Your bot is ready!
To learn more about deploying a bot to Azure, see Deploy your bot to Azure for a complete list of deployment instructions.
First of all you must go to direct lines folder. For example
cd '.\direct lines\Discord'
Then install dependencies
npm install
Finally, set all enviroment variables in .env
file.
Now, you can create a .zip file containing:
π node_modules
π .env
π discord-direct-line.js
Deploy this file in the app service of the bot as a webjob or deploy as a different app service.