Skip to content
This repository has been archived by the owner on Oct 14, 2018. It is now read-only.

Commit

Permalink
Send message via Telegram official bot API.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sahri Riza Umami committed Jan 14, 2016
1 parent 7a8c841 commit e4db6e5
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
1 change: 1 addition & 0 deletions bot/bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ end
function create_config( )
-- A simple config with basic plugins and ourselves as privileged user
config = {
bot_api_key = '',
enabled_plugins = {
"9gag",
"eur",
Expand Down
23 changes: 23 additions & 0 deletions bot/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -547,3 +547,26 @@ function backward_msg_format (msg)
end
return msg
end


-- There is no (yet) lua binding to send markdown message by tg-cli.
-- So, this is a hackish workaround; send message through Telegram official API.
-- You need to provide your API bots TOKEN in config.lua.
function sendAPIMessage(receiver, text, disable_web_page_preview)
local url_api = 'https://api.telegram.org/bot'.._config.bot_api_key
..'/sendMessage?chat_id='..receiver..'&text='..URL.escape(text)
..'&parse_mode=Markdown'
if disable_web_page_preview == true then
url_api = url_api..'&disable_web_page_preview=true'
end
print(url_api)
local dat, res = https.request(url_api)
local tab = json:decode(dat)
if res ~= 200 then
return false, res
end
if not tab.ok then
return false, tab.description
end
return tab
end
99 changes: 99 additions & 0 deletions plugins/reddit.lua
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

0 comments on commit e4db6e5

Please sign in to comment.