Thursday, March 22, 2012

Asterisk 10 (1.10) SMS (messaging or SIP Messaging) in action



[UPDATE: WORKS FLAWLESSLY WITH ASTERISK 11 TOO…On Asterisk 11, you can choose XMPP as well as the protocol, right now its just SIP:, imagine the possibilities. Also fixed the issue when failed message]

[ANOTHER UPDATE FOR Offline message - We all know when devices are offline, messages cannot be sent and it will be discarded by Asterisk. I've written another dialplan/solution to that here

With Asterisk 10 comes a channel independent dialplan for handling SIP MESSAGING (and jabber if that’s configured) method. In other words, phones that support SIP Messaging can send and receive “SMSes” through Asterisk 10 without the need to dial a channel making it like a regular telco SMS. Older Asterisks requires you to dial and answer a channel before you can send messages (see here).

It worked for me well in Asterisk 10.2.1 and this guide takes you through the process of using SMSing with a dialplan in a four step setup below. Asterisk uses the Message/ast_msg_queue channel to do all SIP Method MESSAGE related processing.

I was using FreePBX 2.10 but I guess it doesn’t quite matter as I am using a custom context anyway.

1) You need to modify your SIP general settings in sip.conf [freepbx users use the SIP Settings in Web GUI and add inside “Other SIP Settings”] add these two lines.

Non FreePBX users, edit sip.conf, under general
accept_outofcall_message=yes
outofcall_message_context=astsms


Save and exit.

We will now create the dialplan inside context astsms [FreePBX users edit the extensions_custom.conf file and put these in]

2) Non freepbx users, edit your extensions.conf file add the following
[astsms]
exten => _.,1,NoOp(SMS receiving dialplan invoked)
exten => _.,n,NoOp(To ${MESSAGE(to)})
exten => _.,n,NoOp(From ${MESSAGE(from)})
exten => _.,n,NoOp(Body ${MESSAGE(body)})
exten => _.,n,Set(ACTUALTO=${CUT(MESSAGE(to),@,1)})
exten => _.,n,MessageSend(${ACTUALTO},${MESSAGE(from)})
exten => _.,n,NoOp(Send status is ${MESSAGE_SEND_STATUS})
exten => _.,n,GotoIf($["${MESSAGE_SEND_STATUS}" != "SUCCESS"]?sendfailedmsg)

exten => _.,n,Hangup()
;
; Handle failed messaging
exten => _.,n(sendfailedmsg),Set(MESSAGE(body)="[${STRFTIME(${EPOCH},,%d%m%Y-%H:%M:%S)}] Your message to ${EXTEN} has failed. Retry later.")
exten => _.,n,Set(ME_1=${CUT(MESSAGE(from),<,2)})
exten => _.,n,Set(ACTUALFROM=${CUT(ME_1,@,1)})
exten => _.,n,MessageSend(${ACTUALFROM},ServiceCenter)
exten => _.,n,Hangup()

exten => _.,n,Hangup()

The above dialplan also send a little error message if the SMS didn’t send out saying its from “ServiceCenter”…

3) Save and exit and reload asterisk. [FreePBX users do #amportal restart]. Send SMSes! Yeah! But make sure you use a phone that supports SIP SMS.
IMPORTANT: In the SMS message center settings in your phone, enter the IP address of your Asterisk Server. In my case, I was using Unidata WiFi phone. As usual, do let us know the outcome and using other phones.
Here’s a message I got using the phone Smile

Confirmed working with:
1) Unidata phones
2) Yealink phones (with SMS capabilities)
3) CSIPSimple on Andriod
4) X-Lite softphone
5) Most phones that support SIP Messaging

camera (1)

49 comments:

Anonymous said...

i have done changes as describe above but there is no message come to user,only i can send message from sip user to asterisk but cant get back from asterisk to sip user.please can you help me..

JayWS said...

Can you email your dialplan to me sanjayws@hotmail.com and your logs when you fire sms-es both ways?

Amal said...

There seems to be a lot of confusion over SMS and SIP, and I would like you to clarify for me that this is "text messaging" in as much as handling SIP MESSAGE packets, and NOT SMS (as defined by GSM standard). Am I correct here? If so, I would kindly suggest that you update the blog syntax to be clearer about that fact, because I've spent several days chasing ghosts, trying to find "SMS" modules and code for Asterisk/FreePBX when it truly is not the same thing as SIP MESSAGE handling.

