Sending the caller's number in a Telegram

Hello everyone. How to implement sending messages with the caller’s number in telegram? There is a queue on how to make it so that at the time of the operator’s response, a message with the caller’s number arrives in the telegram. Thank you all.

Hello @Vital174,

Can you elaborate more on how do you want to achieve this? for example:

  1. A user is calling to your PBX
  2. You are playing him a message that you will call him back as soon as possible.
  3. Collect his CLI (Caller ID).
  4. Send a message to the Telegram API with the collected CLI.

Thank you,

Daniel Friedman
Trixton LTD.

Hello. An external user calls the number and gets into the queue, the operator answers the call and at this moment, a message comes in a telegram with the caller’s number

Hello @Vital174,

  1. Do you need that the message to the Telegram would be sent after the agent answers the call?
  2. Do you know how to get the data from the PBX to a web service that will send the message to the Telegram API ?
  3. Which version of Freepbx you are using? the open-source or the commercial one?

Thank you,

Daniel Friedman
Trixton LTD.

  1. Yes
  2. No, I don’t know
  3. FreePBX 14.0.13.34-open

Hello @Vital174,

First,
here is a link for you to read on how to implement Telegram API:

Second,
If you want to send the message after an agent answers the call, you will have to create a custom context that will trigger another custom context to send the message to the Telegram API upon answering the call.

If you have an external web service that will help you to send the message to the Telegram API, it would be an easier integration from the PBX point of view.

