This repository has been archived by the owner on Oct 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send message via Telegram official bot API.
- Loading branch information
Sahri Riza Umami
committed
Jan 14, 2016
1 parent
7a8c841
commit e4db6e5
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
do | ||
|
||
local function run(msg, matches) | ||
|
||
if is_chat_msg(msg) then | ||
thread_limit = 5 | ||
receiver = '-'..msg.to.id | ||
else | ||
thread_limit = 8 | ||
receiver = msg.from.id | ||
end | ||
|
||
if matches[1] == 'nsfw' then | ||
is_nsfw = true | ||
else | ||
is_nsfw = false | ||
end | ||
|
||
if matches[2] then | ||
if matches[2]:match('^r/') then | ||
url = 'http://www.reddit.com/'..matches[2]..'/.json?limit='..thread_limit | ||
else | ||
url = 'http://www.reddit.com/search.json?q='..matches[2]..'&limit='..thread_limit | ||
end | ||
elseif msg.text == '!reddit' then | ||
url = 'http://www.reddit.com/.json?limit='..thread_limit | ||
end | ||
|
||
-- Do the request | ||
local res, code = https.request(url) | ||
if code ~=200 then return nil end | ||
|
||
local jdat = json:decode(res) | ||
if #jdat.data.children == 0 then | ||
return nil | ||
end | ||
|
||
if _config.bot_api_key ~= '' then | ||
local subreddit = '*'..(matches[2] or 'redd.it')..'*\n' | ||
for i,v in ipairs(jdat.data.children) do | ||
local long_url = '\n' | ||
if not v.data.is_self then | ||
long_url = '\n[['..v.data.url..']]\n' | ||
end | ||
local title = unescape_html(v.data.title) | ||
-- TODO : escape square brackets from Telegrams markdown parser | ||
local title = string.gsub(title, '%]', ')') | ||
local title = string.gsub(title, '%[', '(') | ||
if v.data.over_18 and not is_nsfw then | ||
subreddit = '' | ||
elseif v.data.over_18 and is_nsfw then | ||
subreddit = subreddit..i..'. *NSFW* '..'['..title..'](redd.it/'..v.data.id..')'..long_url | ||
else | ||
subreddit = subreddit..i..'. '..'['..title..'](redd.it/'..v.data.id..')'..long_url | ||
end | ||
end | ||
sendAPIMessage(receiver, subreddit, true) | ||
else | ||
local subreddit = (matches[2] or 'redd.it')..'\n' | ||
for i,v in ipairs(jdat.data.children) do | ||
local long_url = '\n' | ||
if not v.data.is_self then | ||
long_url = '\n'..v.data.url..'\n' | ||
end | ||
local title = unescape_html(v.data.title) | ||
if v.data.over_18 and not is_nsfw then | ||
subreddit = '' | ||
elseif v.data.over_18 and is_nsfw then | ||
subreddit = subreddit..i..'. NSFW '..'[redd.it/'..v.data.id..'] '..title..long_url | ||
else | ||
subreddit = subreddit..i..'. '..'[redd.it/'..v.data.id..'] '..title..long_url | ||
end | ||
end | ||
return subreddit | ||
end | ||
end | ||
|
||
-------------------------------------------------------------------------------- | ||
|
||
return { | ||
description = 'Returns the five (if group) or eight (if private message) top posts for the given subreddit or query, or from the frontpage.', | ||
usage = { | ||
'!reddit : Reddit frontpage.', | ||
'!reddit r/[query] : Subreddit', | ||
'!redditnsfw [query] : Subreddit (include NSFW).', | ||
'!r [query] : Subreddit.', | ||
'!rnsfw [query] : Subreddit (include NSFW).' | ||
}, | ||
patterns = { | ||
'^!reddit$', | ||
'^!(r) (.*)$', | ||
'^!(reddit) (.*)$', | ||
'^!r(nsfw) (.*)$', | ||
'^!reddit(nsfw) (.*)$' | ||
}, | ||
run = run | ||
} | ||
|
||
end |