Missed Call Telegram Notifications

This article describes two ways to implement Telegram notifications for missed calls.

Dialplan-Based Example

  1. Go to the "System File Customization" section:

System file customization section
  1. Open the "extensions.conf" file for editing. Set the mode to "Append to the end of the file" and insert the following context:

[add-trim-prefix-clid-custom]
exten => _[0-9*#+a-zA-Z][0-9*#+a-zA-Z]!,1,NoOp(start check blacklist)
	same => n,Set(CHANNEL(hangup_handler_push)=hangup-ext-queues,h,1);
	same => n,Return()

[hangup-ext-queues]
exten => h,1,ExecIf($["${M_DIALSTATUS}" = "ANSWER"]?return)
    same => n,Set(TOKEN=5118292900:AAEWCOAXkay5fXb8AJptZmDyqkNk8QbP200)
    same => n,Set(CHAT_ID=939950800)
    same => n,Set(URL=https://api.telegram.org/bot${TOKEN}/sendMessage)
    same => n,Set(TEXT=MISSED CALL from: ${CALLERID(name)}, did: ${FROM_DID}, callid: ${CHANNEL(callid)})
    same => n,SHELL(curl -s -X POST '${URL}' -d chat_id='${CHAT_ID}' -d text='${TEXT}')
    same => n,Set(MISSED=${SHELL(curl -s -X POST '${URL}' -d chat_id='${CHAT_ID}' -d text='${TEXT}')})
    same => n,return

In this context, replace:

  • TOKEN with your Telegram bot’s token.

  • CHAT_ID with the chat ID where messages should be sent.

Save your changes.

Editing file "extensions.conf"
  1. Go to "modules.conf" for editing. Set the mode to "Append to the end of the file" and insert the following line:

load => func_shell.so

Save your changes.

Editing file "modules.conf"

PHP-AGI Example

  1. Go to "Dialplan Applications" and create a new application by clicking "Add a new":

Creating new dialplan app
  1. Provide the following parameters for the dialplan:

  • Name – any name

  • Number to Call the Application – any number

  • Code Type – "PHP-AGI script"

Dialplan parameters
  1. Go to the "Program Code" tab and insert the following PHP-AGI script:

<?php
require_once 'Globals.php';
use \GuzzleHttp\Client;

const API_KEY = '';
const CHAT_ID = '';

$agi = new MikoPBX\Core\Asterisk\AGI();

$name = $agi->get_variable('CALLERID(name)', true);
$num  = $agi->get_variable('CALLERID(num)', true);
$did  = $agi->get_variable('$FROM_DID', true);
$id   = $agi->get_variable('CHANNEL(linkedid)', true);
$date = date('Y.d.m H:i:s', str_replace('mikopbx-', '', $id));

$TEXT = "Missed call: $name, did: $did, callid: $num, id: $id, date: $date";
$apiURL = 'https://api.telegram.org/bot' . API_KEY . '/';
$client = new Client([
    'base_uri' => $apiURL,
    'timeout' => 1,
    'http_errors' => false,
]);
try {
    $client->post( 'sendMessage', ['query' => ['chat_id' => CHAT_ID, 'text' => $TEXT]] );
}catch (Throwable $e){
}

In this context, replace:

  • API_KEY with your Telegram bot token.

  • CHAT_ID with the chat ID where you want to send the message.

You can modify the notification text in the $TEXT variable.

  1. After saving the script, copy its identifier from the browser’s address bar, which looks like "DIALPLAN-APP-1B2B846E":

Dialplan application identifier
  1. Go to "System file customization":

"System file customization" section
  1. Open "extensions.conf" for editing. Set the mode to "Append to the end of the file" and insert the following context:

[add-trim-prefix-clid-custom]
exten => _.X!,1,Set(CHANNEL(hangup_handler_push)=hangup-ext-queues,h,1);
	same => n,return

[hangup-ext-queues]
exten => h,1,ExecIf($["${M_DIALSTATUS}" = "ANSWER"]?return)
    same => n,AGI(DIALPLAN-APP-1B2B846E.php)
    same => n,return

Replace "DIALPLAN-APP-1B2B846E" with your dialplan application ID.

Save your changes.

Editing file "extensions.conf"

Last updated

Was this helpful?