JayWS said...

Amal,

I take SMS as Short Messaging Service, be it via SIP, jabber, or what have you.

Anyway, its absolutely easy to integrate Asterisk 10 with GSM SMS too :)...example, you can jump out of the dialplan

BTW for fun sake..

Here's what Wiki says:
Short Message Service (SMS) is a text messaging service component of phone, web, or mobile communication systems, using standardized communications protocols that allow the exchange of short text messages between fixed line or mobile phone devices.

Moses Kurniawan said...

Oh thanks! I was screwed over a week to make softphone and ipphone can talk each other! I've been installing openfire etc and googling with no success...but this is the real solution! Thanks! *uninstalling openfire*

Moses Kurniawan said...

Oh thanks! I was screwed over a week to make softphone and ipphone can talk each other! I've been installing openfire etc and googling with no success...but this is the real solution! Thanks! *uninstalling openfire*

Admin said...

Hey openfire works great man, esp to those clients that do not support inline sip messaging. I've integrated jabber to asterisk and back :) works a treat!

anyway, whatever makes you happy, glad it worked for you.

Unknown said...

Hi and thank you for the precious infos.
I followed them and sms are working, but only if the receiver is connected.
I have freepbx 2.10 and asterisk 1.8.
Is this the way it is supposed to work or is it because i have asterisk 1.8 and not 1.10?

JayWS said...

Michele, you need Ast 10

Unknown said...

i installed it... same behaviour: only on line sms... any hint?

Unknown said...

Hi Sanjay,

Thanks for this configuration. I have tried it on my ast10, and I have the same issue as Michele.

SMS are only received when the receiver is connected. And I have the error :

[Aug 9 11:32:51] NOTICE[8948]: pbx.c:4440 pbx_extension_helper: No such label 'sendfailedmsg' in extension '2' in context 'astsms'
[Aug 9 11:32:51] WARNING[8948]: pbx.c:10775 pbx_parseable_goto: Priority 'sendfailedmsg' must be a number > 0, or valid label

Here the user '1' tried to send a SMS '2' which is offline.

Is there a way to received the SMS when the user '2' becomes online again ?

Regards,

Unknown said...

Hi Sanjay,

I have the same issue as michele ?

Did you get my previous message ?

Regards,

JayWS said...

Hi guys

For channel independent SMSing, you need Asterisk 10. Are you sure you're using that?

Unknown said...

Hi Sanjay,

Yes I am using ast 10 :

*CLI> core show version
Asterisk 10.7.0 built by root @ phone on a i686 running Linux on 2012-07-31 08:42:48 UTC

:)

Product Reviews said...

Hi Sanjay,

Thanks for this perfect howto. It works like a charm, I've already implemented this on many servers.

I do however have some questions.

- Would you happen to know how to customize the dialplan to force the users clid? At the moment a user can fill-in anything in the clid field on his client which makes it very easy to steal identity.

- Also would it be possible as other have already asked before me to built some sort of messaging queue for when a user is offline? Have asterisk store the message somewhere until the user becomes available, or retry to resend it every so hours and delete the message after lets say a week.

I have no idea if this is at all possible, but it seems to me that if someone knows it's gotta be you. I wish I was more of a asterisk dialplan hero like you seem to be. :(

Cheers, and thanks so far.

R. Roeleveld.

Anonymous said...

Thank you for your post. But I get an error when sending message to offline user. Comment line 10 in dialplan
exten => h,1,Hangup()
and I noticed when user didn't get message.

Rconnect said...

Hi Sanjay, I would like to know as well, if it is possible to have offline SMSs. thank you

Rconnect said...

Hi,

Thanks for this great HowTo. Like other have asked, is possible to have an offline queue? so when a user logins into the SIP server the message his sent. thank you.

JayWS said...

Hi guys, only thing i can think of is to intercept the status message if not sent, then keep the message in a database or a text file somewhere. Then write a script or daemon that will process these files periodically by first checking if the sip peer is alive (status OK) then send the message by invoking asterisk file based dialing.

Not impossible, but takes a little time to do it :)

