2016-07-17 13:22:27 +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 = {
2016-08-01 21:07:27 +02:00
" ^/[Nn][Oo][Tt][Ii][Ff][Yy] (del)$ " ,
" ^/[Nn][Oo][Tt][Ii][Ff][Yy]$ "
2016-07-17 13:22:27 +02:00
}
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-07-17 13:22:27 +02:00
local notify_users = redis : smembers ( ' notify:ls ' )
2016-08-16 20:52:56 +02:00
2016-07-17 13:22:27 +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-07-17 13:22:27 +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-07-17 13:22:27 +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-17 13:22:27 +02:00
return
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-07-17 13:22:27 +02:00
return
end
2016-08-01 21:07:27 +02:00
print ( ' Setze notify redis hash ' .. hash .. ' auf false ' )
2016-07-17 13:22:27 +02:00
redis : hset ( hash , ' notify ' , false )
2016-08-01 21:07:27 +02:00
print ( ' Lösche ' .. username .. ' von redis set notify:ls ' )
2016-07-17 13:22:27 +02:00
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-07-17 13:22:27 +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-07-17 13:22:27 +02:00
return
end
2016-08-01 21:07:27 +02:00
print ( ' Setze notify in redis hash ' .. hash .. ' auf true ' )
2016-07-17 13:22:27 +02:00
redis : hset ( hash , ' notify ' , true )
2016-08-01 21:07:27 +02:00
print ( ' Setze id in redis hash ' .. hash .. ' auf ' .. msg.from . id )
2016-07-17 13:22:27 +02:00
redis : hset ( hash , ' id ' , msg.from . id )
2016-08-01 21:07:27 +02:00
print ( ' Adde ' .. username .. ' zu redis set notify:ls ' )
2016-07-17 13:22:27 +02:00
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-07-17 13:22:27 +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-07-17 13:22:27 +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-07-17 13:22:27 +02:00
end
end
end
return notify