Lesson 2 · Module 5: Omnichannel Integration: WhatsApp, Telegram and MCP
Connecting your agent to WhatsApp: receiving, replying and sending media
- Receive WhatsApp messages through a webhook
- Reply with text inside the 24-hour window
- Receive and send media: photos and PDF invoices
Why this matters
A customer who messages a business at 11 pm often gets no answer until the next morning, and by then they have booked elsewhere. A WhatsApp agent can answer in seconds, at any hour, but only if it can receive messages, reply correctly within WhatsApp's rules, and handle photos and documents. This lesson shows how those pieces connect, so the agent can read a customer's message, reply, and send back an invoice.
Key ideas
Webhook. A public web address that WhatsApp calls the moment a message arrives, instead of your system checking for new messages on a timer. Think of it as a doorbell.
Verification handshake. Before sending messages, Meta checks that your webhook is really yours. It sends your secret verify token and a random challenge number. If the token matches, you echo the challenge back with status 200 (OK).
Reading the payload. Each incoming message arrives as nested JSON, where entry, then changes, then value hold the useful data. A short example:
value = {"messages": [{"from": "201001234567", "type": "text",
"text": {"body": "Can I book Friday 5pm?"}}]}
msg = value["messages"][0]
print(msg["from"], msg["type"], msg["text"]["body"])
# 201001234567 text Can I book Friday 5pm?
The 24-hour window. You may send free text only within 24 hours of the customer's last message. Each new message from the customer reopens it. Outside that window, you may send only approved templates.
Media as ids. Photos and files arrive as a media id, not as the file itself. To read one, you request a temporary download URL and fetch the file with your access token. To send a PDF, you give a public HTTPS link, or upload the file first and send its id.
Try it before you watch
A customer last messaged your salon 26 hours ago. Can your agent send her a free-text reply, or must it use a template?
Show answerHide answer
It must use an approved template, because free text is allowed only within 24 hours of the customer's last message. Once the window closes, only templates work, and a new message from her would reopen the window.
Words to know
- webhook: a web address on your server that another service calls when something happens, such as a new message.
- verify token: a secret word you choose and enter in Meta's dashboard, which proves your webhook belongs to you.
- payload: the data inside a message, usually a nested structure of labelled values.
- POST request: a way of sending data to a web address, used here to send replies.
- media id: a reference number for a photo or file that WhatsApp holds, which you exchange for the actual file.
- customer service window: the 24-hour period after a customer's last message during which free-text replies are allowed.
What to watch for
- Notice how the handshake echoes the challenge only when the verify token matches, and how the wrong token gets a 403.
- Watch how the nested fields (entry, changes, value) are unwrapped one step at a time to reach the sender and text.
- Pay attention to the 24-hour rule and to the duplicate-message fix, since both come up again in the exercise.
Learn it with your tutor on the live board
Key points from the video
The salon that never sleeps
- A customer messages at 11 pm: "Can I book Friday?"
- The owner sees it at 9 am, and the slot is gone
- Your agent can answer in seconds, on WhatsApp
- Today: connect the agent so it can read and reply
layout: full
parts:
home: {box: Your salon, holds: [phone, agent, app]}
phone: {chip: Phone, sub: "WhatsApp line", icon: smartphone}
agent: {chip: Agent, sub: "Your AI receptionist", icon: bot}
app: {window: app.py, code: ["reply = agent.answer(message)", "send(reply)"]}
ask: {chat: "Can I book Friday?", in: app}
steps:
- at: "a client sends a WhatsApp message"
zoom_out: home
send: ask
- at: "The owner is asleep"
note: "Owner asleep, message waiting"
- at: "set up the official WhatsApp Business Cloud API"
highlight: phone
- at: "connect your agent to that line"
highlight: agent
- at: "owner's phone becomes a receptionist"
spokes: {of: agent, items: [Answers in seconds, Any hour, Sounds like the salon]} What you will learn
- Receive WhatsApp messages through a webhook
- Reply with text inside the 24-hour window
- Receive and send media: photos and PDF invoices
layout: full
parts:
setup: {box: Your setup, holds: [phone, agent]}
phone: {chip: Phone, sub: "Customer's phone", icon: smartphone}
agent: {chip: Agent, sub: "Your assistant", icon: bot}
ask: {chat: "Can I book Friday 5pm?", reply: "Friday 5pm is free. Book it?"}
steps:
- at: "how a WhatsApp message actually reaches your agent"
spokes: {of: phone, items: [Webhook, Pull the fields]}
- at: "reply with text and learn the 24-hour rule"
send: ask
- at: "handle media in both directions"
spokes: {of: agent, items: [Read a photo, Send a PDF invoice]}
- at: "every step maps to a node in tools like n8n or Make"
highlight: agent The round trip of one message
- WhatsApp calls your webhook URL
- Your workflow hands the text to the agent
- The reply goes back through the Cloud API
layout: wide
direction: LR
components:
client: {kind: user, label: Customer}
wa: {kind: external, label: WhatsApp Cloud API}
hook: {kind: api, label: Your webhook}
agent: {kind: llm, label: AI agent}
cal: {kind: service, label: Booking calendar}
links: [client->wa, wa->hook, hook->agent, agent->cal]
steps:
- at: "Let's see the whole journey"
show: [client]
- at: "types her question on WhatsApp"
show: [client, wa]
flow: ["client>wa: Friday 5pm?"]
- at: "Meta's servers receive it"
highlight: [wa]
- at: "That's your webhook address"
show: [client, wa, hook]
highlight: [hook]
- at: "works like a doorbell"
highlight: [hook]
- at: "Now follow one message"
show: all
- at: "sends it to your webhook as JSON"
flow: ["wa>hook: POST JSON"]
- at: "passes the text to the agent"
flow: ["hook>agent: text"]
- at: "checks the calendar"
flow: ["agent>cal: check slot", "cal>agent: free"]
- at: "travels back through the Cloud API"
flow: ["agent>hook: reply", "hook>wa: send", "wa>client: Booked?"] Proving the webhook is yours
VERIFY_TOKEN = "salon-secret-42"
def verify(params):
ok = params.get("hub.mode") == "subscribe"
ok = ok and params.get("hub.verify_token") == VERIFY_TOKEN
if ok:
return 200, params["hub.challenge"]
return 403, "forbidden"
print(verify({"hub.mode": "subscribe",
"hub.verify_token": "salon-secret-42",
"hub.challenge": "1158201444"}))
print(verify({"hub.mode": "subscribe",
"hub.verify_token": "wrong",
"hub.challenge": "1"})) Reading what the customer said
payload = {"entry": [{"changes": [{"value": {
"contacts": [{"profile": {"name": "Mona"},
"wa_id": "201001234567"}],
"messages": [{"from": "201001234567",
"id": "wamid.ABC123",
"type": "text",
"text": {"body": "Can I book Friday 5pm?"}}]
}}]}]}
value = payload["entry"][0]["changes"][0]["value"]
msg = value["messages"][0]
name = value["contacts"][0]["profile"]["name"]
print(name, msg["from"], msg["type"])
print(msg["text"]["body"]) Replying, and the 24-hour rule
import json
reply = {
"messaging_product": "whatsapp",
"to": "201001234567",
"type": "text",
"text": {"body": "Hi Mona! Friday 5pm is free. Book it?"},
}
# version number: from your Meta dashboard
print("POST https://graph.facebook.com/v21.0/<PHONE_ID>/messages")
print("Authorization: Bearer <ACCESS_TOKEN>")
print(json.dumps(reply, indent=2)) Receiving a photo or a file
- Media arrives as an id, not the file
- First, send that to the API
- Step 2: download it with your access token
- Then pass the image to a vision-capable model
layout: full
parts:
wa: {box: Your WhatsApp, holds: [photo, agent]}
photo: {chip: Photo, sub: "Customer's image", icon: image}
agent: {chip: Agent, sub: "Vision model", icon: bot}
steps:
- at: "A salon client sends a photo"
highlight: photo
- at: "carries a media id"
highlight: photo
note: "Media id, not the file"
- at: "First, send that id to the API"
highlight: photo
- at: "Second, download the file"
spokes: {of: photo, items: [Temporary URL, Download with token]}
- at: "hand the image to a model that can see"
highlight: agent Sending an invoice as a PDF
import json
# photo: "type": "image" instead of "document"
# private file: upload first, then send {"id": ...}
invoice = {
"messaging_product": "whatsapp",
"to": "201001234567",
"type": "document",
"document": {
"link": "https://example.com/inv/INV-1042.pdf",
"filename": "INV-1042.pdf",
"caption": "Your invoice: 450 EGP",
},
}
print(json.dumps(invoice, indent=2)) Worked example: book, then bill
layout: full
parts:
deal: {box: Mona's booking, holds: [flow, inv]}
flow: {chip: Workflow, sub: "Book, then bill", icon: workflow}
inv: {chip: Invoice, sub: "PDF to the customer", icon: file-text}
msg: {chat: "Friday at 5? Here's the style I want"}
steps:
- at: "It starts in the chat"
send: msg
- at: "Next, the workflow"
spokes: {of: flow, items: [Webhook, Photo, Calendar, Reply]}
- at: "On Friday the visit happens"
note: "Friday: the visit"
- at: "Finally, the invoice"
highlight: inv
- at: "creates a PDF for four hundred and fifty pounds"
spokes: {of: inv, items: [PDF, Polite caption]}
- at: "without the owner touching her phone"
highlight: flow Common mistake: the triple reply
- Meta expects a quick 200 reply from your webhook
- A slow agent means Meta sends the event again
- Fix: acknowledge with 200 at once, process afterwards
- Save message ids and skip ones you've seen
- Ignore status events (sent, delivered, read)
layout: full
parts:
home: {box: Your webhook, holds: [cust, hook, app]}
cust: {chip: Customer, sub: "Gets the same answer", icon: user}
hook: {chip: Webhook, sub: "Answers 200 at once", icon: server}
app: {window: app.py, code: ["reply = agent.answer(message)", "send(reply)"]}
ask: {chat: "Is Friday 5pm free?", in: app}
steps:
- at: "Here's the mistake that embarrasses almost every first build"
zoom_out: home
send: ask
- at: "waits for the agent to think"
spokes: {of: cust, items: [Slow agent, Meta resends]}
- at: "Now the customer gets three identical answers"
note: "Three identical answers"
- at: "Acknowledge with status 200 immediately"
highlight: hook
- at: "keep a list of message ids you've handled"
spokes: {of: hook, items: [Save message ids, Skip repeats]}
- at: "ignore events with no messages field"
note: "Ignore status events" Recap
- Messages reach your agent through a verified webhook
- Free-text replies only inside the 24-hour window
- Media travels as ids: fetch to read, link or upload to send
layout: full
parts:
day: {box: Your salon, holds: [owner, phone, app]}
owner: {chip: Owner, sub: "Relaxed at the counter", icon: coffee}
phone: {chip: Phone, sub: "Handles the chats", icon: smartphone}
app: {window: app.py}
ask: {chat: "Can I book Friday?", in: app}
steps:
- at: "WhatsApp delivers every message to your webhook"
send: ask
- at: "free text only within 24 hours"
highlight: phone
- at: "Third, media moves as ids"
highlight: phone
- at: "To read a photo"
zoom_out: day
spokes: {of: phone, items: [Fetch to read]}
- at: "To send an invoice"
spokes: {of: phone, items: [Link or upload]}
- at: "the owner can finally rest"
highlight: owner Your exercise
- Connect a WhatsApp Trigger to your test number
- Reply to any text with an agent-written answer
- If the message is a photo, describe it back
- Send yourself a sample PDF invoice
layout: full
parts:
desk: {window: Your test setup, holds: [learner, flow]}
learner: {chip: Learner, sub: "Test number ready", icon: laptop}
flow: {chip: Workflow, sub: "WhatsApp Trigger", icon: workflow}
msg: {chat: "Hi, is Friday free?", in: desk}
steps:
- at: "Using the test number from lesson one"
highlight: learner
- at: "add a WhatsApp Trigger in n8n or Make"
spokes: {of: flow, items: [Text reply, Photo, PDF]}
- at: "Send yourself a text"
zoom_out: desk
send: msg
- at: "Then send a photo"
highlight: learner
- at: "make the workflow send you a sample PDF invoice"
highlight: flow
- at: "Keep that workflow"
highlight: flow Transcript
The salon that never sleeps. Picture a small hair salon. At eleven at night a client sends a WhatsApp message asking for a Friday appointment. The owner is asleep, sees it at nine the next morning, and by then the client has booked somewhere else. That happens every week to thousands of small businesses. In the last lesson you set up the official WhatsApp Business Cloud API and your message templates. Now we connect your agent to that line, so the owner's phone becomes a receptionist that answers in seconds, at any hour, and still sounds like the salon.
What you will learn. Here's the plan for this lesson. First, you'll see how a WhatsApp message actually reaches your agent, using something called a webhook, and how to pull the useful parts out of it. Second, you'll reply with text and learn the 24-hour rule WhatsApp enforces on every business. Third, you'll handle media in both directions: reading a photo a customer sends you, and sending them a PDF invoice. You'll see small Python snippets so you know what's happening under the hood, but every step maps to a node in tools like n8n or Make.
The round trip of one message. Let's see the whole journey before any details. A client called Mona types her question on WhatsApp. Meta's servers receive it, and they need to know where to forward it. That's your webhook address, which is just a public URL you own. A webhook works like a doorbell: instead of you checking for new messages every minute, WhatsApp rings your URL the moment one arrives. Now follow one message. WhatsApp sends it to your webhook as JSON, which is simply a text format where every value carries a label. Your workflow passes the text to the agent. The agent checks the calendar. The reply travels back through the Cloud API to Mona's phone.
Proving the webhook is yours. Before Meta sends you a single message, it checks that the URL really belongs to you. You choose a secret word, called the verify token, and type it into the app dashboard along with your URL. Meta then calls your URL once with that token and a random number called the challenge. If the token matches, you echo the challenge back with status 200, meaning OK. If not, you refuse. In the output, the right token returns the challenge number, and the wrong one gets a 403. In n8n, the WhatsApp Trigger node does this handshake for you.
Reading what the customer said. The message arrives wrapped in several layers, so let's unwrap it slowly. Entry, then changes, then value: that's where everything useful lives. Inside value you'll find contacts, which gives you the customer's display name, and messages, which holds the sender's number, a unique message id, the type, and the text itself. Those four fields are all your agent needs to start. The number tells you who to reply to and which memory to load. The type tells you whether it's text, an image or a document. The id lets you avoid handling the same message twice, which matters later in this lesson.
Replying, and the 24-hour rule. To reply, you send a POST request, which simply means sending data to a web address. The address is the messages endpoint, the exact web address for your phone number ID, and the version number in it comes from your dashboard. You also include your access token, a password-like key from the Meta dashboard, in a line called the Authorization header. In n8n or Make, each of these is just a field in the WhatsApp or HTTP node. The body names the recipient, the type, and the text the agent wrote. Now the rule that catches everyone. You may send free text only within 24 hours of the customer's last message. That's the customer service window. Each new message from Mona opens it again for another 24 hours. Once it closes, you can only send one of the approved templates you built in lesson one, like an appointment reminder.
Receiving a photo or a file. Customers love sending pictures. A salon client sends a photo of the haircut she wants, or a shop customer sends a screenshot of a bank transfer. The webhook doesn't carry the picture itself. It carries a media id, not the file. So you make two calls. First, send that id to the API, and it returns a temporary download URL. Second, download the file from that URL, again sending your access token. Then hand the image to a model that can see, and the agent can say, "Got it, that's a layered bob, about ninety minutes."
Sending an invoice as a PDF. Sending media uses the same messages endpoint with a different type. For an invoice, the type is document, and you give a link to the PDF, a file name the customer will see, and a short caption. For a photo, such as a before and after picture, the type is image instead. One condition matters: the link must be a public HTTPS address, meaning a secure web link that Meta can download. If your invoices are private files, upload the PDF to the media endpoint first, get back an id, and send that id in place of the link.
Worked example: book, then bill. Let's put it all together for the salon. It starts in the chat. Mona sends a photo of a hairstyle and asks for Friday at five. Next, the workflow. The webhook fires, the workflow downloads the photo, and the agent sees the style, checks the calendar, books the slot and sends back a reply with the confirmation, all inside the 24-hour window. On Friday the visit happens. Finally, the invoice. The workflow creates a PDF for four hundred and fifty pounds and sends it as a document message with a polite caption. One conversation, booked and billed, without the owner touching her phone.
Common mistake: the triple reply. Here's the mistake that embarrasses almost every first build. Your webhook waits for the agent to think, check the calendar and write a reply, which can take ten seconds or more. Meta doesn't hear back quickly, assumes delivery failed, and sends the same event again. Now the customer gets three identical answers. The remedy has two parts. Acknowledge with status 200 immediately and do the work afterwards. And keep a list of message ids you've handled, skipping any repeat. Also note that delivery and read receipts hit the same URL, so ignore events with no messages field.
Recap. Let's recap. First, WhatsApp delivers every message to your webhook, after a one-time handshake with your verify token, and the useful fields sit under entry, changes and value. Second, your agent can reply with free text only within 24 hours of the customer's last message. After that, use an approved template. Third, media moves as ids. To read a photo, fetch its URL and download it with your token. To send an invoice or picture, give a public link or upload the file first. Answer fast, skip duplicates, and the owner can finally rest.
Your exercise. Now it's your turn, and you can finish this in under an hour. Using the test number from lesson one, add a WhatsApp Trigger in n8n or Make and connect it to your agent. Send yourself a text and make sure the agent replies once, not three times. Then send a photo and have the agent describe what it sees. Finally, make the workflow send you a sample PDF invoice with a caption. Keep that workflow, because next lesson we'll plug a Telegram bot into the very same agent.
Explore the animations
The animations from the video, exact and live: play them, pause, drag the slider or jump to a step at your own pace.
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Loading the animation...
Python runs in your browser, nothing to install. The first run downloads Python (about 20 MB, then cached).
1 Answer the webhook handshake
Meta checks your webhook with a GET request carrying hub.mode, hub.verify_token and hub.challenge. Complete verify so it returns (200, challenge) when the mode is subscribe and the token matches VERIFY_TOKEN, and (403, "forbidden") otherwise.
2 Read the first message from a payload
WhatsApp wraps every event in entry, changes and value. Complete read_message so it returns a dict with name, from, id, type and text for the first message. text is the body only for text messages (otherwise None), and the function returns None for events with no messages, such as delivery receipts.
3 Choose text or template by the 24-hour window
WhatsApp allows free text only within 24 hours of the customer's last message. Complete send_mode so it returns "text" while the window is open and "template" once it has closed. Times are seconds since the epoch, and exactly 24 hours after the last message the window is already closed.
Answer every question, then check. 70% passes; you can retry as often as you like.
Lesson summary
- Reading
- Video watched
- Exercises
- Quiz passed (70%)
- Messages reach your agent through a verified webhook
- Free-text replies only inside the 24-hour window
- Media travels as ids: fetch to read, link or upload to send