Advertisement
psycholyzern

service_entergroup.lua

Jul 25th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.72 KB | None | 0 0
  1. local add_user_cfg = load_from_file('data/add_user_cfg.lua')
  2.  
  3. local function template_add_user(base, to_username, from_username, chat_name, chat_id)
  4.    base = base or ''
  5.    to_username = (to_username or '')
  6.    from_username = (from_username or '')
  7.    chat_name = chat_name or ''
  8.    chat_id = "chat#id" .. (chat_id or '')
  9.    base = string.gsub(base, "{to_username}", to_username)
  10.    base = string.gsub(base, "{from_username}", from_username)
  11.    base = string.gsub(base, "{chat_name}", chat_name)
  12.    base = string.gsub(base, "{chat_id}", chat_id)
  13.    return base
  14. end
  15.  
  16. function chat_new_user_link(msg)
  17.    -- if a user entered a group chat
  18.    local pattern = add_user_cfg.initial_chat_msg_link
  19.    -- change message if self invite via link
  20.    --if msg.from.id == our_id then
  21.    if msg.from.username == 'botix' then
  22.      pattern = add_user_cfg.invited_chat_msg_link
  23.    end
  24.    
  25.    local to_username = msg.from.print_name
  26.    -- change to username if exists
  27.    if msg.action.username then
  28.      to_username = '@'..msg.action.username
  29.    end
  30.  
  31.    local chat_name = msg.to.print_name
  32.    local chat_id = msg.to.id
  33.    pattern = template_add_user(pattern, to_username, from_username, chat_name, chat_id)
  34.    if pattern ~= '' then
  35.       local receiver = get_receiver(msg)
  36.       send_msg(receiver, pattern, ok_cb, false)
  37.    end
  38. end
  39.  
  40. function chat_new_user(msg)
  41.  
  42.    -- if a user were invited into a grouo
  43.    local pattern = add_user_cfg.initial_chat_msg
  44.    -- change message if bot's was invited
  45.    if msg.action.user.id == our_id then
  46.      pattern = add_user_cfg.invited_chat_msg
  47.    end
  48.  
  49.    local to_username = msg.action.user.print_name
  50.    -- change to username if exists
  51.    if msg.action.user.username then
  52.      to_username = '@'..msg.action.user.username
  53.    end
  54.  
  55.    local from_username = msg.from.print_name
  56.    -- change to username if exists
  57.    if msg.from.username then
  58.      from_username = '@'..msg.from.username
  59.    end
  60.  
  61.    local chat_name = msg.to.print_name
  62.    local chat_id = msg.to.id
  63.    pattern = template_add_user(pattern, to_username, from_username, chat_name, chat_id)
  64.    if pattern ~= '' then
  65.       local receiver = get_receiver(msg)
  66.       send_msg(receiver, pattern, ok_cb, false)
  67.    end
  68. end
  69.  
  70.  
  71. local function run(msg, matches)
  72.    if not msg.service then
  73.       return "Are you trying to troll me?"
  74.    end
  75.    if matches[1] == "chat_add_user" then
  76.       chat_new_user(msg)
  77.    elseif matches[1] == "chat_add_user_link" then
  78.       chat_new_user_link(msg)
  79.    end
  80. end
  81.  
  82. return {
  83.    description = "Service plugin that sends a custom message when an user enters a chat.",
  84.    usage = "",
  85.    patterns = {
  86.       "^!!tgservice (chat_add_user)$",
  87.       "^!!tgservice (chat_add_user_link)$"
  88.    },
  89.    run = run
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement