Skip to content
This repository has been archived by the owner on Oct 14, 2018. It is now read-only.

Lua binding improvements #358

Open
wants to merge 2 commits into
base: supergroups
Choose a base branch
from

Conversation

rizaumami
Copy link

Add new bindings for lua scripting:

  • block_user
  • unblock_user
  • channel_join
  • channel_leave
  • channel_list
  • channel_set_about
  • channel_set_admin
  • channel_set_photo
  • channel_set_username
  • export_channel_link
  • get_message
  • import_channel_link
  • media_caption
  • rename_channel
  • resolve_username

Code examples of:

  • block_user and unblock_user to block and unbloking a user.
do

  local function run(msg, matches)
    if matches[2]:match('^%d.+$') then
      if matches[1] == 'block' then
        block_user('user#id'..matches[2], ok_cb, true)
        return 'User '..matches[2]..' blocked'
      elseif matches[1] == 'unblock' then
        unblock_user('user#id'..matches[2], ok_cb, true)
        return 'User '..matches[2]..' unblocked'
      end
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Block a user.',
    usage = {
      ' !block [user_id] : Block user by their ID.'
    },
    patterns = {
      '^!(block) (%d.+)$',
      '^!(unblock) (%d.+)$'
    },
    run = run
  }

end
  • rename_channel to rename channels/supergroups title.
do

  local function run(msg, matches)
    if matches[1] == 'rename' then
      rename_channel(get_receiver(msg), matches[2], ok_cb, true)
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Set channel/supergroup title.',
    usage = {
      ' !channel rename [title] : Set channel/supergroup title to [title].'
    },
    patterns = {
      '^!channel (rename) (.*)$'
    },
    run = run
  }

end
  • channel_set_username to set channels/supergroups username.
    This username will also become channels/supergroups URL.
do

  local function run(msg, matches)
    if matches[1] == 'setname' then
      channel_set_username(get_receiver(msg), matches[2], ok_cb, true)
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Set channel/supergroup username.',
    usage = {
      ' !channel setname [username] : Set channel/supergroup username to [username].'
    },
    patterns = {
      '^!channel (setname) (%w.+)$'
    },
    run = run
  }

end
  • export_channel_link to generate channels/supergroups invite link.
do

  local function exp ort_channel_link_cb(extra, success, result)
    send_msg(get_receiver(extra), result, ok_cb, true) 
  end

--------------------------------------------------------------------------------

  local function run(msg, matches)
    if matches[1] == 'set' then
      export_channel_link(get_receiver(msg), export_channel_link_cb, msg)
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Generate channels/supergroups invite link.',
    usage = {
      '!link set : Generate channels/supergroups invite link.'
    },
    patterns = {
      '^!link (set)$'
    },
    run = run
  }

end
  • get_message to get user id by reply:
do

  local function action_by_reply(extra, success, result)
    if result.from.username then
      user_name = '@'..result.from.username
    else
      user_name = ''
    end
    local text = 'Name: '..(result.from.first_name or '')..' '..(result.from.last_name or '')..'\n'
               ..'First name: '..(result.from.first_name or '')..'\n'
               ..'Last name: '..(result.from.last_name or '')..'\n'
               ..'User name: '..user_name..'\n'
               ..'ID: '..result.from.peer_id
    send_large_msg(extra, text)
  end

--------------------------------------------------------------------------------

  local function run(msg)
    if msg.text == '!id' and msg.reply_id then
      get_message(msg.reply_id, action_by_reply, get_receiver(msg))
    end
  end

--------------------------------------------------------------------------------

  return {
    decription = 'Print user_id by_reply',
    usage = 'Reply to a message then type: !id',
    patterns = {
      '^!id$'
    },
    run = run
  }

end
  • import_channel_link to Join a channel/supergoup via its invite link.
do

  local function export_channel_link_cb(extra, success, result)
    send_msg(get_receiver(extra), result, ok_cb, true) 
  end

--------------------------------------------------------------------------------

  local function run(msg, matches)
    if matches[1] == 'join' then
      import_chat_link(matches[2], ok_cb, false)
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Join a channel/supergroup via its invite link.',
    usage = {
      '!join [invite_link] : Join a channel/supergroup via its invite link.'
    },
    patterns = {
      '^!(join) (.*)$'
    },
    run = run
  }

end
  • media_caption to warn sticker sender:
do

  local function pre_process(msg) 
    if msg.media and msg.media.caption == 'sticker.webp' then
      send_msg(get_receiver(msg), 'WARNING!\nDO NOT send sticker into this group!', ok_cb, true) 
    end
  end  

--------------------------------------------------------------------------------

  return {
    description = 'Warn sticker sender.',
    patterns = {
      '^!!tgservice (.+)$'
    },
    pre_process = pre_process
  }

end
  • resolve_username to invite user by its username:
do
  local function resolve_username_cb(extra, success, result)    
    if success == 1 then
      if extra.to.type == 'channel' then
        channel_invite_user(get_receiver(extra), result.id, ok_cb, false)
      else
        chat_add_user(get_receiver(extra), result.id, ok_cb, false)
      end
    end
  end

--------------------------------------------------------------------------------

  local function run(msg, matches)
    if matches[1]:match('^@.+$') then
      resolve_username(matches[1]:gsub('@', ''), resolve_username_cb, msg)
    end
  end

--------------------------------------------------------------------------------

  return {
    description = 'Invite other user to the chat group.',
    usage = {
      ' !invite @[user_name] : Invite by their @[user_name].'
    },
    patterns = {
      '^!invite (@.*)$'
    },
    run = run
  }

end

	modified:   launch.sh
	new file:   patches/lua-tg.get_message.resolve_username.media_caption.patch
@zhityer
Copy link

zhityer commented Feb 8, 2016

resolve_username don't work for me
i use patch and exampple but fail

@rizaumami
Copy link
Author

@zhityer
The patch is working.
Are you sure the error is in the patch?

@zhityer
Copy link

zhityer commented Feb 12, 2016

not sure
but example resolve_username don't work for me with your github ( supergroups branch )
and
Example of get_message to get user id by reply, don't work very good
some time not answer,

I exactly examples put into plugins of supergroups branch but resolve_username not working and get_message some time work

@rizaumami
Copy link
Author

Cannot help you.
I've just tested this by re-cloned the repo. It's working.
Better you debug your script ...

@richpeterson
Copy link

Set_message definitely does not work well, I tried it on two supergroups, one returned the "id" corresponding, while another group returned the id of any user under which corresponded.

@rizaumami
Copy link
Author

@richpeterson
Because you didn't disable the original id.lua.
My example script use the same command; !id which trigger response from both script.

@richpeterson
Copy link

No, I modified the function to work with " !idtest " and continues with the same problem , it does not work as it should.

@richpeterson
Copy link

https://telegram.me/joinchat/BFjvHDyG6Ltdf9OLh5fK3g in this group you can try

…me, rename_channel, resolve_username, get_message, export_channel_link, channel_set_admin, channel_set_about, channel_set_photo, channel_leave, channel_join, get_channel_list, media-document_caption and add_inviter_to_members_table to lua binding
@rizaumami rizaumami changed the title get_message, resolve_username and media_caption patch. Lua binding improvements Feb 28, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants