使用C#TAPI的呼叫监视器

时间:2018-10-17 21:52:00

标签: c# .net telephony tapi netapi32

我需要创建一个程序来监视电话活动,并获取有关电话的信息,例如电话号码和姓名。 我对TAPI代码和C#不太满意,所以希望有人能帮助我,我很绝望。

我有这段代码,试图在出现呼叫时检测可用设备并从这些设备中获取信息:

using System;
using TAPI3Lib; 
using JulMar.Atapi; 

namespace ConsoleApp1
{
  class Program
  {
    private void tapi_ITTAPIEventNotification_Event(TAPI_EVENT TapiEvent, object pEvent)
    {
        try
        {
            ITCallNotificationEvent cn = pEvent as ITCallNotificationEvent;
            if(cn.Call.CallState == CALL_STATE.CS_CONNECTED)
            {
                string calledidname = cn.Call.get_CallInfoString(CALLINFO_STRING.CIS_CALLEDIDNAME);
                Console.WriteLine("Called ID Name " + calledidname);
                string callednumber = cn.Call.get_CallInfoString(CALLINFO_STRING.CIS_CALLEDIDNUMBER);
                Console.WriteLine("Called Number " + callednumber);
                string calleridname = cn.Call.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNAME);
                Console.WriteLine("Caller ID Name " + calleridname);
                string callernumber = cn.Call.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNUMBER);
                Console.WriteLine("Caller Number " + callernumber);


            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }



    static void Main(string[] args)
    {
        TapiManager mgr = new TapiManager("ConsoleApp1");
        mgr.Initialize();

        foreach(TapiLine line in mgr.Lines)
        {
            foreach (string s in line.Capabilities.AvailableDeviceClasses)
                Console.WriteLine("{0} - {1}", line.Name, s);
        }
    }
}
}

但是,当我运行它时,只看到可用的设备,却看不到有关呼叫的任何信息。我习惯于用Java进行编程,所以我想我应该发送以调用在主体中获取调用信息的方法,但是我不知道该怎么做,而且我不知道它们执行的任何代码。 因此,希望您能帮助我了解TAPI的工作原理以及如何做我的代码。

2 个答案:

答案 0 :(得分:0)

该行是TapiLine,您必须使用TapiCall

答案 1 :(得分:0)

好的,首先,您要坚持使用一个版本的TAPI。在您的using语句中,您要导入TAPI 2.x托管的libaray和TAPI 3.x托管的库。

using TAPI3Lib; // this is a TAPI 3.x library
using JulMar.Atapi; // this is a TAPI 2.x library

如果您选择使用TAPI 3.x,则应从创建一个新类开始,该类可以处理各种TAPI事件。为此,它需要实现ITTAPIEventNotification接口:

public class CallNotification : ITTAPIEventNotification
{
    public void Event(TAPI_EVENT TapiEvent, object pEvent)
    {
        if(pEvent == null)
            throw new ArgumentNullException(nameof(pEvent));

        switch (TapiEvent)
        {
            case TAPI_EVENT.TE_CALLNOTIFICATION:
                // This event will be raised every time a new call is created on an monitored line-
                // You can use CALLINFO_LONG.CIL_ORIGIN to see weather it's an inbound call, or an
                // outbound call.
                break;
            case TAPI_EVENT.TE_CALLSTATE:
                // This event will be raised every time the state of a call on one of your monitored
                // Lines changes.
                // If you'd want to read information about a call, you can do it here:
                ITCallStateEvent callStateEvent = (ITCallStateEvent)pEvent;
                ITCallInfo call = callStateEvent.Call;

                string calledidname = call.get_CallInfoString(CALLINFO_STRING.CIS_CALLEDIDNAME);
                Console.WriteLine("Called ID Name " + calledidname);

                string callednumber = call.get_CallInfoString(CALLINFO_STRING.CIS_CALLEDIDNUMBER);
                Console.WriteLine("Called Number " + callednumber);

                string calleridname = call.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNAME);
                Console.WriteLine("Caller ID Name " + calleridname);

                string callernumber = call.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNUMBER);
                Console.WriteLine("Caller Number " + callernumber);
                break;
        }

        // Since you're working with COM objects, you should release any used references.
        Marshal.ReleaseComObject(pEvent); 
    }
}

要使用此类,您需要创建TAPI3Lib.TAPIClass的新实例并调用其Initialize方法。之后,您可以将新创建​​的CallNotification类附加为事件处理程序。您还可以指定您的处理程序要接收的事件类型。请注意,此时您将不会收到任何事件通知,因为您尚未告诉TAPIClass它应监视的行:

CallNotification callevent = new CallNotification();
TAPIClass tapi = new TAPIClass();
tapi.Initialize();
tapi.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION | TAPI_EVENT.TE_CALLSTATE);
tapi.ITTAPIEventNotification_Event_Event += new ITTAPIEventNotification_EventEventHandler(callevent.Event);

为了告诉TAPIClass应该监视哪些行,您需要做两件事。询问所有注册到您的IPBX的线路,并确定您有权监视的线路(这是IPBX配置):

public List<ITAddress> EnumerateLines(TAPIClass tapi)
{
    List<ITAddress> addresses = new List<ITAddress>();

    ITAddress address;
    uint arg = 0;

    ITAddressCapabilities addressCapabilities;
    int callfeatures;
    int linefeatures;
    bool hasCallFeaturesDial;
    bool hasLineFeaturesMakeCall;

    IEnumAddress ea = tapi.EnumerateAddresses();

    do
    {
        ea.Next(1, out address, ref arg);

        if (address != null)
        {
            addressCapabilities = (ITAddressCapabilities)address;

            callfeatures = addressCapabilities.get_AddressCapability(ADDRESS_CAPABILITY.AC_CALLFEATURES1);
            linefeatures = addressCapabilities.get_AddressCapability(ADDRESS_CAPABILITY.AC_LINEFEATURES);

            hasCallFeaturesDial = (callfeatures1 & (int)0x00000040) != 0; //Contains LineCallFeatures Dial; see Tapi.h for details
            hasLineFeaturesMakeCall = (linefeatures & (int)0x00000008) != 0; //Contains LineFeatures MakeCall; see Tapi.h for details

            // this is basically saying "Am I allowed to dial numbers and create calls on this specific line?"
            if(hasCallFeaturesDial && hasLineFeaturesMakeCall)
                address.Add(address);
        }
    } while (address != null);

    return addresses;
}

public void RegisterLines(TAPIClass tapi, IEnumerable<ITAddress> addresses)
{
    if (tapi == null)
        throw new ArgumentNullException(nameof(tapi));

    if (addresses == null)
        throw new ArgumentNullException(nameof(addresses));

    foreach (ITAddress address in addresses)
    {
        tapi.RegisterCallNotifications(address, true, true, TapiConstants.TAPIMEDIATYPE_AUDIO, 2);
    }
}

所以您的初始化看起来像这样:

CallNotification callevent = new CallNotification();
TAPIClass tapi = new TAPIClass();
tapi.Initialize();

IEnumerable<ITAddress> addresses = this.EnumerateLines(tapi);
this.RegisterLines(tapi, addresses);

tapi.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION | TAPI_EVENT.TE_CALLSTATE);
tapi.ITTAPIEventNotification_Event_Event += new 
ITTAPIEventNotification_EventEventHandler(callevent.Event);

一旦运行了程序,并且执行了上面的代码,当它们的呼叫状态更改时,您将收到来自传入和传出呼叫的通知。

希望您可以关注这篇文章。如有任何疑问,请问=)