Skip to content

Commit

Permalink
Add optional message forwarding
Browse files Browse the repository at this point in the history
  • Loading branch information
un-def committed Jul 14, 2020
1 parent da70594 commit 48034c2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ _M.log = function(...)
message = tostring(message):format(select(args_offset, ...))
end
local info = debug_getinfo(2, 'Sln')
message = ('%s:%s: %s():\n\n*** [%s] %s\n'):format(
message = ('%s:%s: %s:\n\n*** [%s] %s\n'):format(
info.short_src:match('//(app/.+)'), info.currentline,
info.name, LOG_LEVEL_NAMES[level], message)
raw_log(level, message)
Expand Down
55 changes: 50 additions & 5 deletions app/views/webhook.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ local helpers = require('app.views.helpers')
local get_file_from_message = require('app.tg').get_file_from_message

local config = require('app.config')
local tg = require('app.tg')


local yield = coroutine.yield

local ngx_say = ngx.say
local ngx_req = ngx.req
local ngx_header = ngx.header
local ngx_thread_spawn = ngx.thread.spawn
local ngx_HTTP_NOT_FOUND = ngx.HTTP_NOT_FOUND
local ngx_INFO = ngx.INFO
local ngx_ERR = ngx.ERR

local json_encode = json.encode
local json_decode = json.decode
Expand All @@ -28,11 +34,15 @@ local GET_FILE_MODES = constants.GET_FILE_MODES

local tg_bot_username = config.tg.bot_username
local tg_webhook_secret = config._processed.tg_webhook_secret
local tg_forward_chat_id = config.tg.forward_chat_id
local hide_image_download_link = config.hide_image_download_link
local link_url_prefix = config._processed.link_url_prefix
local enable_upload = config._processed.enable_upload
local enable_upload_api = config._processed.enable_upload_api

local prepare_connection = tg.prepare_connection
local request_tg_server = tg.request_tg_server

local render_link_factory = helpers.render_link_factory
local render_to_string = helpers.render_to_string
local markdown_escape = helpers.markdown_escape
Expand All @@ -56,6 +66,37 @@ local send_webhook_response = function(message, template_path, context)
end


local forward_message = function(message)
-- yield to return from ngx.thread.spawn ASAP
yield()
local conn, err = prepare_connection()
if not conn then
log(ngx_ERR, 'tg api connection error: %s', err)
return
end
local params = {
path = '/bot%s/forwardMessage',
method = 'POST',
headers = {
['content-type'] = 'application/json',
},
body = json_encode{
chat_id = tg_forward_chat_id,
from_chat_id = message.chat.id,
message_id = message.message_id,
},
}
local res, err = request_tg_server(conn, params, true) -- luacheck: ignore 411
if not res then
log(ngx_ERR, 'tg api request error: %s', err)
return
end
if not res.ok then
log(ngx_INFO, 'tg api response is not "ok": %s', res.description)
end
end


return {

initial = function(secret)
Expand All @@ -76,6 +117,15 @@ return {

local is_groupchat = message.chat.type ~= TG_CHAT_PRIVATE

-- tricky way to ignore groupchat service messages (e.g., new_chat_member)
if is_groupchat and not message.reply_to_message then
return
end

if tg_forward_chat_id then
ngx_thread_spawn(forward_message, message)
end

if message.text then
local command, bot_username = message.text:match('^/([%w_]+)@?([%w_]*)')
if not command then
Expand All @@ -92,11 +142,6 @@ return {
})
end

-- tricky way to ignore groupchat service messages (e.g., new_chat_member)
if is_groupchat and not message.reply_to_message then
return
end

local file, err = get_file_from_message(message)
if not file then
log(err)
Expand Down
3 changes: 3 additions & 0 deletions config.example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ return {
-- chat_id (integer or string) for uploaded files
-- set to nil to disable http uploading
upload_chat_id = nil,
-- chat_id (integer or string) for forwarded messages
-- set to nil to disable message forwarding
forward_chat_id = nil,
},

nginx_conf = {
Expand Down

0 comments on commit 48034c2

Please sign in to comment.