Steel Reign said...

Found a little bug in the system.

When send a text message that is alpha numeric, it does not the numbers if it is 3 numbers or less after a space.

Example: text 1, text 12, text 123
the recipient will see the text but not the numbers

Example: Sender-text 123
Receiver-text

Example: text 1234 or greater works fine.

Steel Reign said...

I have found to problems/bugs with this. First let me explain my setup.

-Asterisk 10.10.0 built by root @ jenkins6.schmoozecom.net on a x86_64 running CentOS 6.2

-X-lite 4 on PC

-Bria for iPhone

First problem/bug: Unable to send SMS message from iPhone over 3g network to PC on LAN, but I can send from PC to iPhone

Second Can not send test SMS with anything lets then 4 digits after text a space.

Example: text 1, text 12, text 123 all fail to send.

Even though everything looks ok:
-- Executing [2000@astsms:1] NoOp("Message/ast_msg_queue", "SMS receiving dialplan invoked") in new stack
-- Executing [2000@astsms:2] NoOp("Message/ast_msg_queue", "To sip:2000@mydomain.net") in new stack
-- Executing [2000@astsms:3] NoOp("Message/ast_msg_queue", "From "Steel Reign" ") in new stack
-- Executing [2000@astsms:4] NoOp("Message/ast_msg_queue", "Body test ") in new stack
-- Executing [2000@astsms:5] Set("Message/ast_msg_queue", "ACTUALTO=sip:2000") in new stack
-- Executing [2000@astsms:6] MessageSend("Message/ast_msg_queue", "sip:2000,"Steel Reign" ") in new stack
-- Executing [2000@astsms:7] NoOp("Message/ast_msg_queue", "Send status is SUCCESS") in new stack
-- Executing [2000@astsms:8] GotoIf("Message/ast_msg_queue", "0?sendfailedmsg") in new stack
-- Executing [2000@astsms:9] Hangup("Message/ast_msg_queue", "") in new stack
== Spawn extension (astsms, 2000, 9) exited non-zero on 'Message/ast_msg_queue'


text 1234 and greater works perfectly.

JayWS said...

The sendfailedmsg thingybob can be fixed if you remove the h, priority. The new GotoIf checkouts for h and i priorities and goes there and does stuff there, which is pretty neat, should actually update that dialplan. So yea, just remove the 1st h, priority (already updated the dialplan above)

same60 said...

First of all, great work Sanjay, thank you so much.
2nd point:
"Hi guys, only thing i can think of is to intercept the status message if not sent, then keep the message in a database or a text file somewhere. Then write a script or daemon that will process these files periodically by first checking if the sip peer is alive (status OK) then send the message by invoking asterisk file based dialing.

Not impossible, but takes a little time to do it :)"

Im stuck on this point. I already implemented the local db who store messages if the peer im contacting is unable to get the text.
I'm looking for a solution to send the stored messages once the peer is back online, all automated of course: it means i should detect the registrar instance and send to that peer his stored message in queue. I already implementing that queue, so basically i only need to fix this part.
Any hint?

Admin said...

Hi guys, try this for offline.

http://highsecurity.blogspot.com/2013/01/asterisk-10-or-11-messaging-smssip.html

Unknown said...

I cannot get this to work. I am using Ubuntu 12.04 LTS with Asterisk 11. I performed step 1,2,and 3. Still no luck. What is funny is the handsets send text without the PBX, once SIP is registered I can make calls but SMS fails.

Unknown said...

I have followed the steps above and still do not have SMS. However, when my phones are disconnected from the PBX but connected to the network they send SMS perfectly with no server. Once SIP registers I can make calls but no SMS. Any ideas? I can provide my .conf files.

JayWS said...

Yeah, send your configs in pastebin also the log when you try to sms. CHeers

Anonymous said...

Hi,

Can i manualy run the sh file?
I think that is not producing the file.

I' trying ./astqueue.sh with the parameters and i'm getting permicion denied.

Thank you.

JayWS said...

Run

chmod +x astqueue.sh

You can run but it will.prob do nothing much

Unknown said...

Thank you for the response and interest.

Sip.conf - http://pastebin.com/PSRnqNqL

Extensions.conf - http://pastebin.com/2beREFg0

Unknown said...

