Skip to content

Commit

Permalink
Fix #379 and fix missing placeholder substitution for rules
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Minari <yangm97@gmail.com>
  • Loading branch information
yangm97 committed Sep 28, 2018
1 parent 95d1079 commit 7d85070
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
13 changes: 6 additions & 7 deletions lua/groupbutler/plugins/extra.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ function _M:onTextMessage(blocks)
else
local hash = 'chat:'..msg.chat.id..':extra'
local new_extra = blocks[3]
local reply_markup, test_text = u:reply_markup_from_text(new_extra)
local reply_markup, test_text = u:reply_markup_from_text(u:replaceholders(new_extra, msg))
test_text = test_text:gsub('\n', '')

local ok, err = msg:send_reply(test_text:replaceholders(msg), "Markdown", reply_markup)
local ok, err = msg:send_reply(test_text, "Markdown", reply_markup)
if not ok then
api:sendMessage(msg.chat.id, api_err.trans(err), "Markdown")
else
Expand Down Expand Up @@ -138,9 +138,8 @@ function _M:onTextMessage(blocks)
if msg.chat.id > 0
or(is_locked(self, msg.chat.id) and not msg:is_from_admin()) then -- send it in private
if not file_id then
local reply_markup, clean_text = u:reply_markup_from_text(text)
_, err = api:sendMessage(msg.from.id, clean_text:replaceholders(msg.reply or msg), "Markdown",
link_preview, nil, nil, reply_markup)
local reply_markup, clean_text = u:reply_markup_from_text(u:replaceholders(text, msg.reply or msg))
_, err = api:sendMessage(msg.from.id, clean_text, "Markdown", link_preview, nil, nil, reply_markup)
elseif special_method then
_, err = sendMedia(self, msg.from.id, file_id, special_method) -- photo, voices, video need their method to be sent by file_id
else
Expand All @@ -160,8 +159,8 @@ function _M:onTextMessage(blocks)
api:sendDocument(msg.chat.id, file_id, nil, nil, msg_to_reply)
end
else
local reply_markup, clean_text = u:reply_markup_from_text(text)
api:sendMessage(msg.chat.id, clean_text:replaceholders(msg.reply or msg), "Markdown", link_preview, nil, msg_to_reply, reply_markup) -- if the admin replies to a user, the bot will reply to the user too
local reply_markup, clean_text = u:reply_markup_from_text(u:replaceholders(text, msg.reply or msg))
api:sendMessage(msg.chat.id, clean_text, "Markdown", link_preview, nil, msg_to_reply, reply_markup) -- if the admin replies to a user, the bot will reply to the user too
end
end

Expand Down
3 changes: 1 addition & 2 deletions lua/groupbutler/plugins/pin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ function _M:new(update_obj)
end

local function get_reply_markup(self, msg, text)
local reply_markup, new_text = self.u:reply_markup_from_text(text)
return reply_markup, new_text:replaceholders(msg, "rules", "title")
return self.u:reply_markup_from_text(self.u:replaceholders(text, msg, "rules", "title"))
end

local function pin_message(self, chat_id, message_id)
Expand Down
7 changes: 3 additions & 4 deletions lua/groupbutler/plugins/welcome.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,18 @@ local function get_reply_markup(self, msg, text)

local new_text, reply_markup
if text then
reply_markup, new_text = u:reply_markup_from_text(text)
text = new_text:replaceholders(msg)
reply_markup, new_text = u:reply_markup_from_text(u:replaceholders(text, msg))
end

if is_on(self, msg.chat.id, "Welbut") then
if not reply_markup then
reply_markup = {inline_keyboard={}}
end
local line = {{text = i18n("Read the rules"), url = u:deeplink_constructor(msg.chat.id, "rules")}}
local line = {{new_text = i18n("Read the rules"), url = u:deeplink_constructor(msg.chat.id, "rules")}}
table.insert(reply_markup.inline_keyboard, line)
end

return reply_markup, text
return reply_markup, new_text
end

local function send_welcome(self, msg)
Expand Down
8 changes: 4 additions & 4 deletions lua/groupbutler/utilities.lua
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ end
-- considered as the sequence of strings - names of placeholders. If
-- placeholders to replacing are specified, this function processes only them,
-- otherwise it processes all available placeholders.
function string:replaceholders(msg, ...)
function _M:replaceholders(str, msg, ...)
if msg.new_chat_member then
msg.from = msg.new_chat_member
elseif msg.left_chat_member then
Expand All @@ -136,7 +136,7 @@ function string:replaceholders(msg, ...)
username = msg.from.username and '@'..msg.from.username or '-',
id = msg.from.id,
title = msg.chat.title,
-- rules = self:deeplink_constructor(msg.chat.id, 'rules'),
rules = self:deeplink_constructor(msg.chat.id, "rules"),
}
-- remove flag about escaping
table.remove(tail_arguments, 1)
Expand All @@ -148,7 +148,7 @@ function string:replaceholders(msg, ...)
userorname = msg.from.username and '@'..msg.from.username:escape() or msg.from.first_name:escape(),
id = msg.from.id,
title = msg.chat.title:escape(),
-- rules = self:deeplink_constructor(msg.chat.id, 'rules'),
rules = self:deeplink_constructor(msg.chat.id, "rules"),
}
end

Expand All @@ -157,7 +157,7 @@ function string:replaceholders(msg, ...)
substitutions[placeholder] = replace_map[placeholder]
end

return self:gsub('$(%w+)', substitutions)
return str:gsub('$(%w+)', substitutions)
end

function _M:is_allowed(_, chat_id, user_obj) -- action is not used anymore
Expand Down

0 comments on commit 7d85070

Please sign in to comment.