SfB Translator using Azure + Lync SDK + Powershell

———–ESPANOL—————————

Hola,

Hoy les traigo un pequeno proyecto donde podran traducir los mensajes instantaneos, usando los servicios de Azure Cognitive services como servicio de traduccion. y conectarse usando powershell y en SDK de Lync.

En esta etapa temprana, este script solamente envia IM, pero no puede traducir mensajes entrantes. Puedes instalar el script en ambos puntos como workaround.

Vamos a comenzar!

Primero, necesitas crear un servicio en tu portal de Azure, (si necesitas una subcripcion de Azure)

Ve a Cognitive Service, y selecciona Translator Text API

image

Crea un nuevo servicio y selecciona la opcion gratuita, te permitira traducir hasta 2Millones de caracteres sin ningun costo

image

Una vez creado, por favor copia las llaves de acceso, ya que las necesitaras mas adelante en el script.

Sencillo, no lo creen?

Ahora vamos a crear y configurar nuestro script.

La siguiente es la funcion de Translate, la principal del script. Aqui solo necesitas reemplazar el accountKey con el de tu servicio para que comience a funcionar.

function Translate {

$target = $PrtTextBox.Text.toString()

$accountKey = “<Here your account Key>”
$tokenServiceURL = “https://api.cognitive.microsoft.com/sts/v1.0/issueToken”
$query = “?Subscription-Key=$accountKey”
$uri = $tokenServiceUrl+$query

$token = Invoke-RestMethod -Uri $uri -Method Post
$token

$auth = “Bearer “+$token
$header = @{Authorization = $auth}
$header

$fromLang = $DropDownBox.SelectedItem.ToString()
$toLang = $DropDownBox2.SelectedItem.ToString()
$text = $PrtTextBox2.text.toString()

$translationURL = “http://api.microsofttranslator.com/v2/Http.svc/Translate”
$query = “?text=” + [System.Web.HttpUtility]::UrlEncode($text)
$query += “&from=” + $fromLang
$query += “&to=” + $toLang
$query += “&contentType=text/plain”
$uri = $translationUrl+$query

$ret = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
$ret.OuterXml

$message= $ret.string.’#text’

if (-not (Get-Module -Name Microsoft.Lync.Model))
{
try
{
# you may need to change the location of this DLL
Import-Module “C:\Program Files (x86)\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll” -ErrorAction Stop
}
catch
{
Write-Warning “Microsoft.Lync.Model not available, download and install the Lync 2013 SDK http://www.microsoft.com/en-us/download/details.aspx?id=36824″
}
}

# Connect to the local Skype process
try
{
$client = [Microsoft.Lync.Model.LyncClient]::GetClient()
}
catch
{
Write-Host “`nMust be signed-in to Skype”
break
}

#Start Conversation
$msg = New-Object “System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType, String]”

#Add the Message
$msg.Add(1,$message)

# Add the contact URI
try
{
$contact = $client.ContactManager.GetContactByUri($target)
}
catch
{
Write-Host “`nFailed to lookup Contact”$target
break
}

# Create a conversation
$convo = $client.ConversationManager.AddConversation()
$convo.AddParticipant($contact) | Out-Null

# Set the message mode as IM
$imModality = $convo.Modalities[1]
# Send the message
$imModality.BeginSendMessage($msg, $null, $imModality) | Out-Null
# End the Convo to suppress the UI
$convo.End() | Out-Null

$Label3.Text = “Sent the following message to $target : $message”

}

Una vez que tenemos la funcion, crearemos la interfaz grafica.

$path = Get-location

$Prtform = New-Object Windows.Forms.Form
$Prtform.StartPosition = “CenterScreen”
$Prtform.text = “S4B Translate Agent by Rodolfo Castro UCBLOGMX”
$Image = [system.drawing.image]::FromFile(“$path\Image\SfBTranslator.jpg”)
$Prtform.BackgroundImage = $Image
$PrtForm.BackgroundImageLayout = “None”
$PrtForm.Width = $Image.Width
$PrtForm.MinimumSize = New-Object Drawing.Size @($Image.width,800)
$PrtForm.MaximumSize = New-Object Drawing.Size @($Image.width,800)
#$PrtForm.AutoSize = $True