Also here is a picture of a PCAP capture showing the message sending to my server from handset 6000 to 6001 and the server rejecting
http://oi46.tinypic.com/28h0j2a.jpg

I do not know how to view the logs on the PBX I can get to the CLI. I use the graphical interface for everything and text editor for making changes to .conf files.

again thank you.

JayWS said...

Justin

Can the two phones call each other to begin with?

Check the logs by doing

tail -f /var/log/asterisk/full

Put that in pastebin too

Unknown said...

I wasn't able to gather that file but I was able to copy the CLI logs during the handoffs. I am able to login with SIP and initiate a phone call.

If you look you will see the PBX responds with 401 Unauthorized when I am trying to send a message.

This is the same message I receive when doing a PCAP capture.

http://pastebin.com/q7DMb0tH

I still feel like my configs are messed up somewhere? Thank you very much for your help.

JayWS said...

SIP/2.0 401 Unauthorized

That can be normal.

I need the logs, otherwise, sip traces dont tell a thing...

Unknown said...

I'm not finding the "full" log file. I have messages, queue_log and h323_log?

I have verified that messages are enabled via CLI>logger show channels

Here is the pastebin for messages.conf
http://pastebin.com/a92zL50g

JayWS said...

Justin

Edit
/etc/asterisk/logger.conf

Add this line
full => notice,warning,error,debug,verbose

Stop and restart asterisk, your full log will be there!

Mochammad Effendi said...

Dear Sanjay,

I use softphone client Jitsi but send message is always disable

But if i use 3CXPhone it is enable

Please help

Anonymous said...

Asterisnnow 3.0 with Freepbx or Pbxinaflash

This works.
Just copy and paste.

Frank said...

Hi folks,

I just tried this between 2 Ekiga clients and the Windows Ekiga doesn't send Authorization header for the MESSAGE packets while the linux version does.
Result: from the linux client you can send messages from the windows you cannot...
This is the header what's missing at the windows client:

Authorization: Digest username="99", realm="asterisk", nonce="18863da3", uri="sip:98@192.168.1.2", algorithm=MD5, response="..."


QA said...

send text messages to india
TelCan offers so many features in US to India calling including the ability to cheapest calls to india to any mobile phone. The best calling cards TO calls from uk to india .
phone in india

Unknown said...

Hi all,


I am trying to enable SIP SIMPLE communication in my test environment.

I have the following env :

- one server (192.168.50.126) with Asterisk 11
- 2 clients using pidgin : demo-bob and demo-alice on my 192.168.50.143

I successfully had a phone call between clients.


Both users managed to register.

Adding verbose on the server, I have the following traces when I send the message "MESSAGE FROM ALICE TO BOB" from "demo-alice" to "demo-bob"

http://paste.fedoraproject.org/19489/37158861/

As you can see I succeed to have the message sent from alice to Asterisk.

