📜 ⬆️ ⬇️

AutoTranslate via Skype

How it all began

After I received a message from a friend with a request to translate an elementary sentence from Russian into English, and a friend was sent to Google. from Google and send it via Skype. But it would be too boring, and I decided that the bot would also call and speak the given language in the given sentence.

A bit of searching, and I found a Skype4COM library that allows you to interact with Skype.

Also, to work with sound, I will use NAudio , and to take data from Google - xNet .

Begin to code

To start, we need to register Skype4COM in the system, for this we will execute
')
regsvr32 "__dll"

After that we can add links to all dll in the project.

Now we need to connect to Skype:

  //,   Skype if (!Skype.Client.IsRunning) { Skype.Client.Start(true, true); } //     Skype ((_ISkypeEvents_Event)Skype).AttachmentStatus += OurAttachmentStatus; //       Skype.CallStatus += CallStatusChanged; Skype.MessageStatus += ReceiveMessage; //  Skype Skype.Attach(8); 


Since joining Skype can last for a relatively long time, we need to know when we joined it:

 private void OurAttachmentStatus(TAttachmentStatus status) { if (status == TAttachmentStatus.apiAttachSuccess) textBox1.Text += "  "; } 


Now that we have joined Skype, we can start receiving messages.

 private void ReceiveMessage(ChatMessage pmessage, TChatMessageStatus status) { //   if (status == TChatMessageStatus.cmsReceived) { //     : string[] message = pmessage.Body.Split(':'); if (message.Length != 2) return; string mess = message[1]; string toLang = message[0]; //   string translate = GetTranslate(mess, toLang); //    byte[] bytes = GetFile(translate, toLang); Stream stream = new MemoryStream(bytes); //  Skype4COM     wav ,  mp3  wav. TimeSpan time = Mp3ToWav(stream, @"d:\test.wav"); //  Skype.PlaceCall(pmessage.FromHandle); //     pmessage.Chat.SendMessage(translate); //,       timer = new Timer(time.TotalMilliseconds); timer.Elapsed += FinishCall; timer.AutoReset = false; } } 


To work with the network, I decided to use the xNet library, which makes it easy to automate actions with websites.

First, we need to get the translation of the sentence:

  private string GetTranslate(string message, string toLang) { //      - using (HttpRequest request = new HttpRequest()) { StringDictionary reqParams = new StringDictionary(); //  ,      , //         ,      string myLang; if (toLang == "en") { myLang = "ru"; } else { myLang = "en"; } //   - request.UserAgent = HttpHelper.RandomChromeUserAgent(); reqParams["text"] = message; reqParams["tl"] = toLang; reqParams["sl"] = myLang; reqParams["client"] = "x"; //    string s = request.Get( "http://translate.google.ru/translate_a/t", reqParams).ToText(); //    string translate = s.Substring(":\"", "\""); return translate; } } 


Now we can get his voice acting:

 private byte[] GetFile(string translate, string toLang) { using (HttpRequest request = new HttpRequest()) { StringDictionary reqParams = new StringDictionary(); //   - reqParams["ie"] = "UTF-8"; reqParams["q"] = translate; reqParams["tl"] = toLang; reqParams["prev"] = "input"; //  byte[] bytes = request.Get( "http://translate.google.ru/translate_tts", reqParams).ToBytes(); return bytes; } } 


Now we need to transcode the file to Wav and save it to disk:

 public static TimeSpan Mp3ToWav(Stream mp3File, string outputFile) { //    mp3  using (Mp3FileReader reader = new Mp3FileReader(mp3File)) { //    var newFormat = new WaveFormat(16000, 16, 1); using (WaveStream pcmStream = new WaveFormatConversionStream(newFormat, reader)) { //     WaveFileWriter.CreateWaveFile(outputFile, pcmStream); //   return reader.TotalTime; } } } 


Now we can write the file playing code:

  private void CallStatusChanged(Call pcall, TCallStatus status) { //   ,     call = pcall; //   if (status == TCallStatus.clsInProgress) { //   pcall.set_InputDevice(TCallIoDeviceType.callIoDeviceTypeFile, @"d:\test.wav"); //   timer.Start(); } } 


Well, the call termination code:
  private void FinishCall(object sender, ElapsedEventArgs e) { call.Finish(); } 


Project files

In conclusion

I know that I probably made a lot of mistakes in the code, and I hope that the programming gurus will enlighten me, what am I wrong about.

PS In an amicable way, it would be necessary to end the call in the Skype. CallInputStatusChanged event, but how much I did not bother, it never came to me.

Source: https://habr.com/ru/post/139319/


All Articles