编程与C#中的CUCM通信

时间:2018-01-09 11:36:08

标签: c# .net cisco tapi cisco-jtapi

我想捕获对客户端的传入呼叫并使用调用者ID来查询Microsoft动态CRM。我花了很多时间找到与思科服务器联系的最佳解决方案。有很多语言,如SIP,TAPI和JTAPI。但是我没有用C#编写这个项目的代码。有没有办法用.Net库来获取此事件?

1 个答案:

答案 0 :(得分:0)

您可以使用TAPI3Lib.dll 并且您必须安装cisco tsp并配置

这是代码

public partial class Form1 : Form
    {
        TAPIClass _tapi;
        ITAddress _line;
        int _cn;

     public Form1()
        {
            InitializeComponent();

            _tapi = new TAPIClass();
            _tapi.Initialize();
            foreach (ITAddress ad in (ITCollection)_tapi.Addresses)
            {
                if (ad.AddressName.StartsWith("Cisco"))
                    cbLines.Items.Add(ad.AddressName);
            }

            if (cbLines.Items.Count == 0) return;

            _tapi.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION |
                                    TAPI_EVENT.TE_CALLINFOCHANGE |
                                    TAPI_EVENT.TE_DIGITEVENT |
                                    TAPI_EVENT.TE_PHONEEVENT |
                                    TAPI_EVENT.TE_CALLSTATE |
                                    TAPI_EVENT.TE_GENERATEEVENT |
                                    TAPI_EVENT.TE_GATHERDIGITS |
                                    TAPI_EVENT.TE_REQUEST);
            _tapi.ITTAPIEventNotification_Event_Event += tapi_ITTAPIEventNotification_Event_Event;
            cbLines.SelectedIndex = 0;

            button1_Click(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_line != null)
            {
                _line = null;
                if (_cn != 0) _tapi.UnregisterNotifications(_cn);
            }

            foreach (ITAddress ad in (ITCollection)_tapi.Addresses)
            {
                if (ad.AddressName == cbLines.Text)
                {
                    _line = ad;
                    break;
                }
            }
            if (_line != null)
            {
                _cn = _tapi.RegisterCallNotifications(_line, true, true, TapiConstants.TAPIMEDIATYPE_AUDIO, 2);
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (_cn != 0) _tapi.UnregisterNotifications(_cn);
        }

        delegate void AddLogDelegate(string text);
        private void AddLog(string text)
        {
            if (InvokeRequired)
            {
                Invoke(new AddLogDelegate(AddLog), text);
            }
            listBox1.Items.Insert(0, text);
        }

        private void tapi_ITTAPIEventNotification_Event_Event(TAPI_EVENT tapiEvent, object pEvent)
        {
            try
            {
                switch (tapiEvent)
                {
                    case TAPI_EVENT.TE_CALLSTATE:
                        var cn2 = pEvent as ITCallStateEvent;
                        switch (cn2?.Call.CallState)
                        {
                            case CALL_STATE.CS_OFFERING:
                                var c = cn2.Call.CallInfoString[CALLINFO_STRING.CIS_CALLERIDNUMBER];
                                AddLog("Call Offering: " + c + " -> " + cn2.Call.Address.DialableAddress);
                                break;
                            case CALL_STATE.CS_CONNECTED:
                                AddLog("Call Started at " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.FFF"));
                                break;
                            case CALL_STATE.CS_IDLE:
                                break;
                            case CALL_STATE.CS_INPROGRESS:
                                AddLog("Outgoing Call: from " + cn2.Call.Address.DialableAddress + " to " + teNumber.Text + " at " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.FFF"));
                                break;
                            case CALL_STATE.CS_DISCONNECTED:
                                AddLog("Call Finished at " + DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.FFF"));
                                break;
                            case CALL_STATE.CS_HOLD:
                                break;
                            case CALL_STATE.CS_QUEUED:
                                break;
                            case null:
                                break;
                            default:
                                throw new ArgumentOutOfRangeException();
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (_line == null) return; 
             var bc = _line.CreateCall(teNumber.Text, TapiConstants.LINEADDRESSTYPE_PHONENUMBER, TapiConstants.TAPIMEDIATYPE_AUDIO);
            bc.Connect(false);
        }
    }