Upstream # 97c3e6be von Brawlbot

This commit is contained in:
Akamaru 2015-12-05 21:36:56 +01:00
parent d243c5db9c
commit 60925fa6dd
4 changed files with 40 additions and 11 deletions

View File

@ -2,7 +2,7 @@ package.path = './.luarocks/share/lua/5.2/?.lua;./.luarocks/share/lua/5.2/?/init
require("luarocks.loader")
require("./bot/utils")
VERSION = '20151112'
VERSION = '20151205'
-- This function is called when tg receive a msg
function on_msg_receive (msg)

View File

@ -640,4 +640,17 @@ function get_location(user_id)
else
return set_location
end
end
function cache_data(plugin, query, data, timeout, typ)
-- How to: cache_data(pluginname, query_name, data_to_cache, expire_in_seconds)
if not timeout then timeout = 86400 end
local hash = 'telegram:cache:'..plugin..':'..query
print('Caching "'..query..'" from plugin '..plugin..' (expires in '..timeout..' seconds)')
if typ == 'key' then
redis:set(hash, data)
else
redis:hmset(hash, data)
end
redis:expire(hash, timeout)
end

View File

@ -10,13 +10,20 @@ local function expand_bitly_link (shorturl)
local url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl
local res,code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end
local data = json:decode(res).data
return data.expand[1].long_url
local data = json:decode(res).data.expand[1]
cache_data('bitly', shorturl, data, 5184000)
return data.long_url
end
local function run(msg, matches)
local shorturl = matches[1]
return expand_bitly_link(shorturl)
local hash = 'telegram:cache:bitly:'..shorturl
if redis:exists(hash) == false then
return expand_bitly_link(shorturl)
else
local data = redis:hgetall(hash)
return data.long_url
end
end
return {

View File

@ -5,16 +5,17 @@ do
local BASE_URL = 'https://gender-api.com/get'
function get_gender_data (name)
local function get_gender_data (name)
local apikey = cred_data.gender_apikey
local url = BASE_URL..'?name='..name..'&key='..apikey
local res,code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end
local data = json:decode(res)
cache_data('gender', string.lower(name), data, 345600)
return data
end
function send_gender_data(data, receiver)
local function send_gender_data(data, receiver)
if data.gender == "female" then
gender = 'weiblich'
end
@ -29,18 +30,26 @@ function send_gender_data(data, receiver)
send_msg(receiver, text, ok_cb, false)
end
function run(msg, matches)
local function run(msg, matches)
name = matches[1]
local data = get_gender_data(name)
local receiver = get_receiver(msg)
local hash = 'telegram:cache:gender:'..string.lower(name)
if redis:exists(hash) == false then
data = get_gender_data(name)
else
data = redis:hgetall(hash)
end
send_gender_data(data, receiver)
end
return {
description = "Sendet Geschlecht",
usage = {"/geschlecht [Name]","/gender [Name]"},
patterns = {"^/geschlecht (.*)$","^/gender (.*)$"},
usage = "/geschlecht [Name]: Sendet Geschlecht",
patterns = {
"^/geschlecht (.*)$",
"^/gender (.*)$"
},
run = run
}
end
end