forked from jqs7/Jqs7Bot
/
trans.go
74 lines (65 loc) · 1.55 KB
/
trans.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"strings"
"github.com/Syfaro/telegram-bot-api"
"github.com/st3v/translator"
"github.com/st3v/translator/microsoft"
)
func (p *Processor) trans(command ...string) {
f := func() {
if p.update.Message.ReplyToMessage != nil &&
p.update.Message.ReplyToMessage.Text != "" &&
len(p.s) < 2 {
in := p.update.Message.ReplyToMessage.Text
result := p.translator(in)
msg := tgbotapi.NewMessage(p.chatid(), result)
bot.SendMessage(msg)
} else if len(p.s) >= 2 {
in := strings.Join(p.s[1:], " ")
result := p.translator(in)
msg := tgbotapi.NewMessage(p.chatid(), result)
bot.SendMessage(msg)
}
}
p.hitter(f, command...)
}
type MsTrans struct {
t translator.Translator
}
func (m *MsTrans) New() {
m.t = microsoft.NewTranslator(msID, msSecret)
}
func (m *MsTrans) Detect(in string) (string, error) {
return m.t.Detect(in)
}
func (m *MsTrans) Trans(in, from, to string) (string, error) {
return m.t.Translate(in, from, to)
}
func ZhTrans(in string) (out string) {
m := &MsTrans{}
m.New()
from, err := m.Detect(in)
if err != nil {
return "警报!弹药系统过载!请放宽后重试"
}
switch from {
case "zh-CHS", "zh-CHT":
out, err = m.Trans(in, from, "en")
default:
out, err = m.Trans(in, from, "zh-CHS")
}
if err != nil {
return "可怜的群组娘被母舰放逐了X﹏X"
}
return out
}
func (p *Processor) translator(in string) string {
result := make(chan string)
typingChan := make(chan bool)
p.sendTyping(typingChan)
go func() {
result <- ZhTrans(in)
}()
<-typingChan
return <-result
}