do

local function save_text(msg)
  if msg.text:sub(10):isempty() then
    return "Benutzung: #addtext [Text]"
  end
  
  local savedtext = msg.text:sub(10)
  local hash = 'telegram:savedtext'
  print('Saving text to redis set '..hash)
  redis:sadd(hash, savedtext)
  return 'Gespeichert: "'..savedtext..'"'
end

local function delete_text(msg)
  if msg.text:sub(10):isempty() then
    return "Benutzung: #delquote [Text]"
  end
  
  local savedtext = msg.text:sub(10)
  local hash = 'telegram:savedtext'
  print('Deleting text from redis set '..hash)
  if redis:sismember(hash, savedtext) == true then
    redis:srem(hash, savedtext)
    return 'Text erfolgreich gelöscht!'
  else
    return 'Dieser Text existiert nicht.'
  end
end

local function get_text(msg)
  local to_id = tostring(msg.to.id)
  local hash = 'telegram:savedtext'
  
  if hash then
    print('Getting text from redis set '..hash)
  	local text_table = redis:smembers(hash)
	if not text_table[1] then
	  return 'Es wurden noch kein Text gespeichert.\nSpeichere doch einen mit /addtext [Text]'
	else
	  return text_table[math.random(1,#text_table)]
	end
  end
end

local function list_text(msg)
  local hash = 'telegram:savedtext'
  
  if hash then
    print('Getting text from redis set '..hash)
    local text_table = redis:smembers(hash)
	local text = ""
    for num,savedtext in pairs(text_table) do
      text = text..num..") "..savedtext..'\n'
    end
	if not text or text == "" then
	  return 'Es wurden noch kein Text gespeichert.\nSpeichere doch einen mit /addtext [Text]'
	else
	  return text
	end
  end
end

local function run(msg, matches)
  if matches[1] == "addtext" then
    return save_text(msg)
  elseif matches[1] == "deltext" then
      return delete_text(msg)
  elseif matches[1] == "listtext" then
    return list_text(msg)
  end
end

return {
  description = "",
  usage = {},
  patterns = {
    "^#(deltext) (.+)$",
    "^#(addtext) (.+)$",
	"^#(listtext)$"
  },
  run = run,
  privileged = true
}

end