Automate the process of replying to first-time emails and labeling them in Gmail.
This project is a Gmail Auto-Reply Bot designed to identify and respond to first-time emails received in your Gmail mailbox. The bot sends automated replies with customizable content and labels the threads for better organization.
- Automatically replies to first-time emails.
- Labels the threads in Gmail for better organization.
- Uses local authentication for secure access to Gmail APIs (Login with Google).
- Periodically checks for new emails at random intervals.
- Node.js installed
- Gmail API credentials (credentials.json)
- npm or yarn package manager
-
Clone the repository:
git clone https://github.com/your-username/gmail-auto-reply-bot.git cd gmail-auto-reply-bot
-
Install Dependencies
npm install
-
Obtain Gmail API credentials:
- Follow the Gmail API Quickstart guide to obtain the
credentials.json
file.
- Follow the Gmail API Quickstart guide to obtain the
-
Configure constants:
- Open the
constants.js
file and updateselfMail
,replyContent
, andlabelName
with your email address, desired reply content, and label name.
- Open the
-
Create a
credentials.json
file- store your obtained gmail
credentials
in this file
- store your obtained gmail
Run the application using the following command:
npm start
The bot will check for new emails, reply to first-time emails, and label the threads at random intervals.
Description: The googleapis
library is a collection of Google APIs client libraries. It provides a set of APIs for interacting with various Google services, including Gmail.
Purpose in Code: Used for creating the Gmail API client and accessing Gmail-related functionalities.
Description: Local authentication module provides a mechanism for authenticating the application locally, usually during development. It helps in obtaining OAuth 2.0 tokens without the need for a web server.
Purpose in Code: Used for authenticating the application to access Gmail APIs locally.
Description: A module containing constants used throughout the application, such as email addresses, reply content, and label names.
Purpose in Code: Provides a centralized location for managing constants used in the application.
Description: Modules that encapsulate functionalities related to Gmail labels and threads. They abstract away the complexity of API interactions and provide methods for common operations.
Purpose in Code: Improves code organization and maintainability by modularizing functionalities related to labels and threads.
Description: Instances of ThreadsService
and LabelsService
classes are created to encapsulate methods related to threads and labels, respectively.
Purpose in Code: Improves code organization and adheres to the principles of object-oriented programming by encapsulating related functionalities into separate instances.
Description: The code extensively uses async/await
syntax to handle asynchronous operations. Promises are utilized for handling asynchronous tasks and improving code readability.
Purpose in Code: Ensures non-blocking behavior, making the code more readable and maintainable when dealing with asynchronous operations, such as API requests.
Description: Arrow functions are used for concise syntax, and template literals are employed for string interpolation and improved readability.
Purpose in Code: Enhances code readability and provides a more modern and concise syntax.
Description: The code calculates a random interval between 10 and 30 seconds for checking emails. It uses setTimeout
to schedule the next email check and handles the process interruption with process.on('SIGINT')
.
Purpose in Code: Ensures periodic and random checking of emails and provides a mechanism to gracefully handle the interruption of the checking process.
Description: The code listens for the SIGINT
signal (Ctrl+C) and clears the timeout, ensuring a clean exit when the process is stopped.
Purpose in Code: Provides a graceful way to stop the periodic email checking process.
Description: The code utilizes try/catch
blocks for error handling, capturing and logging any errors that occur during the execution of asynchronous tasks.
Purpose in Code: Ensures robust error handling, making the application more resilient to unexpected issues.
Description: The code takes advantage of ES6 features, including destructuring, object shorthand notation, and arrow functions, to write cleaner and more concise code.
Purpose in Code: Enhances code readability and adheres to modern JavaScript standards.
Description: The code listens for the SIGINT
signal to handle the process exit gracefully by clearing the timeout.
Purpose in Code: Ensures proper cleanup when the application is stopped, preventing lingering processes.
Description: The createReply
function dynamically generates the subject for the reply based on the original email's subject.
Purpose in Code: Provides flexibility and maintains context in the subject line of the automated replies.
auth.js
: Handles local authentication and token retrieval.constants.js
: Centralizes constants used throughout the application.services/labels.js
andservices/threads.js
: Modules encapsulating functionalities related to Gmail labels and threads.index.js
: Main application entry point.