When the server is trying to transmitting, I see a 401 error message. According to this post (http://forums.digium.com/viewtopic.php?f=1&t=72814) the first 401 should be normal as authentication is requested.

Afterwards the server emit 202 message.

But "demo-bob" never receives a message.
I ran wireshark on server and client. It confirms that no message is sent from Asterisk to "demo-bob".

I never have the trace NoOp added in messages-sip context. However if I add it in users context and performed a call between alice and bob, it works.

Could you please give me advice ?


Here are my extensions.conf and sip.conf according to the link I mentioned.
http://paste.fedoraproject.org/19626/16493741/
http://paste.fedoraproject.org/19627/49423137/


Thanks a lot,

Eloi

Unknown said...

Hi all,


I am trying to enable SIP SIMPLE communication in my test environment.

I have the following env :

- one server (192.168.50.126) with Asterisk 11
- 2 clients using pidgin : demo-bob and demo-alice on my 192.168.50.143

I successfully had a phone call between clients.

I used the following link to enable SIMPLE messaging between my clients : http://highsecurity.blogspot.ca/2012/03/asterisk-10-110-sms-messaging-or-sip.html

Both users managed to register.

Adding verbose on the server, I have the following traces when I send the message "MESSAGE FROM ALICE TO BOB" from "demo-alice" to "demo-bob"

http://paste.fedoraproject.org/19489/37158861/

As you can see I succeed to have the message sent from alice to Asterisk.

When the server is trying to transmitting, I see a 401 error message. According to this post (http://forums.digium.com/viewtopic.php?f=1&t=72814) the first 401 should be normal as authentication is requested.

Afterwards the server emit 202 message.

But "demo-bob" never receives a message.
I ran wireshark on server and client. It confirms that no message is sent from Asterisk to "demo-bob".

Adding NoOp traces in sip-messages context I see that I never go into it while it works when adding traces in users context during a call.

Could you please give me advice ?


Here are my extensions.conf and sip.conf according to the link I mentioned.
http://paste.fedoraproject.org/19626/16493741/
http://paste.fedoraproject.org/19627/49423137/


Thanks a lot,

Eloi

Yair Magana said...

Did I have to open some ports on my Firewall ?? I try on my local network and works find, when Im outside i have a Message error "Service Unavailable (503)" i make calls without any problems.

Thanks

Anonymous said...

can u plz mail me the procedure how to create extensions in X-lite,register the IP address of asterisk server and how to send SMS to asterisk from X_lite SIP phone

mikeisfly said...

Thanks for the great article worked like a charm. I'm using Freepbx 5.211.65-12 and Asterisk 11.9. My question is I know you outlined how to store a failed message to then send it when the phone comes back on line. Have you implemented it? If so can you submit additional information to this guide on how to do so? If you have not, can I donate $50 for your time to do it.

Thanks for the great work.

JayWS said...

Those want offline try this: http://highsecurity.blogspot.com/2013/01/asterisk-10-or-11-messaging-smssip.html

Chris said...

Hi,

I'm running the FreePBX distro and cannot get it to work. I think it is related to the deviceanduser setup from FreePBX which makes it possible to use multiple devices for one user. This is the relevant log when I sent a message:

[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:1] NoOp("Message/ast_msg_queue", "SMS receiving dialplan invoked") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:2] NoOp("Message/ast_msg_queue", "To sip:3344@172.16.42.119") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:3] NoOp("Message/ast_msg_queue", "From "3" ") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:4] NoOp("Message/ast_msg_queue", "Body Test 2") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:5] Set("Message/ast_msg_queue", "ACTUALTO=sip:3344") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:6] MessageSend("Message/ast_msg_queue", "sip:3344,"3" ") in new stack
[2014-06-30 15:16:32] WARNING[2716][C-00000001] chan_sip.c: Purely numeric hostname (3344), and not a peer--rejecting!
[2014-06-30 15:16:32] WARNING[2716][C-00000001] chan_sip.c: Purely numeric hostname (3344), and not a peer--rejecting!
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:7] NoOp("Message/ast_msg_queue", "Send status is FAILURE") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:8] GotoIf("Message/ast_msg_queue", "1?sendfailedmsg") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Goto (astsms,3344,10)
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:10] Set("Message/ast_msg_queue", "MESSAGE(body)="[30062014-15:16:32] Your message to 3344 has failed. Retry later."") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:11] Set("Message/ast_msg_queue", "ME_1=sip:3@172.16.42.119>") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:12] Set("Message/ast_msg_queue", "ACTUALFROM=sip:3") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:13] MessageSend("Message/ast_msg_queue", "sip:3,ServiceCenter") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: -- Executing [3344@astsms:14] Hangup("Message/ast_msg_queue", "") in new stack
[2014-06-30 15:16:32] VERBOSE[2716][C-00000001] pbx.c: == Spawn extension (astsms, 3344, 14) exited non-zero on 'Message/ast_msg_queue'

Any ideas how to get this working with this setup?

FreePBX release 6.5
Asterisk 11.10.2
deviceanduser mode enabled in Advanced Settings.

Thanks,
Chris

pradeep said...


hi i am getting this error can you p[lease tell me y this happening??


Executing [7002@msgcontext:11] Set("Message/ast_msg_queue", "ME_1=") in new stack
[Feb 19 20:43:48] ERROR[10799][C-00000001]: pbx_functions.c:608 ast_func_read: Function CUT not registered

Pradeep said...

[Feb 19 20:43:48] ERROR[10799][C-00000001]: pbx_functions.c:608 ast_func_read: Function CUT not registered

unable to clear this error can anyone please help me?