forked from wrxck/mattata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mattata-ai.lua
158 lines (147 loc) · 3.82 KB
/
mattata-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
--[[
_ _ _ _
_ __ ___ __ _| |_| |_ __ _| |_ __ _ __ _(_)
| '_ ` _ \ / _` | __| __/ _` | __/ _` |_____ / _` | |
| | | | | | (_| | |_| || (_| | || (_| |_____| (_| | |
|_| |_| |_|\__,_|\__|\__\__,_|\__\__,_| \__,_|_|
Copyright (c) 2017 Matthew Hesketh
See LICENSE for details
mattata-ai is a basic AI implementation, hooked to Cleverbot, written in Lua.
Intended for use with the mattata library, a feature-packed Telegram bot.
]]
local ai = {}
local https = require('ssl.https')
local http = require('socket.http')
local url = require('socket.url')
local ltn12 = require('ltn12')
local json = require('dkjson')
local digest = require('openssl.digest')
local html = require('htmlEntities')
local redis = require('mattata-redis')
or false
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.cleverbot_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.cleverbot_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='
query = query .. ai.str_to_hex(
digest.new('md5'):final(
query:sub(8, 33)
)
)
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
return ai