So, here is an example of how to trigger another custom context upon an agent answers the call:

  1. Configure a misc app for your queue with a 3300 extension.

  2. Configure a custom destination (queue-answer-handler,${EXTEN},1) that will send the call to it from the IVR (assuming you are using an IVR)

  3. insert this code to the extensions_custom.conf file and reload the dialplan (dialplan reload).

     [queue-answer-handler]
     exten => _X!,1,Log(NOTICE, An incoming call to Queue from CLI:${CALLERID(num)})
     ;Set after answer sub routine
     same => n,Set(__QGOSUB=sub-queue-answer)
     ;Set Caller ID for the queue
     same => n,Set(__QCALLER_ID=${CALLERID(num)})
     ;Dial the misc app extension of the queue
     same => n,Goto(from-internal,3300,1)
    
     [sub-queue-answer]
     exten => s,1,Set(__ANSWERED_BY=${CUT(CUT(CHANNEL(name),/,2),@,1)})
     same => n,Set(__Q_MESSAGE=An incoming call from ${QCALLER_ID} has been answered by ${ANSWERED_BY})
     ;Send the message to the Telegram API 
     same => n,System(curl -d chat_id={YOUR_CHAT_ID} -d text=${Q_MESSAGE} https://api.telegram.org/bot{YOUR_API_KEY_HERE}/sendMessage)
     same => n,Return()
    

Thank you,

Daniel Friedman
Trixton LTD.

Thanks. I will try, I will write according to the result.

Does not work. What could be the problem? The following scheme works for me: in the extensions_override_freepbx.conf file, I insert the code
[ext-local]
exten => 102,1,Set(__RINGTIMER=${IF($["${DB(AMPUSER/102/ringtimer)}" > “0”]?${DB(AMPUSER/102/ringtimer)}:${RINGTIMER_DEFAULT})})
exten => 102,n,System(/var/www/html/sm.sh ${CALLERID(num)} ) ;
exten => 102,n,ExecIf($["${REGEX(“from-queue” ${CHANNEL})}"=“1” && “${CONTEXT}”=“from-internal-xfer”]?Set(__CWIGNORE=))
exten => 102,n,Macro(exten-vm,novm,102,0,0,0)
exten => 102,n(dest),Set(__PICKUPMARK=)
exten => 102,n,GotoIf($["${IVR_CONTEXT}" != “”]?${IVR_CONTEXT},return,1)
exten => 102,hint,SIP/102&Custom:DND102,CustomPresence:102

Next, I place the script /var/www/html/sm.sh

#!/bin/sh

API_TOKEN=‘TOKEN’
CHAT_ID=‘ID CHAT’
MSG="$1 $2 $3"
if [ -z “$CHAT_ID” ]; then
echo ‘Please, define CHAT_ID first! See “chat”:{“id”:xxxxxxx string below:’
/usr/bin/wget -qO - https://api.telegram.org/bot$API_TOKEN/getUpdates
exit 1
fi

/usr/bin/wget -O /dev/null -q “https://api.telegram.org/bot$API_TOKEN/sendMessage?chat_id=$CHAT_ID&parse_mode=html&text=$MSG” 2>&1

if [ $? -eq 0 ]; then
echo ‘Message sent successfully.’
else
echo ‘Error while sending message!’
exit 1
fi

Where 102 is my internal number. This scheme works, but only if you make a direct call to the 102 number. Also, messages are received even if the user did not pick up the phone.
I don’t use IVR. The call goes directly to the queue

Hello @Vital174,

It is not working because you are not using the correct dialplan.
Don’t use the extensions_override_freepbx.conf because it is intended only for Freepbx overriding dialplan, and in case of module upgrades, you will not enjoy them.

It is not a problem if you are not using an IVR, but you need to send the call directly to the custom context that I showed you to set the correct parameters before the call continues to the queue.

You can customize my dialplan to send the call directly to the queue if you want.

So, the correct action from here is to set a misc-app for your queue and send the call to the custom destination of the custom context.

inbound route -> custom-destination -> custom-context

Thank you,

Daniel Friedman
Trixton LTD.

Hello @Vital174,

If you are not sending the call from an IVR, then you need to change slightly my dialplan as it was intended to be executed from an IVR with a one digit option (_X!) like that:

The line same => n,Goto(from-internal,3300,1) contains the 3300 extension, but you can change it to the queue number or to the misc-app extension.

[queue-answer-handler]
 exten => _X.,1,Log(NOTICE, An incoming call to Queue from CLI:${CALLERID(num)})
 ;Set after answer sub routine
 same => n,Set(__QGOSUB=sub-queue-answer)
 ;Set Caller ID for the queue
 same => n,Set(__QCALLER_ID=${CALLERID(num)})
 ;Dial the misc app extension of the queue
 same => n,Goto(from-internal,3300,1)

 [sub-queue-answer]
 exten => s,1,Set(__ANSWERED_BY=${CUT(CUT(CHANNEL(name),/,2),@,1)})
 same => n,Set(__Q_MESSAGE=An incoming call from ${QCALLER_ID} has been answered by ${ANSWERED_BY})
 ;Send the message to the Telegram API 
 same => n,System(curl -d chat_id={YOUR_CHAT_ID} -d text=${Q_MESSAGE} https://api.telegram.org/bot{YOUR_API_KEY_HERE}/sendMessage)
 same => n,Return()

Thank you,

Daniel Friedman
Trixton LTD.

I didn’t understand, I need to add a custom context to Freepbx, via the GUI, and then, in extensions_custom.conf, write what you recommended?

Hello @Vital174,

Yes, exactly.

Every time you need to fork out from the Freepbx dialplan, you are doing this with the custom destinations module. It is very convenient, and helps you to maintain your Freepbx modules updated.

Thank you,

Daniel Friedman
Trixton LTD.

I did this: Admin-Custom Destinations-Add Custom Destination, created “telegramqueue,s,1”. What are my next steps?

Hello @Vital174,

I wrote to you exactly how to configure the custom destination: queue-answer-handler,${EXTEN},1
Then, setup an inbound route and choose this custom-destination as a destination.

After applying the configuration, call the external number that you configured in the inbound route from your mobile phone and watch the log.

Thank you,

Daniel Friedman
Trixton LTD.

The call went into the queue, after the answer: “curl -d chat_id={my id chat} -d text=An incoming call from my phone number has been answered by 102 https://api.telegram.org/bot{my_API_KEY_HERE}/sendMessage” but nothing came in the telegram

Hello @Vital174,

That is good progress. Maybe we need to put quotation marks on the parameters. like that:

[queue-answer-handler]
 exten => _X.,1,Log(NOTICE, An incoming call to Queue from CLI:${CALLERID(num)})
 ;Set after answer sub routine
 same => n,Set(__QGOSUB=sub-queue-answer)
 ;Set Caller ID for the queue
 same => n,Set(__QCALLER_ID=${CALLERID(num)})
 ;Dial the misc app extension of the queue
 same => n,Goto(from-internal,3300,1)

[sub-queue-answer]
exten => s,1,Set(__ANSWERED_BY=${CUT(CUT(CHANNEL(name),/,2),@,1)})
same => n,Set(__Q_MESSAGE=An incoming call from ${QCALLER_ID} has been answered by ${ANSWERED_BY})
;Send the message to the Telegram API 
same => n,System(curl -d chat_id="{YOUR_CHAT_ID}" -d text="${Q_MESSAGE}" https://api.telegram.org/bot{YOUR_API_KEY_HERE}/sendMessage)
same => n,Return()

Please provide also the logs (core set verbose 4).

Thank you,

Daniel Friedman
Trixton LTD.

I redid it, nothing has changed. And how do you send the log?

Hello @Vital174,

Can you send me your dialplan code? I just noticed that you are using a bash helper script to send the Telegram message. You can turn on log with this command (core set verbose 4)

Thank you,

Daniel Friedman
Trixton LTD.

I made such a scheme and everything worked as it should!

[sub-queue-answer]
exten => s,1,Set(__ANSWERED_BY=${CUT(CUT(CHANNEL(name),/,2),@,1)})
same => n,Set(__Q_MESSAGE=An incoming call from ${QCALLER_ID} has been answered by ${ANSWERED_BY})
same => n,System(/var/www/html/sm.sh ${CALLERID(num)} ) ;
same => n,Return()

1 Like

Through the script.

1 Like