$Label = New-Object System.Windows.Forms.Label
$Label.Text = “Target SIP Address”
$Label.AutoSize = $True
$label.Location = New-Object System.Drawing.Size(10,240)
$PrtForm.Controls.Add($Label)

$Label3 = New-Object System.Windows.Forms.Label
$Label3.Size = New-Object System.Drawing.Size(600,200)
$label3.Location = New-Object System.Drawing.Size(10,340)
$label3.BackColor = “Black”
$label3.Forecolor = “Yellow”
$PrtForm.Controls.Add($Label3)

$PrtTextBox = New-Object System.Windows.Forms.TextBox
$PrtTextBox.Location = New-Object System.Drawing.Size(120,240)
$PrtTextBox.Size = New-Object System.Drawing.Size(200,20)
$PrtForm.Controls.Add($prtTextBox)

$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = “From”
$Label5.AutoSize = $True
$label5.Location = New-Object System.Drawing.Size(350,240)
$PrtForm.Controls.Add($Label5)
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = “To”
$Label6.AutoSize = $True
$label6.Location = New-Object System.Drawing.Size(450,240)
$PrtForm.Controls.Add($Label6)

$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = “Message”
$Label2.AutoSize = $True
$label2.Location = New-Object System.Drawing.Size(10,275)
$PrtForm.Controls.Add($Label2)

$PrtTextBox2 = New-Object System.Windows.Forms.TextBox
$PrtTextBox2.Location = New-Object System.Drawing.Size(10,295)
$PrtTextBox2.Size = New-Object System.Drawing.Size(500,600)
$PrtTextBox2.MinimumSize = New-Object Drawing.Size @(500,600)
$PrtTextBox2.MaximumSize = New-Object Drawing.Size @(500,600)
$PrtForm.Controls.Add($prtTextBox2)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(120,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “Send”
$OKButton.Add_Click({

Translate($null)
}
)
$PrtForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(195,150)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$PrtForm.Close()})
$PrtForm.Controls.Add($CancelButton)

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(380,240)
$DropDownBox.Size = New-Object System.Drawing.Size(50,20)
$DropDownBox.DropDownHeight = 100
$PrtForm.Controls.Add($DropDownBox)

$List=@(“ES”,”EN”,”FR”,”DE”,”IT”,”BG”,”AF”,”BP”,”JA”,”KLI”,”HE”)
foreach ($item in $List) {
$DropDownBox.Items.Add($item)
}

$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Location = New-Object System.Drawing.Size(480,240)
$DropDownBox2.Size = New-Object System.Drawing.Size(50,20)
$DropDownBox2.DropDownHeight = 100
$PrtForm.Controls.Add($DropDownBox2)

foreach ($item in $List) {
$DropDownBox2.Items.Add($item)
}

$PrtForm.ShowDialog()

Quedara algo como esto.

image

Muy sencillo.

Target SIP Address : Direccion a la cual enviara el mensaje

From : idioma original

To: idioma al que traducira

Message: Mensaje a ser traducido

Send: Enviar y traducir

Cancel: Cerrar y cancelar

Esta es la primer etapa, voy a continuar trabajando hasta que este script este junto con el cliente de SfB sin necesidad de tenerlo separado

Por el momento, soporta hasta 60 idiomas, mismos que Azure Translate Service

image

En mi script solo agregue algunos, si deseas probar algun otro solo agregalo a la lista del dropbox list

Aqui hay un video de como funciona.

Espero les sea de ayuda.

———-ENGLISH——————–

Hey,

Today I bring to you a small project so now you can translate IM only, using Azure cognitive services as Translator Service, and connecting using powershell and Lync SDK.

