Skip to content

Commit

Permalink
[mod] #62 翻訳機能実装
Browse files Browse the repository at this point in the history
  • Loading branch information
Diaboro87 committed Dec 19, 2017
1 parent 9ae0b76 commit 410d533
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# DiscordBot
TOKEN=<your token>
CLIENT_ID=<your client_id>

# DOCOMO
DOCOMO_TALK_APIKEY=<docomo apikey>

# BlueMix
BLUE_MIX_USER=<blue mix user>
BLUE_MIX_PASS=<blue mix password>
40 changes: 40 additions & 0 deletions xp_fiat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "discordrb"
require "mechanize"
require "json"
require "net/http"
require "uri"
require "./command_patroller"
require "dotenv/load"

Expand Down Expand Up @@ -180,6 +182,44 @@ def get_trend_message(response)
"【#{title}\n#{link_url} "
end

# -----------------------------------------------------------------------------
# 翻訳(google)

bot.command :translate do |event, *sentences|
sentence = ""
sentences.each do |word|
sentence += word + " "
end
text = blue_mix_translate(event, sentence, model_id:"ja-en")
blue_mix_translate(event, text, model_id:"en-ja")
end

def blue_mix_translate(event, sentence, model_id:)

uri = URI.parse("https://gateway.watsonplatform.net/language-translator/api/v2/translate")
request = Net::HTTP::Post.new(uri)
request.basic_auth(ENV["BLUE_MIX_USER"], ENV["BLUE_MIX_PASS"])
request.content_type = "application/json"
request["Accept"] = "application/json"
request.body = JSON.dump({
"model_id": model_id,
"text": sentence
})

req_options = {
use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end

translated = JSON.parse(response.body)["translations"][0]["translation"]
event.send_message("#{translated} ")
end



# -----------------------------------------------------------------------------
# CoinExchange.io
bot.command :ce do |event|
Expand Down

0 comments on commit 410d533

Please sign in to comment.