การส่งสัญญาณเตือนและการแจ้งเตือน
ทำให้บอตที่นำไปใช้งานแล้วดำเนินการต่อได้ทันทีด้วยการส่งสัญญาณเตือนผ่านอีเมล Slack และเว็บฮุก เมื่อบอตพบสิ่งใดหรือทำงานล้มเหลว
การส่งสัญญาณเตือนและการแจ้งเตือน เป็นบทเรียน Web Scraping & Bots ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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_priceThrottling 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] = nowAlerting 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))
raiseKeeping 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.
เรียนรู้ Python ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “การส่งสัญญาณเตือนและการแจ้งเตือน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การส่งสัญญาณเตือนและการแจ้งเตือน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Web Scraping & Bots ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Web Scraping & Bots มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การส่งสัญญาณเตือนและการแจ้งเตือน”
ทำให้บอตที่นำไปใช้งานแล้วดำเนินการต่อได้ทันทีด้วยการส่งสัญญาณเตือนผ่านอีเมล Slack และเว็บฮุก เมื่อบอตพบสิ่งใดหรือทำงานล้มเหลว คุณปฏิบัติ Web Scraping & Bots ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Web Scraping & Bots หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Web Scraping & Bots บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การส่งสัญญาณเตือนและการแจ้งเตือน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Web Scraping & Bots นี้ได้ไหม
ได้ บทเรียน Web Scraping & Bots ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- สร้างบอตติดตามราคา
- สร้างเครื่องมือติดตามโซเชียลมีเดีย
- นำบอตขึ้นแพลตฟอร์มคลาวด์
- การส่งสัญญาณเตือนและการแจ้งเตือน