-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranslate.lua
209 lines (169 loc) · 5.75 KB
/
translate.lua
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
local log = require("log")
local Api = require("coreApi")
local json = require("json")
local http = require("http")
function ReceiveFriendMsg(CurrentQQ, data)
if data.FromUin ==2986807981 then--防止自我复读
return 1 end
if string.find(data.Content, "翻译成") == 1 then
local keyWord ="" --涙じゃない。目から尿が出ているだけなんだ
local sl = "auto" --默认自动匹配原语言'
local tl = "en" --默认翻译英语
else
return 1 --没有就退出
end
if string.find(data.Content, "翻译成中文:") == 1 then
keyWord = data.Content:gsub("翻译成中文:", "")
tl="zh-CN"
end
if string.find(data.Content, "翻译成英语:") == 1 then
keyWord = data.Content:gsub("翻译成英语:", "")
tl="en"
end
if string.find(data.Content, "翻译成英文:") == 1 then
keyWord = data.Content:gsub("翻译成英文:", "")
tl="en"
end
if string.find(data.Content, "翻译成日语:") == 1 then
keyWord = data.Content:gsub("翻译成日语:", "")
tl="ja-JP"
end
if string.find(data.Content, "翻译成日文:") == 1 then
keyWord = data.Content:gsub("翻译成日文:", "")
tl="ja-JP"
end
if keyWord ~="" then
keyWord = url_encode(keyWord)
log.notice("keyWord-->%s",keyWord)
response, error_message =
http.request(
"GET",
"http://translate.google.cn/translate_a/single?",
{
query = "client=gtx&sl="..sl.."&tl="..tl.."&dt=t&q="..keyWord ,
headers = {
--["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
}
}
)
local html = response.body
log.notice("html-->%s",html)
local re = json.decode(html)
local resultstr = re[1][1][1]
--log.notice("resultstr =%s",resultstr)
sendfriendresult(CurrentQQ,data,resultstr)
keyWord =nil --清理
sl = nil
tl = nil
end
end
return 1
end
function ReceiveGroupMsg(CurrentQQ, data)
if data.FromUserId ==2986807981 then--防止自我复读
return 1 end
if string.find(data.Content, "翻译成") == 1 then
local keyWord ="" --涙じゃない。目から尿が出ているだけなんだ
local sl = "auto" --默认自动匹配原语言'
local tl = "en" --默认翻译英语
else
return 1 --没有就退出
end
if string.find(data.Content, "翻译成中文:") == 1 then
keyWord = data.Content:gsub("翻译成中文:", "")
tl="zh-CN"
end
if string.find(data.Content, "翻译成英语:") == 1 then
keyWord = data.Content:gsub("翻译成英语:", "")
tl="en"
end
if string.find(data.Content, "翻译成英文:") == 1 then
keyWord = data.Content:gsub("翻译成英文:", "")
tl="en"
end
if string.find(data.Content, "翻译成日语:") == 1 then
keyWord = data.Content:gsub("翻译成日语:", "")
tl="ja-JP"
end
if string.find(data.Content, "翻译成日文:") == 1 then
keyWord = data.Content:gsub("翻译成日文:", "")
tl="ja-JP"
end
if keyWord ~="" then
keyWord = url_encode(keyWord)
log.notice("keyWord-->%s",keyWord)
response, error_message =
http.request(
"GET",
"http://translate.google.cn/translate_a/single?",
{
query = "client=gtx&sl="..sl.."&tl="..tl.."&dt=t&q="..keyWord ,
headers = {
--["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
}
}
)
local html = response.body
log.notice("html-->%s",html)
local re = json.decode(html)
local resultstr = re[1][1][1]
--log.notice("resultstr =%s",resultstr)
sendgroupresult(CurrentQQ,data,resultstr)
keyWord =nil --清理
sl = nil
tl = nil
end
end
return 1
end
function ReceiveEvents(CurrentQQ, data, extData)
return 1
end
function sendgroupresult(CurrentQQ,data,resultstr)
if resultstr == "" then return 1 end --空则退出
ApiRet =
Api.Api_SendMsg(
CurrentQQ,
{
toUser = data.FromGroupId, --回复当前消息的来源群ID
sendToType = 2, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg",
groupid = 0, --不是私聊自然就为0咯
content = "频率有限制请勿滥用, 翻译结果:"..resultstr,
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
return 1
end
function sendfriendresult(CurrentQQ,data,resultstr)
if resultstr =="" then return 1 end --空则退出
ApiRet =
Api.Api_SendMsg(
CurrentQQ,
{
toUser = data.FromUin, --回复当前消息的来源群ID
sendToType = 1, --2发送给群1发送给好友3私聊
sendMsgType = "TextMsg", --进行文本复读回复
groupid = 0, --不是私聊自然就为0咯
content = "频率有限制请勿滥用, 翻译结果:"..resultstr, --回复内容
atUser = 0 --是否 填上data.FromUserId就可以复读给他并@了
}
)
return 1
end
function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w ])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function url_decode(str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)",
function(h) return string.char(tonumber(h,16)) end)
str = string.gsub (str, "\r\n", "\n")
return str
end