-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai.lua
395 lines (370 loc) · 12.7 KB
/
ai.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
--[[
_ _ _ _
_ __ ___ __ _| |_| |_ __ _| |_ __ _ __ _(_)
| '_ ` _ \ / _` | __| __/ _` | __/ _` |_____ / _` | |
| | | | | | (_| | |_| || (_| | || (_| |_____| (_| | |
|_| |_| |_|\__,_|\__|\__\__,_|\__\__,_| \__,_|_|
v1.0.0
Copyright (c) 2017 Matthew Hesketh <[email protected]>
See LICENSE for details
mattata-ai is a basic AI implementation, hooked to Cleverbot, written in Lua.
]]
local ai = {}
local configuration = require('configuration')
local api = require('telegram-bot-lua.core').configure(configuration.bot_token)
local tools = require('telegram-bot-lua.tools')
local redis = dofile('libs/redis.lua')
local http = require('socket.http')
local url = require('socket.url')
local ltn12 = require('ltn12')
local digest = require('openssl.digest')
function ai:init()
self.info = api.info -- Set the bot's information to the object fetched from the Telegram bot API.
print('Connected to the Telegram bot API!')
print('\n\tUsername: @' .. self.info.username .. '\n\tName: ' .. self.info.name .. '\n\tID: ' .. self.info.id .. '\n')
self.version = 'v1.0.0'
self.last_update = self.last_update or 0 -- If there is no last update known, make it 0 so the bot doesn't encounter any
-- problems when it tries to add the necessary increment.
return true
end
function ai:run(configuration, token)
-- mattata-ai's main long-polling function which repeatedly checks the Telegram bot API for updates.
-- The objects received in the updates are then further processed through object-specific functions.
token = token or configuration.bot_token
assert(token, 'You need to enter your Telegram bot API token in configuration.lua, or pass it as the second argument when using the ai:run() function!')
local is_running = ai.init(self) -- Initialise the bot.
while is_running do -- Perform the main loop whilst the bot is running.
-- Check the Telegram bot API for updates.
local success = api.get_updates(1, self.last_update + 1, 20, nil, false)
if success and success.result then
for k, v in ipairs(success.result) do
self.last_update = v.update_id
if v.message then
if v.message.reply_to_message then
v.message.reply = v.message.reply_to_message -- Make the `update.message.reply_to_message`
-- object `update.message.reply` to make any future handling easier.
v.message.reply_to_message = nil -- Delete the old value by setting its value to nil.
end
ai.on_message(self, v.message)
if configuration.debug then
print(
string.format(
'%s[36m[Update #%s] Message from %s to %s%s[0m',
string.char(27),
v.update_id,
v.message.from.id,
v.message.chat.id,
string.char(27)
)
)
end
end
end
else
print(
string.format(
'%s[31m[Error] There was an error retrieving updates from the Telegram bot API!%s[0m',
string.char(27),
string.char(27)
)
)
end
end
print(self.info.first_name .. ' is shutting down...')
end
function ai.on_message(self, message)
if not message or not message.chat or not message.text then
return false
elseif message.date < os.time() - 10 then
return false
elseif self.info.name:match(' ') then
self.info.name = self.info.name:match('^(.-) ')
end
redis:incr('ai:received_messages')
self.info.name = self.info.name:lower()
message.text = message.text:lower()
if message.text:gsub(' ', '') == self.info.name then
return false
elseif message.chat.type ~= 'private' and not message.text:match(self.info.name) and not message.text:lower():match("bot") then
if not message.reply or not message.reply.from or message.reply.from.id ~= self.info.id then
return false
end
end
message.text = message.text:gsub(self.info.name, '')
api.send_chat_action(message.chat.id)
local output
if message.reply and message.reply.text and message.reply.text:len() > 0 and message.reply.from and message.reply.from.id == self.info.id then
output = ai.process(message.text, message.reply.text)
else
output = ai.process(message.text)
end
if not output then
if message.reply and message.reply.text and message.reply.text:len() > 0 and message.reply.from and message.reply.from.id == self.info.id then
output = ai.process(message.text, message.reply.text, true)
else
output = ai.process(message.text)
end
end
return ai.send_reply(message, output and '<pre>' .. tools.escape_html(output) .. '</pre>' or '<pre>' .. tools.escape_html(ai.offline()) .. '</pre>', 'html')
end
function ai.get_me(token)
token = token or configuration.bot_token
return ai.request(string.format('https://api.telegram.org/bot%s/getMe', token))
end
-- A variant of `ai.send_message()`, optimised for sending a message as a reply.
function ai.send_reply(message, text, parse_mode, disable_web_page_preview, reply_markup, token)
local success = api.send_message(
message,
text,
parse_mode,
disable_web_page_preview,
false,
message.message_id,
reply_markup
or '{"remove_keyboard":true}',
token
)
if not success
then
success = api.send_message(
message,
text,
parse_mode,
disable_web_page_preview,
false,
message.message_id,
reply_markup,
token
)
end
redis:incr('ai:sent_replies')
return success
end
function ai:exception(err, message, log_chat)
local output = string.format(
'[%s]\n%s: %s\n%s\n',
os.date('%X'),
self.info.username,
tools.escape_html(err)
or '',
tools.escape_html(message)
)
if log_chat then
return ai.send_message(log_chat, '<pre>' .. output .. '</pre>', 'html')
end
return true
end
function ai.num_to_hex(int)
local hex = '0123456789abcdef'
local s = ''
while int > 0 do
local mod = math.fmod(int, 16)
s = hex:sub(mod + 1, mod +1 ) .. s
int = math.floor(int / 16)
end
if s == '' then
s = '0'
end
return s
end
function ai.str_to_hex(str)
local s = ''
while #str > 0 do
local h = ai.num_to_hex(str:byte(1, 1))
if #h < 2 then
h = '0' .. h
end
s = s .. h
str = str:sub(2)
end
return s
end
function ai.unescape(str)
if not str then
return false
end
str = str:gsub('%%(%x%x)', function(x)
return tostring(tonumber(x, 16)):char()
end)
return str
end
function ai.cookie()
local cookie = {}
local _, res, headers = http.request{
['url'] = 'http://www.cleverbot.com/',
['method'] = 'GET'
}
if res ~= 200 then
return false
end
local set = headers['set-cookie']
local k, v = set:match('([^%s;=]+)=?([^%s;]*)')
cookie[k] = v
return cookie
end
function ai.talk(message, reply)
if not message then
return false
end
return ai.cleverbot(message, reply)
end
function ai.cleverbot(message, reply)
local cookie = ai.cookie()
if not cookie then
return false
end
for k, v in pairs(cookie) do
cookie[#cookie + 1] = k .. '=' .. v
end
local query = 'stimulus=' .. url.escape(message)
if reply then
query = query .. '&vText2=' .. url.escape(reply)
end
query = query .. '&cb_settings_scripting=no&islearning=1&icognoid=wsf&icognocheck='
local sub = query:sub(8, 33)
local digested = digest.new('md5'):final(sub)
query = query .. ai.str_to_hex(digested)
local _, res, headers = http.request(
{
['url'] = 'http://www.cleverbot.com/webservicemin?uc=UseOfficialCleverbotAPI&',
['method'] = 'POST',
['headers'] = {
['Host'] = 'www.cleverbot.com',
['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0',
['Accept'] = '*/*',
['Accept-Language'] = 'en-US,en;q=0.5',
['Accept-Encoding'] = 'gzip, deflate',
['Referrer'] = 'http://www.cleverbot.com/',
['Content-Length'] = query:len(),
['Content-Type'] = 'text/plain;charset=UTF-8',
['Cookie'] = table.concat(cookie, ';'),
['DNT'] = '1',
['Connection'] = 'keep-alive'
},
['source'] = ltn12.source.string(query)
}
)
if res ~= 200 or not headers.cboutput then
return false
end
local output = ai.unescape(headers.cboutput)
if not output then
return false
end
return output
end
function ai.process(message, reply)
if not message then
return ai.unsure()
end
local original_message = message
message = message:lower()
if message:match('^hi%s*') or message:match('^hello%s*') or message:match('^howdy%s*') or message:match('^hi.?$') or message:match('^hello.?$') or message:match('^howdy.?$') then
return ai.greeting()
elseif message:match('^bye%s*') or message:match('^good[%-%s]?bye%s*') or message:match('^bye$') or message:match('^good[%-%s]?bye$') then
return ai.farewell()
elseif message:match('%s*y?o?ur%s*name%s*') or message:match('^what%s*is%s*y?o?ur%s*name') then
return 'My name is ai, what\'s yours?'
elseif message:match('^do y?o?u[%s.]*') then
return ai.choice(message)
elseif message:match('^how%s*a?re?%s*y?o?u.?') or message:match('.?how%s*a?re?%s*y?o?u%s*') or message:match('.?how%s*a?re?%s*y?o?u.?$') or message:match('^a?re?%s*y?o?u%s*oka?y?.?$') or message:match('%s*a?re?%s*y?o?u%s*oka?y?.?$') then
return ai.feeling()
else
return ai.talk(original_message, reply or false)
end
end
function ai.greeting()
local greetings = {
'Hello!',
'Hi.',
'How are you?',
'What\'s up?',
'Are you okay?',
'How\'s it going?',
'What\'s your name?',
'What are you up to?',
'Hello.',
'Hey!',
'Hey.',
'Howdy!',
'Howdy.',
'Hello there!',
'Hello there.'
}
return greetings[math.random(#greetings)]
end
function ai.farewell()
local farewells = {
'Goodbye!',
'Bye.',
'I\'ll speak to you later, yeah?',
'See ya!',
'Oh, bye then.',
'Bye bye.',
'BUH-BYE!',
'Aw. See ya.'
}
return farewells[math.random(#farewells)]
end
function ai.unsure()
local unsure = {
'What?',
'I really don\'t understand.',
'What are you trying to say?',
'Huh?',
'Um..?',
'Excuse me?',
'What does that mean?'
}
return unsure[math.random(#unsure)]
end
function ai.feeling()
local feelings = {
'I am good thank you!',
'I am well.',
'Good, how about you?',
'Very well thank you; you?',
'Never better!',
'I feel great!'
}
return feelings[math.random(#feelings)]
end
function ai.choice(message)
local generic_choices = {
'I do!',
'I do not.',
'Nah, of course not!',
'Why would I?',
'Um...',
'I sure do!',
'Yes, do you?',
'Nope!',
'Yeah!'
}
local personal_choices = {
'I love you!',
'I\'m sorry, but I don\'t really like you!',
'I really like you.',
'I\'m crazy about you!'
}
if message:match('%s*me.?$')
then
return personal_choices[math.random(#personal_choices)]
end
return generic_choices[math.random(#generic_choices)]
end
function ai.offline()
local responses = {
'I don\'t feel like talking right now!',
'I don\'t want to talk at the moment.',
'Can we talk later?',
'I\'m not in the mood right now...',
'Leave me alone!',
'Please can I have some time to myself?',
'I really don\'t want to talk to anyone right now!',
'Please leave me in peace.',
'I don\'t wanna talk right now, I hope you understand.'
}
return responses[math.random(#responses)]
end
return ai