At this early phase, this script only sends Translated IMs, but cannot translate incoming messages. But there’s a workaround. Let’s get started.

First you need to create an Azure Cognitive services on your Azure Portal (yes, you need to have Azure subscription)

Go to Cognitive Services, and select Translator Text API

image

Create a new service , and select the Free pricinig Tier. It will allow you to translate up to 2M characters without payment.

image

once is created, please copy Access Keys, as you will need it on the script.

Easy , does it?

Now, we need to create and configure our script.

The following is our Translate function, the core of the script. Here you only need to replace your accountKey to start working.

function Translate {

$target = $PrtTextBox.Text.toString()

$accountKey = “<Here your account Key>”
$tokenServiceURL = “https://api.cognitive.microsoft.com/sts/v1.0/issueToken”
$query = “?Subscription-Key=$accountKey”
$uri = $tokenServiceUrl+$query

$token = Invoke-RestMethod -Uri $uri -Method Post
$token

$auth = “Bearer “+$token
$header = @{Authorization = $auth}
$header

$fromLang = $DropDownBox.SelectedItem.ToString()
$toLang = $DropDownBox2.SelectedItem.ToString()
$text = $PrtTextBox2.text.toString()

$translationURL = “http://api.microsofttranslator.com/v2/Http.svc/Translate”
$query = “?text=” + [System.Web.HttpUtility]::UrlEncode($text)
$query += “&from=” + $fromLang
$query += “&to=” + $toLang
$query += “&contentType=text/plain”
$uri = $translationUrl+$query

$ret = Invoke-RestMethod -Uri $uri -Method Get -Headers $header
$ret.OuterXml

$message= $ret.string.’#text’

if (-not (Get-Module -Name Microsoft.Lync.Model))
{
try
{
# you may need to change the location of this DLL
Import-Module “C:\Program Files (x86)\Microsoft Office 2013\LyncSDK\Assemblies\Desktop\Microsoft.Lync.Model.dll” -ErrorAction Stop
}
catch
{
Write-Warning “Microsoft.Lync.Model not available, download and install the Lync 2013 SDK http://www.microsoft.com/en-us/download/details.aspx?id=36824″
}
}

# Connect to the local Skype process
try
{
$client = [Microsoft.Lync.Model.LyncClient]::GetClient()
}
catch
{
Write-Host “`nMust be signed-in to Skype”
break
}

#Start Conversation
$msg = New-Object “System.Collections.Generic.Dictionary[Microsoft.Lync.Model.Conversation.InstantMessageContentType, String]”

#Add the Message
$msg.Add(1,$message)

# Add the contact URI
try
{
$contact = $client.ContactManager.GetContactByUri($target)
}
catch
{
Write-Host “`nFailed to lookup Contact”$target
break
}

# Create a conversation
$convo = $client.ConversationManager.AddConversation()
$convo.AddParticipant($contact) | Out-Null

# Set the message mode as IM
$imModality = $convo.Modalities[1]
# Send the message
$imModality.BeginSendMessage($msg, $null, $imModality) | Out-Null
# End the Convo to suppress the UI
$convo.End() | Out-Null

$Label3.Text = “Sent the following message to $target : $message”

}

Once you get the core function, I create a GUI for easy control.

$path = Get-location

$Prtform = New-Object Windows.Forms.Form
$Prtform.StartPosition = “CenterScreen”
$Prtform.text = “S4B Translate Agent by Rodolfo Castro UCBLOGMX”
$Image = [system.drawing.image]::FromFile(“$path\Image\SfBTranslator.jpg”)
$Prtform.BackgroundImage = $Image
$PrtForm.BackgroundImageLayout = “None”
$PrtForm.Width = $Image.Width
$PrtForm.MinimumSize = New-Object Drawing.Size @($Image.width,800)
$PrtForm.MaximumSize = New-Object Drawing.Size @($Image.width,800)
#$PrtForm.AutoSize = $True

