lita-translation 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 92de845747ee164159ffba572f90f692d6d42ea6
4
+ data.tar.gz: 2681a8075c783578e9d115ca7975083401c59b77
5
+ SHA512:
6
+ metadata.gz: 4408c35c04ec73dd30b1de26772be8ce47dc16aa7e227dd3151fa9e9c5c284e186f77c7150eaf8dfd8ce09e369adbed8b8659d39c1024a3afc3123da3b9a40ac
7
+ data.tar.gz: 8eeb31021dbb354d75886894b7835540c6597afffb6d311d36859367075b57865f2603ab1bba7e184255a02ee67ecde83dd58d24e46fa65704a22f2bdec3631a
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ dump.rdb
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Michael Chua
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,47 @@
1
+ # lita-translation
2
+
3
+ Language translation plugin that uses Microsoft's Translator API.
4
+
5
+ ## Installation
6
+
7
+ Add lita-translation to your Lita instance's Gemfile:
8
+
9
+ ``` ruby
10
+ gem "lita-translation"
11
+ ```
12
+
13
+ ## Configuration
14
+
15
+ This plugin requires you to obtain a client id and secret for [Microsoft's Translation API](http://www.microsoft.com/translator/api.aspx). Register an account at the [Microsoft Azure Marketplace](https://azure.microsoft.com), and then register an application that uses the translation API service.
16
+
17
+ ### Required attributes
18
+
19
+ * `client_id` (String) - A human readable identifier for your client that you provide on registration.
20
+ * `client_secret` (String) - A key generated by Microsoft for your client.
21
+
22
+ ### Example
23
+
24
+ ```
25
+ Lita.configure do |config|
26
+ config.handlers.translation.client_id = "my-translation-id"
27
+ config.handlers.translation.client_secret = "some key"
28
+ end
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ An OAuth token is automatically requested by the plugin whenever a fresh one is unavailable.
34
+
35
+ Microsoft uses ISO-639 codes to identify languages. Klingon is available.
36
+
37
+ * languages - List language codes supported by Microsoft's Translator API
38
+ * determine '[text]' - Determine language of the given text
39
+ * translate '[text]' to [code] (from [code]) - Translate the given text from one language to another
40
+ * translate me to [code] (from [code]) - Begin auto-translating all user's speech to the given language
41
+ * stop translating me - End any auto-translation
42
+
43
+ Source language is optional during translation. Microsoft will attempt to detect the source language if none is supplied.
44
+
45
+ ## License
46
+
47
+ [MIT](http://opensource.org/licenses/MIT)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,12 @@
1
+ require "lita"
2
+
3
+ Lita.load_locales Dir[File.expand_path(
4
+ File.join("..", "..", "locales", "*.yml"), __FILE__
5
+ )]
6
+
7
+ require "lita/handlers/translation"
8
+
9
+ Lita::Handlers::Translation.template_root File.expand_path(
10
+ File.join("..", "..", "templates"),
11
+ __FILE__
12
+ )
@@ -0,0 +1,101 @@
1
+ require 'json'
2
+
3
+ class MSTranslator
4
+ OAUTH_URI = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"
5
+ API_URI = "https://api.microsofttranslator.com/V2/Ajax.svc/"
6
+
7
+ REST_LANGUAGES = "GetLanguagesForTranslate"
8
+ REST_DETECTION = "Detect"
9
+ REST_TRANSLATE = "Translate"
10
+
11
+ def initialize(clientId, clientSecret, http, redis)
12
+ @clientId = clientId
13
+ @clientSecret = clientSecret
14
+ @http = http
15
+ @redis = redis
16
+ end
17
+
18
+ def grabAccessToken()
19
+ result = @http.post(
20
+ OAUTH_URI,
21
+ client_id: @clientId,
22
+ client_secret: @clientSecret,
23
+ scope: "http://api.microsofttranslator.com",
24
+ grant_type: "client_credentials"
25
+ )
26
+ if result.status == 200
27
+ raw = JSON.parse(result.body)
28
+ @redis.set("token", raw["access_token"])
29
+ @redis.set("start", Time.now)
30
+ @redis.set("expiry", raw["expires_in"])
31
+ true
32
+ else
33
+ false
34
+ end
35
+ end
36
+
37
+ def staleToken?()
38
+ start = @redis.get("start")
39
+ expiry = @redis.get("expiry")
40
+ if start.nil? || (Time.parse(start) + expiry.to_i) < Time.now
41
+ return true
42
+ end
43
+ false
44
+ end
45
+
46
+ def apiSuccess?(result)
47
+ if result.status != 200
48
+ false
49
+ else
50
+ /ID=\d{4}\.V2_Json\.(\w+)\.\w{8}/.match(result.body).nil?
51
+ end
52
+ end
53
+
54
+ def languages()
55
+ result = @http.get do |req|
56
+ req.url API_URI+REST_LANGUAGES
57
+ req.headers['Authorization'] = "Bearer "+@redis.get("token")
58
+ end
59
+ if apiSuccess?(result)
60
+ TranslationResult.new(
61
+ true,
62
+ JSON.parse(result.body.slice(3..result.body.length)).join(",")
63
+ )
64
+ else
65
+ TranslationResult.new(
66
+ false,
67
+ result.body.slice(3..result.body.length)
68
+ )
69
+ end
70
+ end
71
+
72
+ def detect(text)
73
+ result = @http.get do |req|
74
+ req.url API_URI+REST_DETECTION, :text => text
75
+ req.headers['Authorization'] = "Bearer "+@redis.get("token")
76
+ end
77
+ TranslationResult.new(apiSuccess?(result), result.body.slice(3..result.body.length))
78
+ end
79
+
80
+ def translate(text, to, from=nil)
81
+ result = @http.get do |req|
82
+ req.url API_URI+REST_TRANSLATE, :text => text, :from => from, :to => to, :contentType => "text/plain"
83
+ req.headers['Authorization'] = "Bearer "+@redis.get("token")
84
+ end
85
+ TranslationResult.new(apiSuccess?(result), result.body.slice(3..result.body.length))
86
+ end
87
+
88
+ end
89
+
90
+ class TranslationResult
91
+ def initialize(success, message)
92
+ @success = success
93
+ @message = message
94
+ end
95
+ def success
96
+ @success
97
+ end
98
+ def message
99
+ @message
100
+ end
101
+ end
@@ -0,0 +1,116 @@
1
+ require "lita/handlers/msTranslator"
2
+
3
+ module Lita
4
+ module Handlers
5
+ class Translation < Handler
6
+ config :client_id, type: String, required: true
7
+ config :client_secret, type: String, required: true
8
+
9
+ def tokenAvailable?(response, translator)
10
+ if translator.staleToken?
11
+ response.reply(t("replies.access_token.attempt"))
12
+ if translator.grabAccessToken
13
+ response.reply(t("replies.access_token.success"))
14
+ return true
15
+ else
16
+ response.reply(t("replies.access_token.fail"))
17
+ return false
18
+ end
19
+ else
20
+ return true
21
+ end
22
+ end
23
+
24
+ route /^languages$/, :languages, help: {
25
+ t("help.languages.usage") => t("help.languages.description")
26
+ }
27
+ def languages(response)
28
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
29
+ if tokenAvailable?(response, translator)
30
+ result = translator.languages
31
+ if result.success
32
+ response.reply(t("replies.languages"))
33
+ else
34
+ response.reply(t("replies.failure"))
35
+ end
36
+ response.reply(result.message)
37
+ end
38
+ end
39
+
40
+ route /^determine '(.+)'$/, :determine, help: {
41
+ t("help.determine.usage") => t("help.determine.description")
42
+ }
43
+ def determine(response)
44
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
45
+ if tokenAvailable?(response, translator)
46
+ result = translator.detect(response.matches.pop[0])
47
+ if result.success
48
+ code = result.message
49
+ response.reply(t("replies.determine", code: code))
50
+ else
51
+ response.reply(t("replies.failure"))
52
+ response.reply(result.message)
53
+ end
54
+ end
55
+ end
56
+
57
+ route /^translate me to (\w+)( from (\w+))?$/, :auto_start, help: {
58
+ t("help.auto_start.usage") => t("help.auto_start.description")
59
+ }
60
+ def auto_start(response)
61
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
62
+ if tokenAvailable?(response, translator)
63
+ to = response.matches.flatten[0]
64
+ from = response.matches.flatten[2]
65
+ redis.set(response.user.id+":to", to)
66
+ redis.set(response.user.id+":from", from)
67
+ response.reply(t("replies.auto_start", code: to, user: response.user.name))
68
+ end
69
+ end
70
+
71
+ route /^stop translating me$/, :auto_end, help: {
72
+ t("help.auto_end.usage") => t("help.auto_end.description")
73
+ }
74
+ def auto_end(response)
75
+ redis.del(response.user.id+":to")
76
+ redis.del(response.user.id+":from")
77
+ response.reply(t("replies.auto_end", user: response.user.name))
78
+ end
79
+
80
+ route /^translate '(.+)' to (\w+)( from (\w+))?$/, :tran_lang, help: {
81
+ t("help.translate.usage") => t("help.translate.description")
82
+ }
83
+ def tran_lang(response)
84
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
85
+ if tokenAvailable?(response, translator)
86
+ text = response.matches.flatten[0]
87
+ to = response.matches.flatten[1]
88
+ from = response.matches.flatten[3]
89
+ result = translator.translate(text, to, from)
90
+ if result.success
91
+ response.reply(t("replies.translate", translated: result.message))
92
+ else
93
+ response.reply(t("replies.failure"))
94
+ response.reply(result.message)
95
+ end
96
+ end
97
+ end
98
+
99
+ route /./, :monitor
100
+ def monitor(response)
101
+ if(!redis.get(response.user.id+":to").nil?)
102
+ translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
103
+ to = redis.get(response.user.id+":to")
104
+ from = redis.get(response.user.id+":from")
105
+ result = translator.translate(response.message.body, to, from)
106
+ if result.success
107
+ response.reply(t("replies.auto", user: response.user.name, translated: result.message))
108
+ end
109
+ end
110
+ end
111
+
112
+ end
113
+
114
+ Lita.register_handler(Translation)
115
+ end
116
+ end
@@ -0,0 +1,23 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-translation"
3
+ spec.version = "1.0.0"
4
+ spec.authors = ["Michael Chua"]
5
+ spec.email = ["chua.mbt@gmail.com"]
6
+ spec.description = %q{Language translation plugin that uses Microsoft's Translator API. }
7
+ spec.summary = %q{Language translation plugin that uses Microsoft's Translator API. }
8
+ spec.license = "MIT"
9
+ spec.metadata = { "lita_plugin_type" => "handler" }
10
+ spec.homepage = "https://github.com/chua-mbt/lita-translation"
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", ">= 4.2"
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.3"
20
+ spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "rack-test"
22
+ spec.add_development_dependency "rspec", ">= 3.0.0"
23
+ end
@@ -0,0 +1,32 @@
1
+ en:
2
+ lita:
3
+ handlers:
4
+ translation:
5
+ help:
6
+ languages:
7
+ usage: "languages"
8
+ description: "List language codes supported by Microsoft's Translator API"
9
+ determine:
10
+ usage: "determine '[text]'"
11
+ description: "Determine language of the given text"
12
+ translate:
13
+ usage: "translate '[text]' to [code] (from [code])"
14
+ description: "Translate the given text from one language to another"
15
+ auto_start:
16
+ usage: "translate me to [code] (from [code])"
17
+ description: "Begin auto-translating all user's speech to the given language"
18
+ auto_end:
19
+ usage: "stop translating me"
20
+ description: "End any auto-translation"
21
+ replies:
22
+ access_token:
23
+ attempt: "Requesting an access token..."
24
+ success: "Got an access token for Microsoft's Translator API!"
25
+ fail: "Could not get an access token!"
26
+ languages: "The following language codes are supported: "
27
+ determine: "Language code: %{code}"
28
+ translate: "Translation: %{translated}"
29
+ failure: "API Failure: "
30
+ auto_start: "Beginning translation to %{code} for %{user}."
31
+ auto: "%{user}: %{translated}."
32
+ auto_end: "Ceasing translation for %{user}."
@@ -0,0 +1,4 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Translation, lita_handler: true do
4
+ end
@@ -0,0 +1,6 @@
1
+ require "lita-translation"
2
+ require "lita/rspec"
3
+
4
+ # A compatibility mode is provided for older plugins upgrading from Lita 3. Since this plugin
5
+ # was generated with Lita 4, the compatibility mode should be left disabled.
6
+ Lita.version_3_compatibility_mode = false
File without changes
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-translation
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Chua
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rack-test
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0
83
+ description: 'Language translation plugin that uses Microsoft''s Translator API. '
84
+ email:
85
+ - chua.mbt@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE
93
+ - README.md
94
+ - Rakefile
95
+ - lib/lita-translation.rb
96
+ - lib/lita/handlers/msTranslator.rb
97
+ - lib/lita/handlers/translation.rb
98
+ - lita-translation.gemspec
99
+ - locales/en.yml
100
+ - spec/lita/handlers/translation_spec.rb
101
+ - spec/spec_helper.rb
102
+ - templates/.gitkeep
103
+ homepage: https://github.com/chua-mbt/lita-translation
104
+ licenses:
105
+ - MIT
106
+ metadata:
107
+ lita_plugin_type: handler
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.4.6
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Language translation plugin that uses Microsoft's Translator API.
128
+ test_files:
129
+ - spec/lita/handlers/translation_spec.rb
130
+ - spec/spec_helper.rb