• 扩展Asterisk1.8.7的AMI接口


    AsteriskAMI接口已经很丰富了,如果需要扩展,可以参考本文。

    一、扩展AsteriskAMI接口

    涉及文件:main/manager.c

    1、函数注册

    添加 myOriginate接口

    在函数int __init_manager(int reload)中添加如下代码:

    ast_manager_register_xml("myOriginate", EVENT_FLAG_ORIGINATE,action_myOriginate); // Add by Mike

    2、功能实现:

    View Code
    //Add by Mike
    static int action_myOriginate(struct mansession *s, const struct message *m)
    {
            const char *name = astman_get_header(m, "Channel");
            const char *exten = astman_get_header(m, "Exten");
            const char *context = astman_get_header(m, "Context");
            const char *priority = astman_get_header(m, "Priority");
            const char *timeout = astman_get_header(m, "Timeout");
            const char *callerid = astman_get_header(m, "CallerID");
    
            char tmp[256];
            int i=0;
            sprintf(tmp,"%ld_%s",time(NULL),name);
            int tmpLen = strlen(tmp);
            for(i=0;i<tmpLen;++i)
            {
                    if(isalnum(tmp[i]))
                            continue;
                    tmp[i]='_';
            }
            char fileName[1024]={0};
            sprintf(fileName,"/var/spool/asterisk/outgoing/%s.call",tmp);
            // FILE *fp = fopen("/var/spool/asterisk/outgoing/1.call","w");
            FILE *fp = fopen(fileName,"w");
            char buf[1024]={0};
            sprintf(tmp,"channel: %s\r\n",name);
            strcpy(buf,tmp);
            sprintf(tmp,"Callerid: %s\r\n",callerid);
            strcat(buf,tmp);
            strcat(buf,"MaxRetries: 1\r\n");
            strcat(buf,"RetryTime: 1\r\n");
           // sprintf(tmp,"WaitTime: %s\r\n",timeout);
            sprintf(tmp,"WaitTime: %s\r\n","30");
            strcat(buf,tmp);
            sprintf(tmp,"Context: %s\r\n",context);
            strcat(buf,tmp);
            sprintf(tmp,"Extension: %s\r\n",exten);
            strcat(buf,tmp);
            sprintf(tmp,"Extension: %s\r\n",exten);
            strcat(buf,tmp);
            fwrite((void*)buf,strlen(buf),1,fp);
            fclose(fp);
            return 0;
    }

    二、配置Asterisk

    1、编译安装

    make && make install

    2、添加拨号方案

    在文件/etc/asterisk/extension.conf中加入以下代码:

    [DLPN_MeetingWithOutRecord]
    exten =>_63XX,1,Answer()
    exten =>_63XX,n,MeetMe(${EXTEN},Mw,1234)
    exten =>_63XX,n,Hangup()

    三、AMI调用测试(Python):

    View Code
    #! /usr/bin/python
    
    import socket
    
    def strLogin(usr,pwd):
            msg = "Action: login\r\n"
            msg += "Events: off\r\n"
            msg += "UserName: " + usr + "\r\n"
            msg += "Secret: " + pwd + "\r\n"        
            msg += "\r\n"
            return msg
    
    def strCall(strNum,strCallid):
            msg = "Action: myOriginate\r\n"
            msg += "Channel: SIP/"+strNum+"\r\n"
            msg += "WaitTime: 1\r\n"
            msg += "CallerId: "+strCallid+"\r\n"
            msg += "Exten: "+strCallid+"\r\n"
            msg += "Context: DLPN_MeetingWithOutRecord\r\n"
            msg += "Priority: 1\r\n"
            msg += "\r\n"
            return msg
    
    def strLogoff():
            return "Action: logoff\r\n\r\n"
    
    def strHangUp(strNum):
            msg = "Action: Hangup\r\n"
            msg += "Channel: SIP/"+strNum
            return msg
    
    def main():
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((raw_input("IP : "), 5038))
    
            if not s :
                    print "Connect fail!"
                    return
            else : print "Connect success!"
    
            strNum = raw_input("Input number to dail:")
            strCallerId = raw_input("Input caller id :")
            msg = ""
            msg += strLogin(raw_input("username :"),raw_input("password"))
            msg += strCall(strNum,strCallerId)
            msg += strLogoff()
    
            s.send(msg)
    
            while True:
                    data = s.recv(1024)
                    if not data : break
                    else :  print data
    
            s.close()
    
    if __name__ == '__main__':
            main()
            #raw_input("Press Enter to continue")

    好,就这些了,希望对你有帮助。

  • E-Mail : Mike_Zhang@live.com
  • 转载请注明出处,谢谢!
  • 相关阅读:
    内存映射mmap的几个api及其使用
    hiredis的安装
    Linux 下解压大全
    redis内存数据库C客户端hiredis API 中文说明
    C/C++使用MySQL
    搜索引擎的缓存(cache)机制
    快速排序(QuickSort)
    冒泡排序
    spring核心之AOP学习总结一
    Spring学习总结六——SpringMVC一
  • 原文地址:https://www.cnblogs.com/MikeZhang/p/asterisk187AMITest20120618.html
  • Copyright © 2020-2023  润新知