Spare time net core wrote an online customer service system. And wrote a series of articles in the blog park to introduce the development process.
I left this small system I wrote in my spare time on the Internet. One after another, someone asked me for a privatized version, and I gave it all. After all, the original intention of the software industry was to share it for free. Later, I simply officially sent a privatized version to others to download directly. Hope to build: open, open source and sharing. Strive to build net community.
In the second half of 2021, friends contacted me one after another to express that they were engaged in the foreign trade industry and hoped that the customer service system could realize two-way real-time automatic translation between customer service and visitors.
At first, I thought it was complicated and didn't do this function. Later, more and more friends asked. I carefully investigated this requirement and found that it is very simple to implement it through the interface on the public cloud! The whole docking process is literally completed in 10 minutes.
This article will introduce in detail the whole process of Baidu translation interface registration, opening and docking, as well as the source code, hoping to be useful to you.
Let's see the implementation effect first
The customer service program is displayed by comparing the original text with the translation.
The visitor side is displayed in the visitor language.
To set the default translation of all visitors' messages, or the customer service can decide whether to translate according to different visitors.
brief introduction
Shengxunwei online customer service and marketing system is based on net core / WPF, an online customer service software, aims to be open, open source and shared. Strive to build net community.
Full privatization package download address
💾 https://kf.shengxunwei.com/freesite.zip
If you like it, please give me a favor and support it. Thank you~
Installation and deployment instructions
📕 https://docs.shengxunwei.com/Post/f7bc8496-14ee-4a53-07b4-08d8e3da6269
Docking use of Baidu translation
Visit Baidu translation open platform, register an account and authenticate.
Open general translation API
be careful:
- In rare languages (such as Ukraine, Philippines and Indonesia), only the enterprise certified premium version can be called, and the uncertified non premium version interface will return 58001 error.
- Please select to open "exclusive edition". The "Standard Version" QPS is only 1, and it is only allowed to call the translation interface once a second, which can not meet the normal use needs of the customer service system.
Create application
"Server callback address" is left blank.
"Server address" can be left blank, or the IP address of the server deploying the customer service system can be filled in.
Get APP ID and key
Enter the "Developer Information" screen, view the APP ID and key in the "application information", save them for standby, and use them in the subsequent main program configuration of the customer service system.
Use C# to call the translation interface
Access mode
The translation API provides multilingual translation services through the HTTP interface. Only by calling the general translation API, passing in the content to be translated, and specifying the source language (supporting automatic detection of source language) and target language to be translated, the corresponding translation results can be obtained.
Generic translation API HTTPS address:
https://fanyi-api.baidu.com/api/trans/vip/translate
Signature generation method
Signature is a string generated by MD5 algorithm to ensure call security. The generated signature length is 32 bits, and the English characters in the signature are in lowercase format.
Generation method:
Step1. Splice the APPID(appid), translation query(q, note UTF-8 coding), random number (salt) and the key allocated by the platform (which can be viewed on the management console) in the request parameters in the order of appid+q+salt + key to obtain string 1.
Step2. Do md5 on string 1 to get a 32-bit lowercase sign.
Note:
- The text to be translated (q) shall be encoded in UTF-8;
- When generating the signature splicing appid+q+salt + key string, q does not need to do URL encode. After generating the signature and before sending the HTTP request, q needs to do URL encode for the text field to be translated to be sent;
3. If you encounter 54001 signature error, please check whether your signature generation method is correct. When splicing and encrypting a sign, q you do not need to do URL encode. Many developers encounter signature error because they do URL encode before splicing a sign;
4. After the signature is generated, when sending an HTTP request, if the query is spliced on the url, the query needs to be url encoded.
input parameter
Request method: GET or POST can be used. If POST is used, please specify the content type as: application/x-www-form-urlencoded
Character coding: unified UTF-8 coding format
query length: to ensure the translation quality, please control the length of a single request within 6000 bytes (Chinese characters are about 2000 input parameters)
code
using System; using System.Text; using System.Net; using System.IO; using System.Security.Cryptography; using System.Web; namespace TransAPICSharpDemo { class Program { static void Main(string[] args) { // original text string q = "apple"; // source language string from = "en"; // target language string to = "zh"; // Change to your APP ID string appId = "2015063000000001"; Random rd = new Random(); string salt = rd.Next(100000).ToString(); // Change to your key string secretKey = "12345678"; string sign = EncryptString(appId + q + salt + secretKey); string url = "http://api.fanyi.baidu.com/api/trans/vip/translate?"; url += "q=" + HttpUtility.UrlEncode(q); url += "&from=" + from; url += "&to=" + to; url += "&appid=" + appId; url += "&salt=" + salt; url += "&sign=" + sign; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; request.UserAgent = null; request.Timeout = 6000; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); Console.WriteLine(retString); Console.ReadLine(); } // Calculate MD5 value public static string EncryptString(string str) { MD5 md5 = MD5.Create(); // Convert string to byte array byte[] byteOld = Encoding.UTF8.GetBytes(str); // Call encryption method byte[] byteNew = md5.ComputeHash(byteOld); // Converts the encrypted result to a string StringBuilder sb = new StringBuilder(); foreach (byte b in byteNew) { // Convert bytes to hexadecimal strings, sb.Append(b.ToString("x2")); } // Returns an encrypted string return sb.ToString(); } } }
When writing your code, replace the APP ID and key in the above example code with the one you got when you registered and opened Baidu translation!
Hope to build: open, open source and sharing. Strive to build net community.