- Portierung folgender Plugins:

+ Reddit_post
+ Eindeutschung von Reddit
+ Site_header
+ Soundcloud
+ Speedtest
+ Spotify
+ Surrogate
+ Tex
+ Xkcd
+ Yourls
- Plugin-Name wird nun angezeigt, wenn Bot getriggert wird
- Wie immer: kleine Änderungen
This commit is contained in:
Andreas Bielawski 2016-06-20 17:58:46 +02:00
parent 49bea282c1
commit 5d26c4289c
12 changed files with 336 additions and 60 deletions

View File

@ -146,6 +146,7 @@ function match_plugins(self, msg, config, plugin)
break; break;
end end
end end
print(plugin.name..' triggered')
return plugin.action(self, msg, config, matches) return plugin.action(self, msg, config, matches)
end) end)
if not success then if not success then

View File

@ -1,19 +1,16 @@
local reddit = {} local reddit = {}
local HTTP = require('socket.http') local https = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('dkjson') local JSON = require('dkjson')
local utilities = require('otouto.utilities') local utilities = require('otouto.utilities')
reddit.command = 'reddit [r/subreddit | query]' reddit.command = 'reddit [r/subreddit | Suchbegriff]'
function reddit:init(config) function reddit:init(config)
reddit.triggers = utilities.triggers(self.info.username, config.cmd_pat, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table reddit.triggers = utilities.triggers(self.info.username, config.cmd_pat, {'^/r/'}):t('reddit', true):t('r', true):t('r/', true).table
reddit.doc = [[``` reddit.doc = [[*
]]..config.cmd_pat..[[reddit [r/subreddit | query] ]]..config.cmd_pat..[[r* _[r/subreddit | Suchbegriff]_: Gibt Top-Posts oder Ergebnisse eines Subreddits aus. Wenn kein Argument gegeben ist, wird /r/all genommen.]]
Returns the top posts or results for a given subreddit or query. If no argument is given, returns the top posts from r/all. Querying specific subreddits is not supported.
Aliases: ]]..config.cmd_pat..[[r, /r/subreddit
```]]
end end
local format_results = function(posts) local format_results = function(posts)
@ -25,7 +22,7 @@ local format_results = function(posts)
title = title:sub(1, 253) title = title:sub(1, 253)
title = utilities.trim(title) .. '...' title = utilities.trim(title) .. '...'
end end
local short_url = 'redd.it/' .. post.id local short_url = 'https://redd.it/' .. post.id
local s = '[' .. title .. '](' .. short_url .. ')' local s = '[' .. title .. '](' .. short_url .. ')'
if post.domain and not post.is_self and not post.over_18 then if post.domain and not post.is_self and not post.over_18 then
s = '`[`[' .. post.domain .. '](' .. post.url:gsub('%)', '\\)') .. ')`]` ' .. s s = '`[`[' .. post.domain .. '](' .. post.url:gsub('%)', '\\)') .. ')`]` ' .. s
@ -35,9 +32,9 @@ local format_results = function(posts)
return output return output
end end
reddit.subreddit_url = 'http://www.reddit.com/%s/.json?limit=' reddit.subreddit_url = 'https://www.reddit.com/%s/.json?limit='
reddit.search_url = 'http://www.reddit.com/search.json?q=%s&limit=' reddit.search_url = 'https://www.reddit.com/search.json?q=%s&limit='
reddit.rall_url = 'http://www.reddit.com/.json?limit=' reddit.rall_url = 'https://www.reddit.com/.json?limit='
function reddit:action(msg, config) function reddit:action(msg, config)
-- Eight results in PM, four results elsewhere. -- Eight results in PM, four results elsewhere.
@ -59,7 +56,7 @@ function reddit:action(msg, config)
source = '*/' .. utilities.md_escape(input) .. '*\n' source = '*/' .. utilities.md_escape(input) .. '*\n'
else else
input = utilities.input(msg.text) input = utilities.input(msg.text)
source = '*Results for* _' .. utilities.md_escape(input) .. '_ *:*\n' source = '*Ergebnisse für* _' .. utilities.md_escape(input) .. '_ *:*\n'
input = URL.escape(input) input = URL.escape(input)
url = reddit.search_url:format(input) .. limit url = reddit.search_url:format(input) .. limit
end end
@ -67,7 +64,7 @@ function reddit:action(msg, config)
url = reddit.rall_url .. limit url = reddit.rall_url .. limit
source = '*/r/all*\n' source = '*/r/all*\n'
end end
local jstr, res = HTTP.request(url) local jstr, res = https.request(url)
if res ~= 200 then if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection) utilities.send_reply(self, msg, config.errors.connection)
else else

