Prerequisites:
- AWS account
- Visual studio with .net core 3.0 installed
Introduction
Bot is an application which performs automated task, bots usually developed to perform a repetitive unit of work which can be scheduled over period of time or performed on demand. Bots work is to perform job asynchronously so we can forget after triggering it, bots may take long time to perform a job and we do not want real-time updates on job so we make them asynchronous. Examples of bots:
- Running a nightly job to clean up the database
- Sending bulk emails to customers
- Dumping data from one server/datasource to another
- Calculating complex equations
- Processing large amount of data in odd hours
In this article we will create simple bot using asp.net core and aws SQS(simple queue service). We will keep the jobs queued in SQS and a .net core process to poll the queue.
Implementation
Step 1: Create a .net core project
Choose project template ”Worker Service”

Worker Service: Worker service is a new project template added in .net core, worker service is similar to windows service which is a always running process in operating system. This project is ideal for developing bots because we want the bot to be running 24*7.
Will name the project “SampleBot”

Below files will be created in under project

Step 2: Create IAM user in AWS
In aws console go to IAM->Users->Add User. Give name “SampleUser” and check Programmatic access.

Click next to add permissions, click on tab “Attach Existing Policies Directly” and add policies as per below image.

I have given full access for simplicity. Click on next to add tags, skip tags and click next to review. Click on Create User and user with access key id and secret key id will be displayed.

Note: Write down access key id and secret key id for future use, once you close the window you will not be able to see the keys again.
Step 3: Create SQS queue in AWS
Create queue with name “SampleSQS”.

Click "Quick Create Queue". And the queue will be created.

Write down the queue url.
Step 4: Setting up the project:
Go to nuget package manager of the project and search for “AWSSDK.SQS” and install it.
Now add below section in app.config file:
"Aws": { "AccessKey": "****", "SecretKey": "****", "SQSUrl": "****" }
Replace **** with keys noted while creating user and the url with queue url.
We have two class files Program.cs contains main method which is entry point of application and Worker.cs which has bot’s handlers. Modify Worker class as following.
public class Worker : BackgroundService { private readonly ILogger<Worker> _logger; private readonly IConfiguration _configuration; public Worker(ILogger<Worker> logger, IConfiguration configuration) { _logger = logger; _configuration = configuration; } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now); //Reading configuration var aswSection = _configuration.GetSection("Aws"); var accessKey = aswSection.GetSection("AccessKey").Value; var secretKey = aswSection.GetSection("SecretKey").Value; var sqsUrl = aswSection.GetSection("SQSUrl").Value; //Creating sqs client var credentials = new Amazon.Runtime.BasicAWSCredentials(accessKey, secretKey); AmazonSQSClient amazonSQSClient = new AmazonSQSClient(credentials, Amazon.RegionEndpoint.USEast2); //Receive request ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(sqsUrl); var response = await amazonSQSClient.ReceiveMessageAsync(receiveMessageRequest, stoppingToken); if (response.Messages.Any()) { foreach (Message message in response.Messages) { Console.WriteLine($"Message received"); Console.WriteLine($"Message: {message.Body}"); //Deleting message var deleteMessageRequest = new DeleteMessageRequest(sqsUrl, message.ReceiptHandle); await amazonSQSClient.DeleteMessageAsync(deleteMessageRequest, stoppingToken); Console.WriteLine($"Message deleted"); } } await Task.Delay(5000, stoppingToken); } } }
ExecuteAsync method is used to run the job in infinite loop. Two more methods StartAsync and StopAsync can be overridden from base class BackgroundService but not required in our case.
Now run the project from visual studio, the command prompt will show loop is running in each 5 second. But there is no message to display.

Step 5: Put message on queue
Go to SQS queue on aws management console, right click on SampleSQS and click on Send message. Send a message “Hi!”.

You could see message on debug console:

So this was the simple bot created, Instead of using aws management console to put message in queue we can create a program or producer to insert messages in queue using below snippet.
var credentials = new Amazon.Runtime.BasicAWSCredentials(accessKey, secretKey); AmazonSQSClient amazonSQSClient = new AmazonSQSClient(credentials, Amazon.RegionEndpoint.USEast2); SendMessageRequest sendMessageRequest = new SendMessageRequest(); sendMessageRequest.QueueUrl = sqsUrl; sendMessageRequest.MessageBody = "{YOUR_QUEUE_MESSAGE}"; SendMessageResponse sendMessageResponse = AmazonSQSClient.SendMessage(sendMessageRequest);
Comments:
Great Article..Thanks for posting.
thanks manju