如何接收 Telegram Bot 的 Bing 翻译?
How to receive Bing translation for Telegram Bot?
我正在创建翻译机器人,我选择了 Microsoft Translator - 来自 Microsoft Azure Market Place 的文本翻译。
现在我只想练习和选择 200 万字的免费计划。
我创建了 bot,注册了它并使其可以接收和回复用户的消息。
然后我去 https://msdn.microsoft.com/en-us/library/hh454950.aspx 接收访问令牌。可能在那里出现了我的问题。
现在我的代码是:
<?php
//Set errors show
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
//Set bot's token
define('BOT_TOKEN', "my telegram bot token");
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
//Set bot's receiving information
$update = file_get_contents("php://input");
$update = json_decode($update, true);
//Receive user's chatid and his message to bot
$chat_id = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
function sendMessage($chat_id, $answer)
{
file_get_contents(API_URL ."sendMessage?chat_id=".$chat_id."&text=".urlencode($answer));
}
//Receive access token to translate
class _auth {
function getToken($grantType, $scopeUrl, $clientId, $clientSecret, $authUrl){
$ch = curl_init();
$params = array(
'grant_type' => $grantType,
'scope' => $scopeUrl,
'client_id' => $clientId,
'client_secret' => $clientSecret
);
$params = http_build_query($params);
curl_setopt_array($ch, array(
CURLOPT_URL => $authUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"Content-type: text/xml",
"Authorization: Bearer ". $access_token,
)
));
$response = curl_exec($ch);
curl_close($ch);
$a_resp = json_decode($response);
$access_token = $a_resp->access_token;
}
}
if($message){
$a = new _auth();
$clientId = "my app's client Id";
$clientSecret = "My app's client secret";
$authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
$scopeUrl = "http://api.microsofttranslator.com";
$grantType = "client_credentials";
sendTranslate($message);
$answer = $message;
sendMessage($chat_id, $answer);
}
function sendTranslate($message){
$message = urlencode($message);
file_get_contents('http://api.microsofttranslator.com/V2/Http.svc/Translate?Text='.$message.'&To=%27ru%27&From=%27en%27');
}
这个想法很简单,用户只需 post 一种语言的一个词,然后机器人 returns 进行翻译。
但是,目前还不能。
我认为这个问题与 access_token 有某种联系,但我不确定。
所以,问题是:如何将我的机器人与 Microsoft 翻译器连接起来。然后将用户的消息发送给译者,接收译者的回答并发送给用户?
终于明白了!
这是代码。附上评论给大家说清楚
//Create class of basic params which required to connection
class _translate {
private $_client_id;
private $_client_secret;
private $grant_type = 'client_credentials';
private $scope_url = 'http://api.microsofttranslator.com';
public function __construct($clientID, $clientSecret) {
$this->_client_id = $clientID;
$this->_client_secret = $clientSecret;
}
//Create function for curl
public function getResponse($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$this->getToken(),
'Content-Type: text/xml'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//Function to receive access token for translation
public function getToken($clientID, $clientSecret) {
//Set user's id and ST
$clientID = $this->_client_id;
$clientSecret = $this->_client_secret;
$ch = curl_init();
//Set params for request
$params = array(
'grant_type' => $this->grant_type,
'scope' => $this->scope_url,
'client_id' => $clientID,
'client_secret' => $clientSecret,
);
$row = http_build_query($params, '', '&');
curl_setopt_array($ch, array(
CURLOPT_URL =>'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $row,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($ch);
curl_close($ch);
$response_obj = json_decode($response);
//Receive access token for further operations
return $response_obj->access_token;
}
//Set function for translation
public function getTranslation($fromLanguage, $toLanguage, $text)
{
//Create answer to server with specified curl
$response = $this->getResponse($this->getURL($fromLanguage, $toLanguage, $text));
//To delete xml tegs
return strip_tags($response);
}
//This function sets url which our bot will call
public function getURL($fromLanguage, $toLanguage, $text)
{
return 'http://api.microsofttranslator.com/v2/Http.svc/Translate?text='.urlencode($text).'&to='.$toLanguage.'&from='.$fromLanguage;
}
}
//Default
//Translate user's message
if($message)
{
//connect to our app with it's id and
$translate = new _translate('id', 'secret');
/*
* Set translation language first - from, second - to.
* $message - user's message
*/
$translation = $translate->getTranslation('en', 'ru', $message);
//get translation of user's message and send it back to user
$answer = $translation;
sendMessage($chat_id, $answer);
}
我正在创建翻译机器人,我选择了 Microsoft Translator - 来自 Microsoft Azure Market Place 的文本翻译。
现在我只想练习和选择 200 万字的免费计划。
我创建了 bot,注册了它并使其可以接收和回复用户的消息。
然后我去 https://msdn.microsoft.com/en-us/library/hh454950.aspx 接收访问令牌。可能在那里出现了我的问题。
现在我的代码是:
<?php
//Set errors show
ini_set('display_errors', 1);
ini_set('log_errors', 1);
error_reporting(E_ALL);
//Set bot's token
define('BOT_TOKEN', "my telegram bot token");
define('API_URL', 'https://api.telegram.org/bot'.BOT_TOKEN.'/');
//Set bot's receiving information
$update = file_get_contents("php://input");
$update = json_decode($update, true);
//Receive user's chatid and his message to bot
$chat_id = $update["message"]["chat"]["id"];
$message = $update["message"]["text"];
function sendMessage($chat_id, $answer)
{
file_get_contents(API_URL ."sendMessage?chat_id=".$chat_id."&text=".urlencode($answer));
}
//Receive access token to translate
class _auth {
function getToken($grantType, $scopeUrl, $clientId, $clientSecret, $authUrl){
$ch = curl_init();
$params = array(
'grant_type' => $grantType,
'scope' => $scopeUrl,
'client_id' => $clientId,
'client_secret' => $clientSecret
);
$params = http_build_query($params);
curl_setopt_array($ch, array(
CURLOPT_URL => $authUrl,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $params,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
"Content-type: text/xml",
"Authorization: Bearer ". $access_token,
)
));
$response = curl_exec($ch);
curl_close($ch);
$a_resp = json_decode($response);
$access_token = $a_resp->access_token;
}
}
if($message){
$a = new _auth();
$clientId = "my app's client Id";
$clientSecret = "My app's client secret";
$authUrl = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
$scopeUrl = "http://api.microsofttranslator.com";
$grantType = "client_credentials";
sendTranslate($message);
$answer = $message;
sendMessage($chat_id, $answer);
}
function sendTranslate($message){
$message = urlencode($message);
file_get_contents('http://api.microsofttranslator.com/V2/Http.svc/Translate?Text='.$message.'&To=%27ru%27&From=%27en%27');
}
这个想法很简单,用户只需 post 一种语言的一个词,然后机器人 returns 进行翻译。
但是,目前还不能。
我认为这个问题与 access_token 有某种联系,但我不确定。
所以,问题是:如何将我的机器人与 Microsoft 翻译器连接起来。然后将用户的消息发送给译者,接收译者的回答并发送给用户?
终于明白了!
这是代码。附上评论给大家说清楚
//Create class of basic params which required to connection
class _translate {
private $_client_id;
private $_client_secret;
private $grant_type = 'client_credentials';
private $scope_url = 'http://api.microsofttranslator.com';
public function __construct($clientID, $clientSecret) {
$this->_client_id = $clientID;
$this->_client_secret = $clientSecret;
}
//Create function for curl
public function getResponse($url) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
'Authorization: Bearer '.$this->getToken(),
'Content-Type: text/xml'
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
//Function to receive access token for translation
public function getToken($clientID, $clientSecret) {
//Set user's id and ST
$clientID = $this->_client_id;
$clientSecret = $this->_client_secret;
$ch = curl_init();
//Set params for request
$params = array(
'grant_type' => $this->grant_type,
'scope' => $this->scope_url,
'client_id' => $clientID,
'client_secret' => $clientSecret,
);
$row = http_build_query($params, '', '&');
curl_setopt_array($ch, array(
CURLOPT_URL =>'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $row,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false
));
$response = curl_exec($ch);
curl_close($ch);
$response_obj = json_decode($response);
//Receive access token for further operations
return $response_obj->access_token;
}
//Set function for translation
public function getTranslation($fromLanguage, $toLanguage, $text)
{
//Create answer to server with specified curl
$response = $this->getResponse($this->getURL($fromLanguage, $toLanguage, $text));
//To delete xml tegs
return strip_tags($response);
}
//This function sets url which our bot will call
public function getURL($fromLanguage, $toLanguage, $text)
{
return 'http://api.microsofttranslator.com/v2/Http.svc/Translate?text='.urlencode($text).'&to='.$toLanguage.'&from='.$fromLanguage;
}
}
//Default
//Translate user's message
if($message)
{
//connect to our app with it's id and
$translate = new _translate('id', 'secret');
/*
* Set translation language first - from, second - to.
* $message - user's message
*/
$translation = $translate->getTranslation('en', 'ru', $message);
//get translation of user's message and send it back to user
$answer = $translation;
sendMessage($chat_id, $answer);
}