Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。
Zappier で Github Issue 作成をトリガーにして、pythonスクリプトからGoogle翻訳APIを叩き、翻訳済みの新しいGithub Issue を作成する例。日本語から英語へバージョン。
補足
Google のトークンの有効期限などは確認していない。とりあえず動いた。
Google翻訳API用に GCP の認証トークンを取得しておく
Issue 作成をトリガーにする
Code By Zapier
Github の title と description を 入力リソースに指定する
Script
先程取得したトークンをスクリプト内に埋め込む Google 翻訳で Markdownが崩れるので、pythonで適当に整形。
import requests import re data = { 'q': [ input["title"], input["body"], ], 'source': 'ja', 'target': 'en', 'format': 'text' } url = 'https://translation.googleapis.com/language/translate/v2' token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' headers = { 'Authorization': 'Bearer {}'.format(token), 'Content-Type': 'application/json', } res = requests.post(url, headers=headers, json=data) # Title output_title = res.json()['data']['translations'][0]['translatedText'] # For twitter in English 240 upper limit characters output_title = output_title[:200] # Fix Body: Image Markdown tag spaces output_body = res.json()['data']['translations'][1]['translatedText'] output_body = re.sub(r'! \[image\] ', "![image]", output_body) output = [ { "title": output_title, "body": output_body } ]
Find or Create Issue をアクションにする
結果
https://github.com/YumaInaura/issue/issues/63
画像もバッチリ
おまけ。必要なら Zapier の Detect Language / Only Continue if
で、日本語判定された場合にだけ、アクションが走るようにしておく。