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("luarocks.loader")
require("./bot/utils") require("./bot/utils")
VERSION = '20151112' VERSION = '20151205'
-- This function is called when tg receive a msg -- This function is called when tg receive a msg
function on_msg_receive (msg) function on_msg_receive (msg)

View File

@ -641,3 +641,16 @@ function get_location(user_id)
return set_location return set_location
end end
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 url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl
local res,code = https.request(url) local res,code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end if code ~= 200 then return "HTTP-FEHLER" end
local data = json:decode(res).data local data = json:decode(res).data.expand[1]
return data.expand[1].long_url cache_data('bitly', shorturl, data, 5184000)
return data.long_url
end end
local function run(msg, matches) local function run(msg, matches)
local shorturl = matches[1] local shorturl = matches[1]
local hash = 'telegram:cache:bitly:'..shorturl
if redis:exists(hash) == false then
return expand_bitly_link(shorturl) return expand_bitly_link(shorturl)
else
local data = redis:hgetall(hash)
return data.long_url
end
end end
return { return {

View File

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