Last active
September 27, 2024 17:07
-
-
Save adamsrog/230e7460988735c9fc5df7696fd5e153 to your computer and use it in GitHub Desktop.
Mailgun webhook for delivering messages to Telegram Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// check for POST method | |
if ($_SERVER['REQUEST_METHOD'] == 'POST') { | |
// get payload data | |
$post = json_decode(file_get_contents('php://input'), true); | |
$signature = $post['signature']; | |
$data = $post['event-data']; | |
$key = "MAILGUNAPIKEY"; | |
// verify legitimacy of payload | |
if (hash_hmac('sha256', $signature['timestamp'] . $signature['token'], $key) === $signature['signature']) { | |
// detect type of message | |
if ($data['event'] == "delivered") { | |
// successful delivery | |
$message = "👍 SUCCESS delivery to " . $data['message']['headers']['to'] . " with subject " . $data['message']['headers']['subject']; | |
} elseif ($data['event'] == "failed" && $data['severity'] == "temporary") { | |
// temporary failure (unknown address) | |
$message = "🚨 UNRECOGNIZED ADDRESS FAILED delivery to " . $data['message']['headers']['to'] . " with subject " . $data['message']['headers']['subject']; | |
} elseif ($data['event'] == "failed" && $data['severity'] == "permanent") { | |
// permanent failure | |
$message = "🚨 FAILED delivery to " . $data['message']['headers']['to'] . " with subject " . $data['message']['headers']['subject']; | |
} elseif ($data['event'] == "complained") { | |
// spam complaint recieved | |
$message = "🚨 SPAM reported from delivery to " . $data['message']['headers']['to'] . " with subject " . $data['message']['headers']['subject']; | |
} elseif ($data['event'] == "unsubscribed") { | |
// unsubscribed | |
$message = "🚫 " . $data['recipient'] . " unsubscribed from " . $data['geolocation']['city'] . ", " . $data['geolocation']['region'] . " (" . $data['geolocation']['country'] . ")"; | |
} elseif ($data['event'] == "clicked") { | |
// clicked | |
$message = "✅ " . $data['recipient'] . " clicked from " . $data['geolocation']['city'] . ", " . $data['geolocation']['region'] . " (" . $data['geolocation']['country'] . ")"; | |
} elseif ($data['event'] == "opened") { | |
// opened the message | |
$message = "✅ " . $data['recipient'] . " opened from " . $data['geolocation']['city'] . ", " . $data['geolocation']['region'] . " (" . $data['geolocation']['country'] . ")"; | |
} else { | |
$message = "Unrecognized event."; | |
} | |
// send notice to telegram | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, "https://api.telegram.org/BOTIDHERE/sendMessage"); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, "chat_id=CHATIDHERE&text=$message"); | |
curl_exec($ch); | |
curl_close($ch); | |
echo $message; | |
} else { | |
echo "Unable to verify payload."; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
MAILGUNAPIKEY = Mailgun webhook signing key