$Label = New-Object System.Windows.Forms.Label
$Label.Text = “Target SIP Address”
$Label.AutoSize = $True
$label.Location = New-Object System.Drawing.Size(10,240)
$PrtForm.Controls.Add($Label)

$Label3 = New-Object System.Windows.Forms.Label
$Label3.Size = New-Object System.Drawing.Size(600,200)
$label3.Location = New-Object System.Drawing.Size(10,340)
$label3.BackColor = “Black”
$label3.Forecolor = “Yellow”
$PrtForm.Controls.Add($Label3)

$PrtTextBox = New-Object System.Windows.Forms.TextBox
$PrtTextBox.Location = New-Object System.Drawing.Size(120,240)
$PrtTextBox.Size = New-Object System.Drawing.Size(200,20)
$PrtForm.Controls.Add($prtTextBox)

$Label5 = New-Object System.Windows.Forms.Label
$Label5.Text = “From”
$Label5.AutoSize = $True
$label5.Location = New-Object System.Drawing.Size(350,240)
$PrtForm.Controls.Add($Label5)
$Label6 = New-Object System.Windows.Forms.Label
$Label6.Text = “To”
$Label6.AutoSize = $True
$label6.Location = New-Object System.Drawing.Size(450,240)
$PrtForm.Controls.Add($Label6)

$Label2 = New-Object System.Windows.Forms.Label
$Label2.Text = “Message”
$Label2.AutoSize = $True
$label2.Location = New-Object System.Drawing.Size(10,275)
$PrtForm.Controls.Add($Label2)

$PrtTextBox2 = New-Object System.Windows.Forms.TextBox
$PrtTextBox2.Location = New-Object System.Drawing.Size(10,295)
$PrtTextBox2.Size = New-Object System.Drawing.Size(500,600)
$PrtTextBox2.MinimumSize = New-Object Drawing.Size @(500,600)
$PrtTextBox2.MaximumSize = New-Object Drawing.Size @(500,600)
$PrtForm.Controls.Add($prtTextBox2)

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(120,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = “Send”
$OKButton.Add_Click({

Translate($null)
}
)
$PrtForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(195,150)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = “Cancel”
$CancelButton.Add_Click({$PrtForm.Close()})
$PrtForm.Controls.Add($CancelButton)

$DropDownBox = New-Object System.Windows.Forms.ComboBox
$DropDownBox.Location = New-Object System.Drawing.Size(380,240)
$DropDownBox.Size = New-Object System.Drawing.Size(50,20)
$DropDownBox.DropDownHeight = 100
$PrtForm.Controls.Add($DropDownBox)

$List=@(“ES”,”EN”,”FR”,”DE”,”IT”,”BG”,”AF”,”BP”,”JA”,”KLI”,”HE”)
foreach ($item in $List) {
$DropDownBox.Items.Add($item)
}

$DropDownBox2 = New-Object System.Windows.Forms.ComboBox
$DropDownBox2.Location = New-Object System.Drawing.Size(480,240)
$DropDownBox2.Size = New-Object System.Drawing.Size(50,20)
$DropDownBox2.DropDownHeight = 100
$PrtForm.Controls.Add($DropDownBox2)

foreach ($item in $List) {
$DropDownBox2.Items.Add($item)
}

$PrtForm.ShowDialog()

It will looks like this.

image

Very simple.

Target SIP Address : Receiver’s Skype for Business Address, no need to add “sip:”

From : Original Language

To: Translated Language

Message: Text to be translated

Send: click to translate and send

Cancel: Close and cancel.

This is the first phase, I will keep working until this tool can be embedded on the SfB Client.

Now, it support up to 60 languages, same as Azure Translate Service

image

on my script i just added a few, if you want to test, just add it to the dropbox list.

Here is a video how it works. (on SPANISH, sorry)

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión /  Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión /  Cambiar )

Conectando a %s