fix stuff

This commit is contained in:
topkecleon
2016-06-07 00:09:26 -04:00
parent 38fd3561de
commit a97457aa01
3 changed files with 1 additions and 1 deletions

70
otouto/plugins/bing.lua Normal file
View File

@@ -0,0 +1,70 @@
-- Credit to Juan (tg:JuanPotato; gh:JuanPotato) for this plugin.
-- Or rather, the seven lines that actually mean anything.
local bing = {}
local URL = require('socket.url')
local JSON = require('dkjson')
local mime = require('mime')
local https = require('ssl.https')
local ltn12 = require('ltn12')
local utilities = require('utilities')
bing.command = 'bing <query>'
bing.doc = [[```
/bing <query>
Returns the top web search results from Bing.
Aliases: /g, /google
```]]
bing.search_url = 'https://api.datamarket.azure.com/Data.ashx/Bing/Search/Web?Query=\'%s\'&$format=json'
function bing:init()
if not self.config.bing_api_key then
print('Missing config value: bing_api_key.')
print('bing.lua will not be enabled.')
return
end
bing.triggers = utilities.triggers(self.info.username):t('bing', true):t('g', true):t('google', true).table
end
function bing:action(msg)
local input = utilities.input(msg.text)
if not input then
if msg.reply_to_message and msg.reply_to_message.text ~= '' then
input = msg.reply_to_message.text
else
utilities.send_reply(self, msg, bing.doc, true)
return
end
end
local url = bing.search_url:format(URL.escape(input))
local resbody = {}
local _,b,_ = https.request{
url = url,
headers = { ["Authorization"] = "Basic " .. mime.b64(":" .. self.config.bing_api_key) },
sink = ltn12.sink.table(resbody),
}
if b ~= 200 then
utilities.send_reply(self, msg, self.config.errors.results)
return
end
local dat = JSON.decode(table.concat(resbody))
local limit = 4
if msg.chat.type == 'private' then
limit = 8
end
if limit > #dat.d.results then
limit = #dat.d.results
end
local reslist = {}
for i = 1, limit do
local result = dat.d.results[i]
local s = '• [' .. result.Title:gsub('%]', '\\]') .. '](' .. result.Url:gsub('%)', '\\)') .. ')'
table.insert(reslist, s)
end
local output = '*Search results for* _' .. utilities.md_escape(input) .. '_ *:*\n' .. table.concat(reslist, '\n')
utilities.send_message(self, msg.chat.id, output, true, nil, true)
end
return bing

View File

@@ -0,0 +1,65 @@
local channel = {}
local bindings = require('bindings')
local utilities = require('utilities')
--channel.command = 'ch <channel> \\n <message>'
channel.doc = [[```
/ch <channel>
<message>
Sends a message to a channel. Channel may be specified via ID or username. Messages are markdown-enabled. Users may only send messages to channels for which they are the owner or an administrator.
The following markdown syntax is supported:
*bold text*
_italic text_
[text](URL)
`inline fixed-width code`
```pre-formatted fixed-width code block```
Due to the frequent dysfunction and incompletion of the API method used to determine the administrators of a channel, this command may not work for the owners of some channels.
```]]
function channel:init()
channel.triggers = utilities.triggers(self.info.username):t('ch', true).table
end
function channel:action(msg)
-- An exercise in using zero early returns. :)
local input = utilities.input(msg.text)
local output
if input then
local chat_id = utilities.get_word(input, 1)
local admin_list, t = bindings.getChatAdministrators(self, { chat_id = chat_id } )
if admin_list then
local is_admin = false
for _, admin in ipairs(admin_list.result) do
if admin.user.id == msg.from.id then
is_admin = true
end
end
if is_admin then
local text = input:match('\n(.+)')
if text then
local success, result = utilities.send_message(self, chat_id, text, true, nil, true)
if success then
output = 'Your message has been sent!'
else
output = 'Sorry, I was unable to send your message.\n`' .. result.description .. '`'
end
else
output = 'Please enter a message to be sent. Markdown is supported.'
end
else
output = 'Sorry, you do not appear to be an administrator for that channel.\nThere is currently a known bug in the getChatAdministrators method, where administrator lists will often not show a channel\'s owner.'
end
else
output = 'Sorry, I was unable to retrieve a list of administrators for that channel.\n`' .. t.description .. '`'
end
else
output = channel.doc
end
utilities.send_reply(self, msg, output, true)
end
return channel