View File

@ -0,0 +1,55 @@
local reddit_post = {}
local https = require('ssl.https')
local URL = require('socket.url')
local json = require('dkjson')
local utilities = require('otouto.utilities')
reddit_post.triggers = {
"reddit.com/r/([A-Za-z0-9-/-_-.]+)/comments/([A-Za-z0-9-/-_-.]+)"
}
local BASE_URL = 'https://www.reddit.com'
function reddit_post:get_reddit_data(subreddit, reddit_code)
local url = BASE_URL..'/r/'..subreddit..'/comments/'..reddit_code..'.json'
local res,code = https.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
return data
end
function reddit_post:send_reddit_data(data)
local title = utilities.md_escape(data[1].data.children[1].data.title)
local author = utilities.md_escape(data[1].data.children[1].data.author)
local subreddit = utilities.md_escape(data[1].data.children[1].data.subreddit)
if string.len(data[1].data.children[1].data.selftext) > 300 then
selftext = string.sub(unescape(data[1].data.children[1].data.selftext:gsub("%b<>", "")), 1, 300) .. '...'
else
selftext = unescape(data[1].data.children[1].data.selftext:gsub("%b<>", ""))
end
if not data[1].data.children[1].data.is_self then
url = data[1].data.children[1].data.url
else
url = ''
end
local score = comma_value(data[1].data.children[1].data.score)
local comments = comma_value(data[1].data.children[1].data.num_comments)
local text = '*'..author..'* in */r/'..subreddit..'* _('..score..' Upvotes - '..comments..' Kommentare)_:\n'..title..'\n'..selftext..url
return text
end
function reddit_post:action(msg, config, matches)
local subreddit = matches[1]
local reddit_code = matches[2]
local data = reddit_post:get_reddit_data(subreddit, reddit_code)
if not data then utilities.send_reply(self, msg, config.errors.connection) return end
local text = reddit_post:send_reddit_data(data)
if not text then utilities.send_reply(self, msg, config.errors.connection) return end
utilities.send_reply(self, msg, text, true)
end
return reddit_post

View File

@ -0,0 +1,28 @@
local site_header = {}
local utilities = require('otouto.utilities')
function site_header:init(config)
site_header.triggers = {
"^/(head) ([%w-_%.%?%.:,/%+=&#!]+)$",
"^/(dig) ([%w-_%.%?%.:,/%+=&#!]+)$"
}
end
function site_header:action(msg, config, matches)
if msg.from.id ~= config.admin then
utilities.send_reply(self, msg, config.errors.sudo)
end
local url = matches[2]
if matches[1] == 'head' then
input = 'curl --head '..url
elseif matches[1] == 'dig' then
input = 'dig '..url..' ANY'
end
local output = io.popen(input):read('*all')
output = '```\n' .. output .. '\n```'
utilities.send_reply(self, msg, output, true)
end
return site_header

View File

@ -0,0 +1,41 @@
local soundcloud = {}
local http = require('socket.http')
local json = require('dkjson')
local utilities = require('otouto.utilities')
soundcloud.triggers = {
"soundcloud.com/([A-Za-z0-9-/-_-.]+)"
}
local BASE_URL = 'http://api.soundcloud.com/resolve.json'
local client_id = cred_data.soundcloud_client_id
function soundcloud:send_soundcloud_info(sc_url)
local url = BASE_URL..'?url=http://soundcloud.com/'..sc_url..'&client_id='..client_id
local res,code = http.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
local title = data.title
local description = data.description
local user = data.user.username
local user = 'Unbekannt'
local genre = data.genre
local playback_count = data.playback_count
local milliseconds = data.duration
local totalseconds = math.floor(milliseconds / 1000)
local duration = makeHumanTime(totalseconds)
local text = '*'..title..'* von _'..user..'_\n_(Tag: '..genre..', '..duration..'; '..playback_count..' mal angehört)_\n'..description
return text
end
function soundcloud:action(msg, config, matches)
local text = soundcloud:send_soundcloud_info(matches[1])
if not text then utilities.send_reply(self, msg, config.errors.connection) return end
utilities.send_reply(self, msg, text, true)
end
return soundcloud

