2016-05-02 1 views
0

翻訳ボットを作成しています。Microsoft Azure Market PlaceからMicrosoft Translator - Text Translationを選択しました。
今のところ私はちょうどいくつかの練習と2mの言葉のための無料の計画を選択したいと思います。
ボットを作成して登録し、ユーザーのメッセージの受信と返信を可能にしました。
次に、私は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'); 

    } 

アイデアは、ユーザーがちょうど1つの言語の単語を投稿し、簡単で、ボットは、翻訳を返します。
しかし、今のところそれはできません。
問題は何とかaccess_tokenと関連していると思いますが、わかりません。
質問は、私のボットとマイクロソフトの翻訳者との接続方法です。その後、翻訳者にユーザーのメッセージを送信し、翻訳者から回答を受け取り、ユーザーに送信しますか?

答えて

0

最後に私は理解しました!
ここにコードがあります。すべてのコメントはすべての人にわかります。

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

    } 
関連する問題