Send SMS message to IP when ext is dialed using sockets

freepbx
Tags: #<Tag:0x00007fafcdee4c68>

(D Jr W) #1

Is there a way to send a SMS message to IP using sockets when extension is dialed?

this is Python code:
#!/usr/bin/env python3

import time
import socket, sys

def sendMessage(msg): # Calling function
try:
print(msg)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
host = “...” # Get local machine name
port = ***** # Reserve a port for your service.
s.connect((host, port)) # Bind to the port
s.send(msg.encode())
print(“Message sent”)
from_server = s.recv(4096)
s.close()
except:
print(sys.exc_info()[0])

when 302 is dialed sendMessage(“Input 1”)
when 302 is hungup sendMessage(“Input 1 Off”)

when 303 is dialed sendMessage(“Input 2”)
when 303 is hungup sendMessage(“Input 2 Off”)

when 304 is dialed sendMessage(“Input 3”)
when 304 is hungup sendMessage(“Input 3 Off”)

Basically what I want to do


#2

Asterisk has built-in functions for sending messages. Do you really mean something like SMS (in SIP, it would be SIP MESSAGE method), which can be done directly through dialplan, or do you mean custom network messages that are not SIP?


(D Jr W) #3

I’m looking to sent a SMS text Message to a pacific IP/Port address, so the device can act according to the text message when the number is dialed.

If I can send it with SIP message, I would but don’t know how

Ex: if 302 is dialed send ‘Input 1 On’ to the IP/Port Server.
If 302 is disconnected to send ‘Input 1 Off’ to the PI/Port Server.


#4

It’s just important that terminology is right.

This does not make any sense, so you need to refine it. SMS has meaning on the cellular phone network but it does not seem like that’s what you really mean.

Do you want to do some network socket low-level stuff or do you want to send a SIP MESSAGE?

If you want to send a SIP message, here’s the basic outline.

Define an extension (PJSIP or SIP) in FreePBX to receive your message.

In some custom dialplan (https://wiki.asterisk.org/wiki/display/AST/Contexts%2C+Extensions%2C+and+Priorities), define exten 302 to do a MessageSend() pointed at your recipient. (https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_MessageSend)

Add whatever other steps you want 302 to do (bridge a call, play an announcement, etc.)

Do another MessageSend() before hanging up or on the h extension. (https://wiki.asterisk.org/wiki/display/AST/Special+Dialplan+Extensions)

If you don’t want to do a SIP MESSAGE but instead some other kind of socket, you can call your python script from the dialplan instead (https://wiki.asterisk.org/wiki/display/AST/Asterisk+16+Application_System).


(D Jr W) #5

I’m using FreePBX so I can’t create dialplans.

OK how do I run Python module when a number is dialed?


#6

In FreePBX, any custom dialplan you want to create can be done in the file /etc/asterisk/extensions_custom.conf

From there, you can create or extend the context [from-internal-custom] to create custom dialplan that is available from FreePBX phones/extensions.

There is nothing built into the web interface to allow you to run custom Python modules. The “custom” file and likewise the “custom” contexts in the dialplan are there to allow you to add this kind of advanced scripting.