View File

@ -0,0 +1,17 @@
local speedtest = {}
local utilities = require('otouto.utilities')
speedtest.triggers = {
"speedtest.net/my%-result/(%d+)",
"speedtest.net/my%-result/i/(%d+)"
}
function speedtest:action(msg, config, matches)
local url = 'http://www.speedtest.net/result/'..matches[1]..'.png'
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local file = download_to_file(url)
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
return speedtest

View File

@ -0,0 +1,52 @@
local spotify = {}
local https = require('ssl.https')
local json = require('dkjson')
local utilities = require('otouto.utilities')
spotify.triggers = {
"open.spotify.com/track/([A-Za-z0-9-]+)",
"play.spotify.com/track/([A-Za-z0-9-]+)"
}
local BASE_URL = 'https://api.spotify.com/v1'
function spotify:get_track_data(track)
local url = BASE_URL..'/tracks/'..track
local res,code = https.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
return data
end
function spotify:send_track_data(data, self, msg)
local name = data.name
local album = data.album.name
local artist = data.artists[1].name
local preview = data.preview_url
local milliseconds = data.duration_ms
-- convert s to mm:ss
local totalseconds = math.floor(milliseconds / 1000)
local duration = makeHumanTime(totalseconds)
local text = '*'..name..'* von *'..artist..'* aus dem Album *'..album..'* _('..duration..')_'
if preview then
utilities.send_typing(self, msg.chat.id, 'upload_audio')
local file = download_to_file(preview, name..'.mp3')
utilities.send_audio(self, msg.chat.id, file, msg.message_id, totalseconds, artist, name)
return
else
utilities.send_reply(self, msg, text, true)
return
end
end
function spotify:action(msg, config, matches)
local data = spotify:get_track_data(matches[1])
if not data then utilities.send_reply(self, msg, config.errors.connection) return end
spotify:send_track_data(data, self, msg)
return
end
return spotify

View File

@ -0,0 +1,16 @@
local surrogate = {}
local utilities = require('otouto.utilities')
surrogate.triggers = {
"^/s (%-%d+) +(.+)$",
"^/s (%d+) +(.+)$"
}
function surrogate:action(msg)
-- Supergroups don't work!?
utilities.send_message(self, matches[1], matches[2], true, nil, true)
return
end
return surrogate

34
otouto/plugins/tex.lua Normal file
View File

@ -0,0 +1,34 @@
local tex = {}
local URL = require('socket.url')
local utilities = require('otouto.utilities')
tex.command = 'tex <LaTeX>'
function tex:init(config)
tex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('tex', true).table
tex.doc = [[*
]]..config.cmd_pat..[[tex* _<LaTeX>_: Konvertiert LaTeX in ein Bild]]
end
function tex:action(msg, config)
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_message(self, msg.chat.id, tex.doc, true, msg.message_id, true)
return
end
end
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local eq = URL.escape(input)
local url = "http://latex.codecogs.com/png.download?"
.."\\dpi{300}%20\\LARGE%20"..eq
local file = download_to_file(url, 'latex.png')
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)
end
return tex

View File

