DEV Community

Cover image for Intelligent Automation with RPA and AI: Capturing and Summarizing Emails with Selenium and ChatGPT
Alisson Podgurski
Alisson Podgurski

Posted on

Intelligent Automation with RPA and AI: Capturing and Summarizing Emails with Selenium and ChatGPT

Have you ever imagined a robot doing the repetitive task of checking your emails, generating summaries, and automatically highlighting pending tasks? Well, that reality is not far off. With the power of Robotic Process Automation (RPA) combined with the artificial intelligence of ChatGPT, you can create systems that save time and energy on daily tasks, allowing you to focus on what really matters.

In this article, we will explore how to build an intelligent RPA system that automates the capture and summary of emails using Selenium and the OpenAI API. This tutorial will not only show you how to put the theory into practice, but will also serve as inspiration for you to apply automation and AI in other areas of your work and life.

What We Will Build
The goal is simple: create a robot capable of:

  1. Automatically accessing your email account (Gmail).
  2. Capturing the most recent emails directly from your inbox.
  3. Sending these emails to ChatGPT to generate a summary of important points and list any pending tasks.
  4. Saving the summary to a text file for quick reference.

With these tools, you will have a powerful and intelligent automation system that does the heavy lifting for you. Let's get started!

1. Creating the .NET Project

First, let's create a project in .NET. .NET provides a solid and rich foundation for developing robust applications, while Selenium allows us to control the browser and access emails, and ChatGPT will bring intelligence to automatically summarize the content of emails.

Steps:

  1. Open the terminal or command prompt and create a new console project:
dotnet new console -n EmailRPA
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to the project folder:
cd EmailRPA
Enter fullscreen mode Exit fullscreen mode
  1. Add the necessary libraries, such as Selenium WebDriver and OpenAI:
dotnet add package Selenium.WebDriver
dotnet add package Selenium.Chrome.WebDriver
dotnet add package OpenAI
Enter fullscreen mode Exit fullscreen mode

With these initial configurations, we are ready to start implementing our email automation system!

2. Structuring the Automation System

To keep the code clean and organized, we will follow some best practices and divide the functionality into specific classes. This not only improves code maintainability but also makes it easier to expand and integrate with other systems in the future.

Project Structure:

EmailRPA/

├── Program.cs
├── Services/
│ ├── EmailLoginService.cs
│ ├── EmailCaptureService.cs
│ ├── EmailSummaryService.cs
│ └── FileStorageService.cs
├── Models/
│ └── Email.cs

  • EmailLoginService: Logs into Gmail.
  • EmailCaptureService: Captures emails from the inbox.
  • EmailSummaryService: Sends emails to ChatGPT to generate summaries.
  • FileStorageService: Saves the generated summary to a text file.
  • Program.cs: Integrates all these parts.

3. Implementing the Functionality

3.1. Email Model
First, let's create a model to represent the captured emails. It will store the subject and content of the emails.

// Models/Email.cs
namespace EmailRPA.Models
{
    public class Email
    {
        public string Subject { get; set; }
        public string Snippet { get; set; }
    }
}
Enter fullscreen mode Exit fullscreen mode

3.2. Gmail Login Service
To automate the Gmail login process, we use Selenium to navigate to the login page, fill in the email and password, and proceed to the inbox.

// Services/EmailLoginService.cs
using OpenQA.Selenium;
using System.Threading.Tasks;

namespace EmailRPA.Services
{
    public class EmailLoginService
    {
        private readonly IWebDriver _webDriver;

        public EmailLoginService(IWebDriver webDriver)
        {
            _webDriver = webDriver;
        }

