翻訳ボットを作成しています。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と関連していると思いますが、わかりません。
質問は、私のボットとマイクロソフトの翻訳者との接続方法です。その後、翻訳者にユーザーのメッセージを送信し、翻訳者から回答を受け取り、ユーザーに送信しますか?