Class: Lita::Handlers::Translation

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/translation.rb

Instance Method Summary collapse

Instance Method Details

#auto_end(response) ⇒ Object



124
125
126
127
128
# File 'lib/lita/handlers/translation.rb', line 124

def auto_end(response)
  redis.del(response.user.id+":to")
  redis.del(response.user.id+":from")
  response.reply(t("replies.auto_end", user: response.user.name))
end

#auto_start(response) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lita/handlers/translation.rb', line 106

def auto_start(response)
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
  if tokenAvailable?(response, translator)
    to = response.matches.flatten[0]
    from = response.matches.flatten[2]
    name = JSON.parse(redis.get("languages"))[to]
    if name.nil?
      response.reply(t("replies.unknown_code", code: to))
    end
    redis.set(response.user.id+":to", to)
    redis.set(response.user.id+":from", from)
    response.reply(t("replies.auto_start", code: to, name: name, user: response.user.name))
  end
end

#determine(response) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lita/handlers/translation.rb', line 86

def determine(response)
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
  if tokenAvailable?(response, translator)
    result = translator.detect(response.matches.pop[0])
    if result.success
      code = result.message
      code[0] = ''
      code = code[0..-2]
      name = JSON.parse(redis.get("languages"))[code]
      response.reply(t("replies.determine", code: code, name: name))
    else
      response.reply(t("replies.failure"))
      response.reply(result.message)
    end
  end
end

#init_lang(payload) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lita/handlers/translation.rb', line 19

def init_lang(payload)
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
  if translator.staleToken?
    translator.grabAccessToken
  end

  result = translator.languageCodes
  if !result.success
    return
  end

  codes = result.message
  result = translator.languageNames(codes)
  if !result.success
    return
  end

  names = result.message
  updateStoredLang(codes, names)
end

#languages(response) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lita/handlers/translation.rb', line 58

def languages(response)
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
  if tokenAvailable?(response, translator)
    codeResult = translator.languageCodes
    if !codeResult.success
      response.reply(t("replies.failure"))
      response.reply(codeResult.message)
      return
    end

    nameResult = translator.languageNames(codeResult.message)
    if !nameResult.success
      response.reply(t("replies.failure"))
      response.reply(nameResult.message)
      return
    end

    languages = updateStoredLang(codeResult.message, nameResult.message)
    response.reply_privately(t("replies.languages"))
    languages.each{ |code, name|
      response.reply_privately("#{code}(#{name})")
    }
  end
end

#monitor(response) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/lita/handlers/translation.rb', line 151

def monitor(response)
  if(!redis.get(response.user.id+":to").nil?)
    translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
    to = redis.get(response.user.id+":to")
    from = redis.get(response.user.id+":from")
    result = translator.translate(response.message.body, to, from)
    if result.success
      response.reply(t("replies.auto", user: response.user.name, translated: result.message))
    end
  end
end

#tokenAvailable?(response, translator) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lita/handlers/translation.rb', line 40

def tokenAvailable?(response, translator)
  if translator.staleToken?
    response.reply(t("replies.access_token.attempt"))
    if translator.grabAccessToken
      response.reply(t("replies.access_token.success"))
      return true
    else
      response.reply(t("replies.access_token.fail"))
      return false
    end
  else
    return true
  end
end

#tran_lang(response) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lita/handlers/translation.rb', line 133

def tran_lang(response)
  translator = MSTranslator.new(config.client_id, config.client_secret, http, redis)
  if tokenAvailable?(response, translator)
    text = response.matches.flatten[0]
    to = response.matches.flatten[1]
    from = response.matches.flatten[3]
    name = JSON.parse(redis.get("languages"))[to]
    result = translator.translate(text, to, from)
    if result.success
      response.reply(t("replies.translate", code: to, name: name, translated: result.message))
    else
      response.reply(t("replies.failure"))
      response.reply(result.message)
    end
  end
end

#updateStoredLang(codes, names) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/lita/handlers/translation.rb', line 9

def updateStoredLang(codes, names)
  languages = {}
  codes.split(",").zip(names.split(",")).each { |pair|
    languages[pair[0]] = pair[1]
  }
  redis.set("languages", languages.to_json)
  languages
end