Giter VIP home page Giter VIP logo

translatebot's Introduction

Google Translate ChatBot

This is a Workers script that processes the text sent to the bot from a Google Chat client and returns the translated result from Google Translate.

Getting Started

Create a Google Chat bot by enabling the Hangouts Chat API in a project within your gSuite account. Give it a name, description and icon. This bot works in a DM as well as in a room, so you can tick both checkboxes. In the connection settings, define the bot URL. This will be the route where the Workers should be deployed to .

Next, enable the Cloud Translation API. This will enable you to use the paid/private translate.googleapis.com endpoint which is distinct from the free/public translate.google.com endpoint.

This is important to make sure the Terms of Service of G-Suite are applied for this feature, as opposed to the standard Google terms which dictates that everything including private data sent belongs to them to monetize:

When you upload, submit, store, send or receive content to or through our Services, you give Google [...] a worldwide license to use, host, store, reproduce, modify, create derivative works [...], communicate, publish, publicly perform, publicly display and distribute such content.

Next, create an API key. We'll restrict this key to allow only calls to Cloud Translation and Hangout Chat API, but application level restrictions can also be configured.

Add this API key as a Workers secret:

wrangler secret put GCP_API_KEY

Edit the wrangler.toml file, add your Cloudflare account ID and the URL route/zone ID where you want the Workers script to be deployed to.

The script can be deployed by running:

wrangler publish

You can also deploy this to a workers.dev by running:

wrangler publish --env dev

How to use

  • The first argument is the language to be translated to.
  • The second argument is the sentence to translate from.
  • When first argument is not specified, "en" will be assumed and the text will be translated to English by default.
  • The language of the original sentence will be detected and returned in a square bracket.

To interact with the bot in a DM:

To interact with the bot in a room, invite the bot and mention it's name:

Translating to a non-English language:

The Workers endpoint can also be called directly without using the Google Chat client. Here's an alias you can use:

translate () {
	translation_data='{"type": "MESSAGE","message":{"argumentText": "'$@'"}}'
	curl -sX POST https://faizazhar.com/projects/translate-bot/REDACTED -H "Content-Type:application/json" --data "${translation_data}" | jq .text
}

Call the function in a terminal:

translate ms i hope you enjoy this tutorial
"[en] saya harap anda menikmati tutorial ini"

translatebot's People

Contributors

gabanz avatar

Watchers

 avatar

Forkers

ztupidts

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.