2016-06-19 20:52:59 +02:00
-- INFO: Stats must be activated, so that it can collect all members of a group and save his/her id to redis.
-- You can deactivate it afterwards.
local notify = { }
function notify : init ( config )
notify.triggers = {
" ^/notify (del)$ " ,
" ^/notify$ "
}
notify.doc = [ [ *
] ] .. config.cmd_pat .. [[notify* (del): Benachrichtigt dich privat, wenn du erwähnt wirst (bzw. schaltet das Feature wieder aus)]]
end
notify.command = ' notify [del] '
-- See https://stackoverflow.com/a/32854917
function isWordFoundInString ( word , input )
return select ( 2 , input : gsub ( ' ^ ' .. word .. ' %W+ ' , ' ' ) ) +
select ( 2 , input : gsub ( ' %W+ ' .. word .. ' $ ' , ' ' ) ) +
select ( 2 , input : gsub ( ' ^ ' .. word .. ' $ ' , ' ' ) ) +
select ( 2 , input : gsub ( ' %W+ ' .. word .. ' %W+ ' , ' ' ) ) > 0
end
2016-08-24 15:38:29 +02:00
function notify : pre_process ( msg )
2016-06-19 20:52:59 +02:00
local notify_users = redis : smembers ( ' notify:ls ' )
2016-08-16 20:52:56 +02:00
2016-06-19 20:52:59 +02:00
-- I call this beautiful lady the "if soup"
2016-08-16 20:58:28 +02:00
if msg.chat . type == ' group ' or msg.chat . type == ' supergroup ' then
2016-06-19 20:52:59 +02:00
if msg.text then
for _ , user in pairs ( notify_users ) do
if isWordFoundInString ( ' @ ' .. user , string.lower ( msg.text ) ) then
local chat_id = msg.chat . id
local id = redis : hget ( ' notify: ' .. user , ' id ' )
-- check, if user has sent at least one message to the group,
-- so that we don't send the user some private text, when he/she is not
-- in the group.
if redis : sismember ( ' chat: ' .. chat_id .. ' :users ' , id ) then
-- ignore message, if user is mentioning him/herself
2016-08-16 20:52:56 +02:00
if id ~= tostring ( msg.from . id ) then
local send_date = run_command ( ' date -d @ ' .. msg.date .. ' +"%d.%m.%Y um %H:%M:%S Uhr" ' )
local send_date = string.gsub ( send_date , " \n " , " " )
local from = string.gsub ( msg.from . name , " %_ " , " " )
local chat_name = string.gsub ( msg.chat . title , " %_ " , " " )
local text = from .. ' am ' .. send_date .. ' in " ' .. chat_name .. ' ": \n \n ' .. msg.text
2016-08-24 15:38:29 +02:00
utilities.send_message ( id , text , true )
2016-08-16 20:52:56 +02:00
end
2016-06-19 20:52:59 +02:00
end
end
end
end
end
return msg
end
function notify : action ( msg , config , matches )
if not msg.from . username then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Du hast keinen Usernamen und kannst daher dieses Feature nicht nutzen. Tut mir leid! ' )
2016-07-08 20:00:11 +02:00
return
2016-06-19 20:52:59 +02:00
end
local username = string.lower ( msg.from . username )
local hash = ' notify: ' .. username
if matches [ 1 ] == " del " then
if not redis : sismember ( ' notify:ls ' , username ) then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Du wirst noch gar nicht benachrichtigt! ' )
2016-06-19 20:52:59 +02:00
return
end
print ( ' Setting notify in redis hash ' .. hash .. ' to false ' )
redis : hset ( hash , ' notify ' , false )
print ( ' Removing ' .. username .. ' from redis set notify:ls ' )
redis : srem ( ' notify:ls ' , username )
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Du erhälst jetzt keine Benachrichtigungen mehr, wenn du angesprochen wirst. ' )
2016-06-19 20:52:59 +02:00
return
else
if redis : sismember ( ' notify:ls ' , username ) then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Du wirst schon benachrichtigt! ' )
2016-06-19 20:52:59 +02:00
return
end
print ( ' Setting notify in redis hash ' .. hash .. ' to true ' )
redis : hset ( hash , ' notify ' , true )
print ( ' Setting id in redis hash ' .. hash .. ' to ' .. msg.from . id )
redis : hset ( hash , ' id ' , msg.from . id )
print ( ' Adding ' .. username .. ' to redis set notify:ls ' )
redis : sadd ( ' notify:ls ' , username )
2016-08-24 15:38:29 +02:00
local res = utilities.send_message ( msg.from . id , ' Du erhälst jetzt Benachrichtigungen, wenn du angesprochen wirst, nutze `/notify del` zum Deaktivieren. ' , true , nil , true )
2016-06-19 20:52:59 +02:00
if not res then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Bitte schreibe mir [privat](http://telegram.me/ ' .. self.info . username .. ' ?start=notify), um den Vorgang abzuschließen. ' , true )
2016-06-19 20:52:59 +02:00
elseif msg.chat . type ~= ' private ' then
2016-08-24 15:38:29 +02:00
utilities.send_reply ( msg , ' Du erhälst jetzt Benachrichtigungen, wenn du angesprochen wirst, nutze `/notify del` zum Deaktivieren. ' , true )
2016-06-19 20:52:59 +02:00
end
end
end
return notify