Bridge Communications

Monday, January 26, 2015

Sending DTMF in Lync Client SDK

So you call into some 800 number, or some PSTN conference bridge, and it wants you to enter your credit card number, or some conference id using your keypad.  I don't know about you but almost certainly type the wrong digit about 1 or 2 from the end and thus end up wasting time.  Did you know there is a simple way to do this in the Lync client sdk?  Today we'll write a simple piece of code that can let us paste the string into a text box, and automatically send it for us, error free.

So we make a default WPF window and in there we can add the textbox control.

                <TextBox Name="dtmfstring" Width="96" Text="1,2,3" Height="25" Margin="3" Foreground="Black" Background="WhiteSmoke"></TextBox>

Next we'll add a button.

 <Button Width="45" Height="45" Margin="3" Click="Button_Click">
                    <StackPanel HorizontalAlignment="Center">
                        <TextBlock FontSize="11" Text="Send Box" HorizontalAlignment="Center"  TextWrapping="Wrap"></TextBlock>

So a couple of housekeeping things on the back-end for the code behind.  Let's make sure we have added the Lync WPF resources, and hookup to the default running client.

using Microsoft.Lync.Controls;
using Microsoft.Lync.Internal;
using Microsoft.Lync.Controls.Internal;
using Microsoft.Lync.Model;
using Microsoft.Lync.Model.Conversation;
using Microsoft.Lync.Model.Conversation.AudioVideo;
using Microsoft.Lync.Model.Extensibility;

LyncClient lc = LyncClient.GetClient();

We will also want to grab the current conversation we are working on, so we that know which one to passing the dtmf digits to.  For the sake of simplicity I'm going to just assume there is one call.

Conversation conv;
conv = lc.ConversationManager.Conversations[0];

So once we have that done then it's just a matter of iterating through our string of dtmf digits we ant to send and sending them.  We'll do that in our button event.

private void Button_Click(object sender, RoutedEventArgs e)

   Conversation conv;
   conv = lc.ConversationManager.Conversations[0];

   foreach (char c in dtmfstring.Text) 
                var avModality = (AVModality)conv.Modalities[ModalityTypes.AudioVideo];
                var audioChannel = avModality.AudioChannel;

                avModality.AudioChannel.BeginSendDtmf(c.ToString(), myar => 

                    catch { }

                }, null);


That's it.  Pretty simple, you can see that we find the AudioVideo modality, you can't press the number keys on IM, only an audio call.  Then we isolate the Audio channel.  From there you can see we use the async Lync functions BeginSendDtmf() to make the actually key press happen in the audio stream.  You can easily tweak this to use commas and Thread.Sleep(1000) when you hit one of those, if you need pauses in your string.

As always have a great week, and thanks for checking out our blog.

Doug Routledge, C# Lync, SQL, Exchange, UC Developer