Send SMS message to IP when ext is dialed using sockets

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

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?

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.

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 (Home - Asterisk Documentation), define exten 302 to do a MessageSend() pointed at your recipient. (Home - Asterisk Documentation)

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. (Home - Asterisk Documentation)

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 (Home - Asterisk Documentation).

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

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

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.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.