How to execute script on failed PAM login?

Looking for that through the Internet I found out that every distro has their own way of doing this. I’ve found mentions of pam_exec.

What is the right way to implement this?

For context, I am working on a sendmail drop-in replacement that sends messages to a Telegram chat. This is one feature I am looking to implement to send failed login attempts to that alert Telegram channel.