/ZsecMail

Telegram Bot for Disposable Email Service using Cloudflare Workers

Primary LanguageJavaScript

  1. Token and Configuration:

    • telegramAuthToken: This is the authentication token required to communicate with the Telegram Bot API.
    • webhookEndpoint: This is the endpoint where Telegram will send updates.
    • oneSecMailApiUrl: This is the API endpoint to generate a random temporary email address.
  2. Event Listener and Handler:

    • addEventListener('fetch', event => {...}): This adds an event listener for the 'fetch' event. This is a Cloudflare Worker specific API. When an HTTP request is made, this event listener will trigger the provided handler function.
    • async function handleIncomingRequest(event) {...}: This is the handler function that will process incoming HTTP requests.
  3. Request Handling:

    • The handler function extracts information from the incoming request such as the URL, method, and path.
    • It checks if the request method is POST and the path matches the webhook endpoint. If so, it processes the incoming Telegram update.
    • If the request method is GET and the path is '/configure-webhook', it sets up the webhook with Telegram API by sending a request to https://api.telegram.org/bot{token}/setWebhook.
    • Otherwise, it returns a 404 Not Found response.
  4. Processing Telegram Updates:

    • async function processUpdate(update) {...}: This function processes the Telegram update received from the webhook.
    • It checks if the update contains a message.
    • If the message is '/start', it sends a welcome message to the user.
    • If the message is '/generate_email', it generates a temporary email address using the oneSecMailApiUrl and sends it to the user.
    • If the message is '/fetchmail', it fetches emails for the previously generated temporary email address and sends them to the user.
    • If the message is '/info', it sends developer information to the user.
  5. Sending Requests to Telegram API:

    • The function uses fetch() to send requests to the Telegram API for various actions such as sending messages, setting webhooks, etc.
  6. Error Handling:

    • The code includes error handling for failed API requests and invalid user input.
  7. Response Handling:

    • The handler function returns appropriate HTTP responses to the client based on the request and processing results.

This code essentially sets up a Cloudflare Worker to act as a webhook for a Telegram bot. It handles incoming updates from Telegram, processes them based on predefined commands, and interacts with the Telegram API to send responses back to users.