        public async Task<bool> LoginToGmailAsync(string email, string password)
        {
            _webDriver.Navigate().GoToUrl("https://accounts.google.com/signin/v2/identifier?service=mail");

            var emailField = _webDriver.FindElement(By.Id("identifierId"));
            emailField.SendKeys(email);
            _webDriver.FindElement(By.Id("identifierNext")).Click();

            await Task.Delay(6000);

            var passwordField = _webDriver.FindElement(By.Name("Passwd"));
            passwordField.SendKeys(password);
            _webDriver.FindElement(By.Id("passwordNext")).Click();

            await Task.Delay(5000);

            return _webDriver.Url.Contains("mail.google.com");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3.3. Capturing Emails
Now, let's capture the emails from the inbox. This class extracts the subject and content of the most recent emails.

// Services/EmailCaptureService.cs
using OpenQA.Selenium;
using EmailRPA.Models;
using System.Collections.Generic;
using System.Linq;

namespace EmailRPA.Services
{
    public class EmailCaptureService
    {
        private readonly IWebDriver _webDriver;

        public EmailCaptureService(IWebDriver webDriver)
        {
            _webDriver = webDriver;
        }

        public List<Email> CaptureRecentEmails(int emailCount = 5)
        {
            var emails = new List<Email>();
            var emailElements = _webDriver.FindElements(By.XPath("//tr[@class='zA zE']"));

            foreach (var emailElement in emailElements.Take(emailCount))
            {
                var subject = emailElement.FindElement(By.CssSelector(".bog")).Text;
                var snippet = emailElement.FindElement(By.CssSelector(".y2")).Text;

                emails.Add(new Email
                {
                    Subject = subject,
                    Snippet = snippet
                });
            }

            return emails;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

3.4. Email Summarization with ChatGPT
Here’s where ChatGPT comes into play. This class sends the captured emails to GPT and returns a summary.

// Services/EmailSummaryService.cs
using OpenAI.Chat;
using EmailRPA.Models;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EmailRPA.Services
{
    public class EmailSummaryService
    {
        private readonly ChatClient _chatClient;

        public EmailSummaryService(string openAIApiKey)
        {
            _chatClient = new ChatClient(new OpenAI.Client("gpt-4", openAIApiKey));
        }

        public async Task<string> SummarizeEmailsAsync(List<Email> emails)
        {
            var emailData = string.Join("\n\n", emails.Select(e => $"{e.Subject}: {e.Snippet}"));

            var prompt = $"Here are some recent emails:\n\n{emailData}\nPlease summarize the key points and list any pending actions.";
            var completion = await _chatClient.CompleteChatAsync(prompt);

            return completion.Value.ToString();
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

3.5. Saving the Summary to a File
Finally, we need to save the generated summary to a text file for future reference.

// Services/FileStorageService.cs
using System.IO;

namespace EmailRPA.Services
{
    public class FileStorageService
    {
        public void SaveToFile(string content, string filePath)
        {
            File.WriteAllText(filePath, content);
            System.Console.WriteLine($"Summary saved at: {filePath}");
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

4. Integrating Everything in Program.cs

Now that we have all the services set up, let’s integrate them into our main entry point.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using EmailRPA.Services;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        string email = "your-email@gmail.com";
        string password = "your-password";
        string openAIApiKey = "your-openai-api-key";
        string filePath = "SummaryEmails.txt";

        using (IWebDriver webDriver = new ChromeDriver())
        {
            var loginService = new EmailLoginService(webDriver);
            var captureService = new EmailCaptureService(webDriver);
            var summaryService = new EmailSummaryService(openAIApiKey);
            var fileStorageService = new FileStorageService();

            // Login to Gmail
            var loginSuccess = await loginService.LoginToGmailAsync(email, password);
            if (!loginSuccess)
            {
                System.Console.WriteLine("Login failed. Check your credentials.");
                return;
            }

            // Capture recent emails
            var emails = captureService.CaptureRecentEmails(10);

            // Summarize emails via ChatGPT
            var summary = await summaryService.SummarizeEmailsAsync(emails);

            // Save the summary to a file
            fileStorageService.SaveToFile(summary, filePath);
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

5. The Power of Automation and AI in Your Hands

Congratulations! You now have in your hands a system that combines intelligent automation and artificial intelligence to simplify the task of reading emails and extracting important information. This project can be expanded into various areas, from automated reporting to creating alerts about critical subjects in your emails.

With RPA and AI, you can transform manual and repetitive tasks into automatic and intelligent processes, increasing your productivity and reducing the time spent on routine tasks.

To get the full code access my github: EmailRPASystem

Top comments (0)