@ -1,58 +1,37 @@
local xkcd = {} local xkcd = {}
local HTTP = require('socket.http') local http = require('socket.http')
local JSON = require('dkjson') local json = require('dkjson')
local utilities = require('otouto.utilities') local utilities = require('otouto.utilities')
xkcd.command = 'xkcd [i]' xkcd.command = 'xkcd [i]'
function xkcd:init(config) function xkcd:init(config)
xkcd.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('xkcd', true).table xkcd.triggers = {
xkcd.doc = [[``` "^/xkcd (%d+)",
]]..config.cmd_pat..[[xkcd [i] "xkcd.com/(%d+)"
Returns the latest xkcd strip and its alt text. If a number is given, returns that number strip. If "r" is passed in place of a number, returns a random strip. }
```]] xkcd.doc = [[*
]]..config.cmd_pat..[[xkcd* _[i]_: Gibt diesen XKCD-Comic aus]]
end
function xkcd:get_xkcd(id)
local res,code = http.request("http://xkcd.com/"..id.."/info.0.json")
if code ~= 200 then return nil end
local data = json.decode(res)
local link_image = data.img
if link_image:sub(0,2) == '//' then
link_image = link_image:sub(3,-1)
end
return link_image, data.title, data.alt
end end
function xkcd:action(msg, config) function xkcd:action(msg, config)
local url, title, alt = xkcd:get_xkcd(matches[1])
local jstr, res = HTTP.request('http://xkcd.com/info.0.json') if not url then utilities.send_reply(self, msg, config.errors.connection) return end
if res ~= 200 then utilities.send_typing(self, msg.chat.id, 'upload_photo')
utilities.send_reply(self, msg, config.errors.connection) local file = download_to_file(url)
return utilities.send_photo(self, msg.chat.id, file, title..'\n'..alt, msg.message_id)
end
local latest = JSON.decode(jstr).num
local strip_num = latest
local input = utilities.input(msg.text)
if input then
if input == '404' then
utilities.send_message(self, msg.chat.id, '*404*\nNot found.', false, nil, true)
return
elseif tonumber(input) then
if tonumber(input) > latest then
strip_num = latest
else
strip_num = input
end
elseif input == 'r' then
strip_num = math.random(latest)
end
end
local res_url = 'http://xkcd.com/' .. strip_num .. '/info.0.json'
jstr, res = HTTP.request(res_url)
if res ~= 200 then
utilities.send_reply(self, msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
local output = '*' .. jdat.safe_title .. ' (*[' .. jdat.num .. '](' .. jdat.img .. ')*)*\n_' .. jdat.alt:gsub('_', '\\_') .. '_'
utilities.send_message(self, msg.chat.id, output, false, nil, true)
end end
return xkcd return xkcd

57
otouto/plugins/yourls.lua Normal file
View File

@ -0,0 +1,57 @@
local yourls = {}
local http = require('socket.http')
local https = require('ssl.https')
local utilities = require('otouto.utilities')
function yourls:init(config)
if not cred_data.yourls_site_url then
print('Missing config value: yourls_site_url.')
print('yourls.lua will not be enabled.')
return
elseif not cred_data.yourls_signature_token then
print('Missing config value: yourls_signature_token.')
print('yourls.lua will not be enabled.')
return
end
yourls.triggers = {
"^/yourls (https?://[%w-_%.%?%.:/%+=&]+)"
}
end
local SITE_URL = cred_data.yourls_site_url
local signature = cred_data.yourls_signature_token
local BASE_URL = SITE_URL..'/yourls-api.php'
function yourls:prot_url(url)
local url, h = string.gsub(url, "http://", "")
local url, hs = string.gsub(url, "https://", "")
local protocol = "http"
if hs == 1 then
protocol = "https"
end
return url, protocol
end
function yourls:create_yourls_link(long_url, protocol)
local url = BASE_URL..'?format=simple&signature='..signature..'&action=shorturl&url='..long_url
if protocol == "http" then
link,code = http.request(url)
else
link,code = https.request(url)
end
if code ~= 200 then
link = 'Ein Fehler ist aufgetreten. '..link
end
return link
end
function yourls:action(msg, config, matches)
local long_url = matches[1]
local baseurl, protocol = yourls:prot_url(SITE_URL)
utilities.send_reply(self, msg, yourls:create_yourls_link(long_url, protocol))
return
end
return yourls

View File

@ -50,10 +50,9 @@ function utilities:send_photo(chat_id, file, text, reply_to_message_id)
end end
-- https://core.telegram.org/bots/api#sendaudio -- https://core.telegram.org/bots/api#sendaudio
function utilities:send_audio(chat_id, file, text, reply_to_message_id, duration, performer, title) function utilities:send_audio(chat_id, file, reply_to_message_id, duration, performer, title)
local output = bindings.request(self, 'sendAudio', { local output = bindings.request(self, 'sendAudio', {
chat_id = chat_id, chat_id = chat_id,
caption = text or nil,
duration = duration or nil, duration = duration or nil,
performer = performer or nil, performer = performer or nil,
title = title or nil, title = title or nil,