Page Menu
Home
Phabricator (Chris)
Search
Configure Global Search
Log In
Files
F627482
calc.lua
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
1 KB
Referenced Files
None
Subscribers
None
calc.lua
View Options
--[[
mattata v2.0 - Calculator Plugin
Evaluates mathematical expressions using the mathjs.org API.
]]
local
plugin
=
{}
plugin
.
name
=
'calc'
plugin
.
category
=
'utility'
plugin
.
description
=
'Evaluate mathematical expressions'
plugin
.
commands
=
{
'calc'
,
'calculate'
,
'calculator'
}
plugin
.
help
=
'/calc <expression> - Evaluate a mathematical expression.'
function
plugin
.
on_message
(
api
,
message
,
ctx
)
local
https
=
require
(
'ssl.https'
)
local
url
=
require
(
'socket.url'
)
local
tools
=
require
(
'telegram-bot-lua.tools'
)
local
input
=
message
.
args
if
not
input
or
input
==
''
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Please provide an expression to evaluate. Example: /calc 2+2*5'
)
end
local
encoded
=
url
.
escape
(
input
)
local
api_url
=
'https://api.mathjs.org/v4/?expr='
..
encoded
local
body
,
status
=
https
.
request
(
api_url
)
if
not
body
or
status
~=
200
then
return
api
.
send_message
(
message
.
chat
.
id
,
'Failed to evaluate that expression. Please check the syntax and try again.'
)
end
-- mathjs returns the result as plain text
local
result
=
body
:
match
(
'^%s*(.-)%s*$'
)
if
not
result
or
result
==
''
then
return
api
.
send_message
(
message
.
chat
.
id
,
'No result returned for that expression.'
)
end
return
api
.
send_message
(
message
.
chat
.
id
,
string.format
(
'<b>Expression:</b> <code>%s</code>
\n
<b>Result:</b> <code>%s</code>'
,
tools
.
escape_html
(
input
),
tools
.
escape_html
(
result
)),
'html'
)
end
return
plugin
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Nov 15, 2:09 AM (1 d, 21 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
131991
Default Alt Text
calc.lua (1 KB)
Attached To
Mode
R69 mattata
Attached
Detach File
Event Timeline