0Pricing
Web Scraping & Bots · 课时

发送警报与通知

当已部署的机器人发现问题或发生故障时,通过电子邮件、Slack 和 Webhook 推送警报,让机器人真正发挥作用。

发送警报与通知 是 CoddyKit 上的免费 Web Scraping & Bots 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Web Scraping & Bots 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Web Scraping & Bots 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

From Data to Action

A deployed bot that quietly stores data is only half useful. The real value comes when it notifies you: a price dropped, a keyword appeared, or the bot itself broke.

This lesson covers wiring bots to notification channels.

Choosing a Channel

Pick the channel that fits urgency:

  • Email for digests and non-urgent reports.
  • Slack/Discord for team-visible real-time alerts.
  • Push/SMS for urgent, must-see events.

Sending Email

Python's smtplib sends email through any SMTP server. Build the message, connect securely, and send.

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['Subject'] = 'Price Alert'
msg['From'] = 'bot@example.com'
msg['To'] = 'me@example.com'
msg.set_content('Widget dropped to $7.99')

with smtplib.SMTP_SSL('smtp.example.com', 465) as s:
    s.login('bot@example.com', 'password')
    s.send_message(msg)

Slack via Webhook

Slack incoming webhooks accept a simple JSON POST. No SDK required, just a URL you keep secret.

import requests

requests.post(
    'https://hooks.slack.com/services/XXX/YYY/ZZZ',
    json={'text': 'Price dropped to $7.99 :tada:'}
)

Rich Messages

Most chat webhooks support structured blocks: titles, links, and fields. A well-formatted alert is faster to act on than a wall of text.

payload = {
  'blocks': [
    {'type': 'section', 'text': {'type': 'mrkdwn', 'text': '*Price Alert*\nWidget: $7.99'}}
  ]
}
requests.post(webhook_url, json=payload)

Generic Webhooks

Webhooks let your bot trigger anything: a Zapier flow, a custom server, or another service. Send a JSON payload and let the receiver decide what to do.

requests.post('https://my-endpoint.example/hook',
              json={'event': 'price_drop', 'price': 7.99})

Alerting Only on Change

Do not notify on every run. Compare against the last known state and alert only when something meaningful changes, to avoid notification fatigue.

if new_price < last_price:
    send_alert('price dropped to ' + str(new_price))
last_price = new_price

Throttling and Deduplication

Suppress repeat alerts for the same event within a window. Track recently-sent keys so a flapping condition does not spam you.

import time

last_sent = {}
def alert_once(key, msg, cooldown=3600):
    now = time.time()
    if now - last_sent.get(key, 0) > cooldown:
        send_alert(msg)
        last_sent[key] = now

Alerting on Bot Failure

Notify yourself when the bot itself breaks, not just on data events. Wrap the run and send an alert from the except block so silent crashes never go unnoticed.

try:
    run_bot()
except Exception as e:
    send_alert('Bot crashed: ' + str(e))
    raise

Keeping Secrets Safe

Webhook URLs, SMTP passwords, and API tokens are credentials. Store them in environment variables or a secrets manager, never hard-coded in the repository.

import os
webhook = os.environ['SLACK_WEBHOOK_URL']

Confirming Delivery

A notification call can silently fail. Check the response status and log it, so a broken webhook does not leave you unaware that alerts stopped arriving.

resp = requests.post(webhook, json=payload)
if not resp.ok:
    log.error('alert delivery failed: %s', resp.status_code)

Quick Check

Test your understanding of notifications.

Recap

You learned to make deployed bots actionable: choose a channel, send email via smtplib, post to Slack and generic webhooks, format rich messages, alert only on change, throttle duplicates, notify on bot failure, and keep credentials in environment variables.

常见问题解答

「发送警报与通知」课时是免费的吗?

是的 — 「发送警报与通知」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Web Scraping & Bots 课程的其余内容,请升级到 CoddyKit PRO。 Web Scraping & Bots 课程共包含 4 节课。

「发送警报与通知」这节课中我会学到什么?

当已部署的机器人发现问题或发生故障时,通过电子邮件、Slack 和 Webhook 推送警报,让机器人真正发挥作用。 你通过在浏览器中直接运行的动手代码来练习 Web Scraping & Bots,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Web Scraping & Bots 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Web Scraping & Bots 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「发送警报与通知」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Web Scraping & Bots 课中编写并运行代码吗?

能。每节 Web Scraping & Bots 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 构建价格追踪机器人
  2. 创建社交媒体监测器
  3. 将机器人部署到云平台
  4. 发送警报与通知
← 返回 Web Scraping & Bots