Skip to content

Commit

Permalink
fix: Translate API
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Oct 16, 2023
1 parent 254a5b6 commit fe8d165
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions src/commands/translate.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use anyhow::{anyhow, Result};
use anyhow::Result;
use poise::{serenity_prelude as serenity, CreateReply};

use crate::Context;

#[derive(serde::Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct GoogleTranslateInput {
contents: Vec<String>,
target_language_code: String,
}

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct GoogleTranslateResponse {
translations: Vec<GoogleTranslateTranslation>,
struct GoogleTranslateSentence {
trans: Option<String>,
}

#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct GoogleTranslateTranslation {
translated_text: String,
detected_language_code: String,
struct GoogleTranslateResponse {
src: String,
sentences: Vec<GoogleTranslateSentence>,
}

/// Translates a message
Expand All @@ -42,35 +35,38 @@ pub async fn translate(ctx: Context<'_>, message: serenity::Message) -> Result<(
return Ok(());
}

let project_id = std::env::var("GOOGLE_CLOUD_PROJECT_ID")?;
// TODO: use ephemeral access tokens retrieved via service account impersonation
let access_token = std::env::var("GOOGLE_CLOUD_ACCESS_TOKEN")?;
let mut api_url =
"https://translate.googleapis.com/translate_a/single".parse::<reqwest::Url>()?;

let resp = crate::reqwest_client::HTTP
.post(format!("https://translation.googleapis.com/v3beta1/projects/{project_id}/locations/global:translateText"))
.header("authorization", format!("Bearer {}", access_token))
.json(&GoogleTranslateInput {
contents: vec![content],
target_language_code: "en".to_owned(),
})
.send()
.await?;
api_url
.query_pairs_mut()
.append_pair("client", "gtx")
.append_pair("sl", "auto")
.append_pair("tl", "en")
.append_pair("dt", "t")
.append_pair("dj", "1")
.append_pair("source", "input")
.append_pair("q", &content);

let resp = crate::reqwest_client::HTTP.get(api_url).send().await?;

let data: GoogleTranslateResponse = resp.json().await?;
let translation = data
.translations
.first()
.ok_or_else(|| anyhow!("No translations available!"))?;
.sentences
.into_iter()
.filter_map(|s| s.trans)
.collect::<Vec<String>>()
.join("");

ctx.send(
CreateReply::new().embed(
serenity::CreateEmbed::new()
.title("Translation")
.description(&translation.translated_text)
.description(&translation)
.color(0x34d399)
.footer(serenity::CreateEmbedFooter::new(format!(
"{} → en",
translation.detected_language_code
data.src
))),
),
)
Expand Down

0 comments on commit fe8d165

Please sign in to comment.