You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何实现ManyBot开发的Telegram机器人将表单数据发送至邮箱

Got it, let's figure out how to get those ManyBot form submissions from your Telegram bot sent straight to your email. Since most solutions out there focus on forwarding emails to Telegram, here's a solid reverse workflow to make this happen:

Solution Overview

The core idea is to use a webhook to capture form data from ManyBot, then pass that data to a simple middleware service that sends it to your email via SMTP. Here's the step-by-step breakdown:

Step 1: Configure a Webhook in ManyBot

First, you need to tell ManyBot where to send form submission data:

  • Open your ManyBot dashboard and navigate to the form you want to use.
  • Go to the form's settings tab and look for the Webhook URL option.
  • Enter the URL of the middleware service you'll create (we'll cover hosting this next).
  • Optional: Set a Webhook Secret (a random string) to verify that requests come only from ManyBot, adding an extra layer of security.

Step 2: Build the Middleware Service

You'll need a lightweight service to receive ManyBot's POST request, parse the form data, and send it via email. Below is a Python example using Flask (you can adapt this to Node.js, PHP, etc.):

from flask import Flask, request
import smtplib
from email.mime.text import MIMEText
import os

app = Flask(__name__)

# Load sensitive data from environment variables (never hardcode these!)
EMAIL_SENDER = os.getenv("EMAIL_SENDER")
EMAIL_RECEIVER = os.getenv("EMAIL_RECEIVER")
EMAIL_PASSWORD = os.getenv("EMAIL_PASSWORD")
SMTP_SERVER = os.getenv("SMTP_SERVER", "smtp.gmail.com")
SMTP_PORT = int(os.getenv("SMTP_PORT", 465))
MANYBOT_SECRET = os.getenv("MANYBOT_SECRET")

@app.route('/manybot-form-webhook', methods=['POST'])
def handle_submission():
    # Verify webhook secret if set
    if MANYBOT_SECRET:
        incoming_secret = request.headers.get('X-ManyBot-Secret')
        if incoming_secret != MANYBOT_SECRET:
            return "Unauthorized", 403

    # Parse form data from ManyBot's request
    form_data = request.json
    if not form_data:
        return "No data received", 400

    # Format the data into a readable message
    email_body = "New Telegram Form Submission:\n\n"
    for field_name, field_value in form_data.items():
        email_body += f"- {field_name}: {field_value}\n"

    # Create and send the email
    msg = MIMEText(email_body)
    msg['Subject'] = "Telegram Bot: New Form Submission"
    msg['From'] = EMAIL_SENDER
    msg['To'] = EMAIL_RECEIVER

    try:
        with smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT) as server:
            server.login(EMAIL_SENDER, EMAIL_PASSWORD)
            server.send_message(msg)
        return "Submission forwarded to email", 200
    except Exception as e:
        # Log the error for debugging (adjust based on your hosting)
        print(f"Email send failed: {str(e)}")
        return "Internal error", 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=int(os.getenv("PORT", 5000)))

Key Notes for This Script:

  • Use environment variables to store sensitive data like email passwords (never hardcode them in your code).
  • Adjust the SMTP server/port to match your email provider:
    • Gmail: smtp.gmail.com, port 465 (use an app-specific password if 2FA is enabled)
    • Outlook/Office 365: smtp.office365.com, port 587
    • Yahoo: smtp.mail.yahoo.com, port 465
  • The script includes basic error handling and secret verification to keep things secure.

Step 3: Host the Middleware Service

ManyBot needs to access your webhook URL, so you can't run this script locally. Deploy it to a cloud hosting service like:

  • Heroku (free tier works for low-volume submissions)
  • Vercel (supports Python serverless functions)
  • AWS Lambda or Google Cloud Functions
  • Any cheap VPS provider

Once deployed, copy the public URL of your service and paste it into the ManyBot webhook setting you configured earlier.

Step 4: Test the Workflow

  • Have a test user submit your Telegram form.
  • Check your target email inbox (don't forget spam folders!).
  • If it doesn't work:
    • Check ManyBot's webhook logs (in the form settings) to confirm requests are being sent.
    • Look at your hosting service's logs to debug any errors (e.g., SMTP login failures, missing environment variables).

Additional Tips

  • HTML Email Formatting: For a cleaner look, modify the script to send HTML emails using MIMEMultipart and format the form data into a table.
  • Rate Limiting: If you expect high submission volume, add rate limiting to your middleware to prevent abuse.
  • Backup Logging: Save form submissions to a database (like SQLite or Firebase) alongside sending emails, just in case something goes wrong with the email delivery.

内容的提问来源于stack exchange,提问作者Natlus

火山引擎 最新活动