Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

76 Commits

Folders and files

Repository files navigation

Email Integration - Gmail API

Yêu cầu

Lệnh cài nhanh cho Windows (Chạy bằng Powershell, với quyền quản trị viên)

winget install ngrok.ngrok; winget install pnpm.pnpm; winget install Python.Python.3.12

Nhập Y để chấp nhận

Tạo một terminal mới với quyền Admin và cập nhật ngrok

ngrok update

Tạo project

Bước 1

Tạo một project tại đây

Bước 2

Xác thực ngrok theo hướng dẫn và khởi chạy ngrok với cổng 8080

ngrok http 8080

Bước 3

Lấy google cloud project id và tên miền ngrok lần lượt dán vào {ID_project} và {Web_URL} vào script ở dưới

provider "google" {
  project = "{ID_project}"
}

# 1. Bật các API cần thiết
resource "google_project_service" "services" {
  for_each = toset([
    "gmail.googleapis.com",
    "pubsub.googleapis.com"
  ])
  service            = each.key
  disable_on_destroy = false
}

# 2. Tạo Pub/Sub Topic
resource "google_pubsub_topic" "gmail_topic" {
  name       = "gmail-notifications-topic"
  depends_on = [google_project_service.services]
}

# 3. Cấp quyền cho hệ thống Gmail push tin nhắn vào Topic
resource "google_pubsub_topic_iam_member" "gmail_publisher" {
  topic  = google_pubsub_topic.gmail_topic.name
  role   = "roles/pubsub.publisher"
  member = "serviceAccount:gmail-api-push@system.gserviceaccount.com"
}

# 4. Tạo Push Subscription trỏ về Webhook của bạn
resource "google_pubsub_subscription" "gmail_sub" {
  name  = "gmail-notifications-sub"
  topic = google_pubsub_topic.gmail_topic.name

  push_config {
    push_endpoint = "{Web_URL}/api/webhooks/gmail"
  }
}

Bước 4

Vào web này và chạy lệnh sau đây trong cloud shell

cat << 'EOF' > main.tf
{Script_ở_bước_3}
EOF

Bước 5

Cài terraform trên Google cloud shell

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
bash

Kích hoạt service và chạy lệnh sau đây trên cloud shell

terraform init
terraform apply

Nhập yes để xác nhận

Lấy credentials.json

  • Đầu tiên, cấu hình OAuth consent screen tại Google Auth Platform và chọn External.
  • Tiếp theo, vào trang web. Chọn Create Credentials -> OAuth client ID -> Desktop app. Tải file json và đặt vào thư mục root với tên credentials.json
  • Cuối cùng, thêm email vào test user tại đây

Scripts

Từ thư mục gốc repository, tạo một môi trường ảo riêng cho script:

python -m venv .venv
source .venv/bin/activate # Linux
.venv/Scripts/Activate.ps1 # Windows
python -m pip install -r requirements.txt

Tạo người dùng

python -m backend.helper create-user --name "Nguyen Viet Anh" --username vanh --password test.com.vn --email anhvietng62@gmail.com

Khởi động ứng dụng

Yêu cầu

Truyền giá trị google cloud project id và 2 địa chỉ email tương ứng vào .env trước khi khởi chạy ứng dụng

pubsub_oidc_topic=projects/{ID_project}/topics/gmail-notifications-topic
manager_email={Email_người_quản_lý}
sender_email={Email_hệ_thống} 

Biên dịch frontend trước khi chạy lệnh

cd frontend
pnpm build
cd ..

Lệnh chạy

python -m backend.app.main

Khởi động script nhỏ

python -m "script.send&recieve" --help

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages