使用C#TAPI调用Monitor

问题描述 投票:0回答:1

我需要创建一个程序来监控电话呼叫的活动。并获取有关呼叫的信息,如号码和姓名。我对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的工作原理以及我可以做些什么来完成我的代码工作。

c# .net telephony tapi netapi32
1个回答
0
投票

这条线是TapiLine,你必须使用TapiCall


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);

一旦你运行你的程序,它已经完成上面的代码执行,你将在他们的呼叫状态改变时从来电和拨出电话获得通知。

我希望你能听完这篇文章。如果您有任何疑问,请问=)

© www.soinside.com 2019 - 2024. All rights reserved.