如何接收Telegram Bot的Bing翻译


How to receive Bing translation for Telegram Bot?

我正在创建翻译机器人,我已经从Microsoft Azure Market Place选择了Microsoft Translator-Text Translation
现在我只想进行一些练习,并选择200万字的免费计划
我已经创建了机器人,注册了它,并使其可以接收和回复用户的消息
然后我去了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');
    }

这个想法很简单,用户只需用一种语言发布一个单词,机器人就会返回翻译。
但是,目前还不能
我认为这个问题在某种程度上与access_token有关,但我不确定
所以,问题是:如何连接我的机器人与微软翻译。然后将用户的消息发送给翻译人员,接收翻译人员的回答并发送给用户?

我终于明白了!
这是代码。有了所有的评论,让每个人都清楚

 //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);
    }