码迷,mamicode.com
首页 > 其他好文 > 详细

XMPP and Asterisk integration

时间:2015-07-17 16:01:00      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

http://www.mundoopensource.com.br/en_page_xmpp_asterisk_pratical_example/

www.mundoopensource.com.br/xmpp-asterisk-integration-practical-example-part-2/

 

 

In past few years integrate XMPP and Asterisk is one of my principal goals. The development of an realtime communication tool with open source software necessarily needs this two technologies and make it happens is one of my intents.

Nine years ago, since I started to work with Asterisk and Openfire I’m studying a way to do it and when I discovered that Asterisk 11 had improved xmpp support, I noticed that it was the time to make this real.

Using AGIs scripts in python, I started the development of a “tool” that allows users to run commands through their xmpp account in their preferred xmpp client (Pidgin, for example). The current version of my software permits:

    Send SIP messages to an extension
    Send text messages (SMS)
    Enable/disable DND for extension
    Enable/disable divert for extension
    Queue logon/logoff
    Show extension informations
    Show queue informations
    Send XMPP message to all extensions logged in a queue
    Show SIP and IAX peers informations
    And other resources under development

For it works you’ll need to create a column in your SIP table to store the extension’s JID. I created a column named JID in my sippeers table.

Now I’ll show you a practical example of this integration.

What do you need?

You’ll need this following items:

    A running Asterisk 11 (at least) with res_xmpp support configured.
    A running XMPP server (Openfire 3.9.3 is a good one)
    A JID account for your Asterisk (to use in res_xmpp)

And now ?

First you need to make Asterisk works with your XMPP server. Edit /etc/asterisk/xmpp.conf and create a sessions for you server. Example:

[asterisk]
type=client
serverhost=yourjabberdomain
username=asterisk@yourjabberdomain
secret=password
priority=1
usetls=yes
port=5222
usesasl=yes
status=available
statusmessage="Asterisk Server"
endtodialplan=yes
context=from_xmpp
keepalive=yes

After, edit /etc/asterisk/extensions.conf and create  from_xmpp context with the following content:

[from_xmpp]

exten => s,1,NoOp(XMPP Message)
same => n,NoOp(To: ${MESSAGE(to)})
same => n,NoOp(From: ${MESSAGE(from)})
same => n,NoOp(Message: ${MESSAGE(body)})

same => n,NoOp(Sending SIP message)

; AGI that process message and get the following variables
; ${XMPP_MSG} - message text
; ${XMPP_SIPDEST} - to extension
; ${XMPP_SIPORIG}  - from extension
same => n,agi(process_xmpp_msg.agi)

; Set ${XMPPRESOURCE} = session name in xmpp.conf file (can be a global variable)
same => n,Set(XMPPRESOURCE=asterisk)

same => n,Set(MESSAGE(body)=${XMPP_MSG})
same => n,MessageSend(sip:${XMPP_SIPDEST},sip:${XMPP_SIPORIG})
same => n,JabberSend(${XMPPRESOURCE},${MESSAGE(from):5},"Message sent. Status:${MESSAGE_SEND_STATUS}). )

How to use it?

To  “communicate” with your Asterisk server just send messages to Asterisk JID configured in your xmpp.conf file (in this example, asterisk@yourjabberdomain). The message will be received in xmpp context (from_xmpp context) and stored in  ${MESSAGE} variable. The NoOP commands in the example context are used just to show you  how Asterisk processes the message.

UPDATE: some readers ask me to send my process_xmpp_msg.agi. You can get it here. It was adapted from my original script, so I hope it is working. ??

Obs: this post is intended just to discuss XMPP and Asterisk integration so I didn’t list the process_xmpp_msg.agi script contents, because it’s basically used to process the message and search data in DB.

Examples and logs

Message sent(Pidgin):

    (02:53:00 PM) marcelo@xmpp.meudominio.com.br/meuresource: msg 5010 Isso é um teste
    (02:53:01 PM) asterisk@xmpp.meudominio.com.br: Message sent. Status: SUCCESS.

Asterisk console:

    — Executing [s@from_xmpp:1] NoOp(“Message/ast_msg_queue”, “XMPP Message”) in new stack
    — Executing [s@from_xmpp:2] NoOp(“Message/ast_msg_queue”, “To: xmpp:asterisk@xmpp.meudominio.com.br”) in new stack
    — Executing [s@from_xmpp:3] NoOp(“Message/ast_msg_queue”, “From: xmpp:marcelo@xmpp.meudominio.com.br/meuresource”) in new stack
    — Executing [s@from_xmpp:4] NoOp(“Message/ast_msg_queue”, “Message: msg 5010 Isso é um teste”) in new stack
    — Executing [s@from_xmpp:10] NoOp(“Message/ast_msg_queue”, “Sending SIP message”) in new stack
    — Executing [s@from_xmpp:11] Set(“Message/ast_msg_queue”, “MESSAGE(body)=Isso é um teste”) in new stack
    — Executing [s@from_xmpp:12] MessageSend(“Message/ast_msg_queue”, “sip:5010,sip:5099″) in new stack
    — Executing [s@from_xmpp:15] JabberSend(“Message/ast_msg_queue”, “asterisk,marcelo@xmpp.meudominio.com.br/meuresource,Message sent. Status: SUCCESS.”) in new stack

I hope you enjoy this example. It is simple, but next I’ll post another one, that sends a XMPP message to all extensions logged in an Asterisk queue.

Any comments and suggestions, please contact me (mhterres@yahoo.com) so we could discuss more possibilities for this integration.

See you.

 

 

Continuing my posts of XMPP and VoIP (Openfire + Asterisk) integration, and after AstDemo tests, it’s time to release its source code to allow others to use its features and maybe create new commands and functionalities.

AstDemo was developed in Python and require a few libs to run (xmpppy, pyst e psycopg2). You will need a running Asterisk 11 (or later) with XMPP support and a running Openfire Server with ServerInfo Plugin too.

In this first public release, AstDemo has the following features:

  • Calls: call to a specific number and plays tt-monkeys file when call is answered. This feature demands some permissions to run. Read more in CALLS.txt file.
  • Show (basic) informations of Asterisk and Openfire servers.
  • Show SIP peers.
  • Show IAX peers.
  • Show (basic) informations of a SIP peer.
  • Enable or disable the callerid xmpp message on received calls to extension.
  • Monitor SIP or IAX peers.
  • Monitor queues.
  • Show your monitoring items.

If you have Asterisk Realtime enabled, you’ll have some extra features:

  • Show user informations.
  • Enable/disable or show status of Do Not Disturb.
  • Send a message to all members of a queue.
  • Join a queue.
  • Leave a queue.
  • Show queue informations (members and statistics).
  • Call a SIP extension and transfer it to your extension when answered.

AstDemo is available at GitHub, and I hope it helps its development, specially if more people join this project. Download AstDemo here or access project page on Github.

If you are interested in help AstDemo development, please contact me.

And, if you just want to use it, feel free, but please leave your comments and suggestion in this page.

Read AstDemo page for more informations.

See you!

XMPP and Asterisk integration

标签:

原文地址:http://www.cnblogs.com/welhzh/p/4654646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!