Developing bot with .net core and AWS SQS

How to develop bot with .net core and AWS SQS, What is worker Service in .net core


Category: .Net Core 3 Tags: C#, AWS

Bot Code Files

Prerequisites:

  1. AWS account
  2. 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:

  1. Running a nightly job to clean up the database
  2. Sending bulk emails to customers
  3. Dumping data from one server/datasource to another
  4. Calculating complex equations
  5. 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”

New worker service in visual studio

 

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”

New worker service project


Below files will be created in under project

Worker service project files

 

 

 

 

Step 2: Create IAM user in AWS

    In aws console go to IAM->Users->Add User. Give name “SampleUser” and check Programmatic access.

Create new user in AWS


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

Add policies to user in AWS

 

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.

New user created AWS

 

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”.

Create new SQS queue in AWS


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

SQS queue in AWS

 

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.

.net core wroker service running

 

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!”.

Put message in SQS

 

You could see message on debug console:

New message received from SQS

 

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);

Bots can be scheduled using windows scheduler in windows platform or as a cron job in linux platform to perform an operation on regular basis.


Like 0 People
Last modified about 12 days ago
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 135
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x