2016-06-15 18:00:59 +02:00
local bitly_create = { }
local http = require ( ' socket.http ' )
local https = require ( ' ssl.https ' )
local URL = require ( ' socket.url ' )
local json = require ( ' dkjson ' )
2016-07-17 13:22:27 +02:00
local utilities = require ( ' miku.utilities ' )
local bindings = require ( ' miku.bindings ' )
2016-06-15 18:00:59 +02:00
local OAuth = require " OAuth "
2016-07-17 13:22:27 +02:00
local redis = ( loadfile " ./miku/redis.lua " ) ( )
2016-06-15 18:00:59 +02:00
function bitly_create : init ( config )
if not cred_data.bitly_client_id then
print ( ' Missing config value: bitly_client_id. ' )
print ( ' bitly_create.lua will not be enabled. ' )
return
elseif not cred_data.bitly_client_secret then
print ( ' Missing config value: bitly_client_secret. ' )
print ( ' bitly_create.lua will not be enabled. ' )
return
elseif not cred_data.bitly_redirect_uri then
print ( ' Missing config value: bitly_redirect_uri. ' )
print ( ' bitly_create.lua will not be enabled. ' )
return
end
bitly_create.triggers = {
2016-07-18 18:37:29 +02:00
" ^/[Ss][Hh][Oo][Rr][Tt](auth)(.+)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (auth)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (unauth)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (me)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (j.mp) (https?://[%w-_%.%?%.:/%+=&]+)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (bit.ly) (https?://[%w-_%.%?%.:/%+=&]+)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (bitly.com) (https?://[%w-_%.%?%.:/%+=&]+)$ " ,
" ^/[Ss][Hh][Oo][Rr][Tt] (https?://[%w-_%.%?%.:/%+=&]+)$ "
2016-06-15 18:00:59 +02:00
}
bitly_create.doc = [ [ *
] ] .. config.cmd_pat .. [ [ short * _ < Link > _ : Kürzt einen Link mit der Standard Bitly - Adresse
* ] ] .. config.cmd_pat .. [ [ short * _ < j.mp | bit.ly | bitly.com > _ _ [ Link ] _ : Kürzt einen Link mit der ausgewählten Kurz - URL
* ] ] .. config.cmd_pat .. [ [ short * _auth_ : Loggt deinen Account ein und nutzt ihn für deine Links ( empfohlen ! )
* ] ] .. config.cmd_pat .. [ [ short * _me_ : Gibt den eingeloggten Account aus
* ] ] .. config.cmd_pat .. [ [ short * _unauth_ : Loggt deinen Account aus
] ]
end
bitly_create.command = ' short <URL> '
local BASE_URL = ' https://api-ssl.bitly.com '
local client_id = cred_data.bitly_client_id
local client_secret = cred_data.bitly_client_secret
local redirect_uri = cred_data.bitly_redirect_uri
function bitly_create : get_bitly_access_token ( hash , code )
local req = post_petition ( BASE_URL .. ' /oauth/access_token ' , ' client_id= ' .. client_id .. ' &client_secret= ' .. client_secret .. ' &code= ' .. code .. ' &redirect_uri= ' .. redirect_uri )
if not req.access_token then return ' *Fehler beim Einloggen!* ' end
local access_token = req.access_token
local login_name = req.login
redis : hset ( hash , ' bitly ' , access_token )
return ' Erfolgreich als ` ' .. login_name .. ' ` eingeloggt! '
end
function bitly_create : get_bitly_user_info ( bitly_access_token )
local url = BASE_URL .. ' /v3/user/info?access_token= ' .. bitly_access_token .. ' &format=json '
local res , code = https.request ( url )
if code == 401 then return ' Login fehlgeschlagen! ' end
if code ~= 200 then return ' HTTP-Fehler! ' end
local data = json.decode ( res ) . data
if data.full_name then
name = ' * ' .. data.full_name .. ' * (` ' .. data.login .. ' `) '
else
name = ' ` ' .. data.login .. ' ` '
end
local text = ' Eingeloggt als ' .. name
return text
end
function bitly_create : create_bitlink ( long_url , domain , bitly_access_atoken )
local url = BASE_URL .. ' /v3/shorten?access_token= ' .. bitly_access_token .. ' &domain= ' .. domain .. ' &longUrl= ' .. long_url .. ' &format=txt '
local text , code = https.request ( url )
if code ~= 200 then return ' FEHLER: ' .. text end
return text
end
function bitly_create : action ( msg , config , matches )
local hash = ' user: ' .. msg.from . id
bitly_access_token = redis : hget ( hash , ' bitly ' )
if matches [ 1 ] == ' auth ' and matches [ 2 ] then
utilities.send_reply ( self , msg , bitly_create : get_bitly_access_token ( hash , matches [ 2 ] ) , true )
2016-07-17 13:22:27 +02:00
local message_id = redis : hget ( hash , ' bitly_login_msg ' )
utilities.edit_message ( self , msg.chat . id , message_id , ' *Anmeldung abgeschlossen!* ' , true , true )
redis : hdel ( hash , ' bitly_login_msg ' )
2016-06-15 18:00:59 +02:00
return
end
if matches [ 1 ] == ' auth ' then
2016-07-17 13:22:27 +02:00
local result = utilities.send_reply ( self , msg , ' *Bitte logge dich ein und folge den Anweisungen.* ' , true , ' {"inline_keyboard":[[{"text":"Bei Bitly anmelden","url":"https://bitly.com/oauth/authorize?client_id= ' .. client_id .. ' &redirect_uri= ' .. redirect_uri .. ' &state= ' .. self.info . username .. ' "}]]} ' )
redis : hset ( hash , ' bitly_login_msg ' , result.result . message_id )
return
2016-06-15 18:00:59 +02:00
end
if matches [ 1 ] == ' unauth ' and bitly_access_token then
redis : hdel ( hash , ' bitly ' )
utilities.send_reply ( self , msg , ' *Erfolgreich ausgeloggt!* Du kannst den Zugriff [in deinen Kontoeinstellungen](https://bitly.com/a/settings/connected) endgültig entziehen. ' , true )
return
elseif matches [ 1 ] == ' unauth ' and not bitly_access_token then
utilities.send_reply ( self , msg , ' Wie willst du dich ausloggen, wenn du gar nicht eingeloggt bist? ' , true )
return
end
if matches [ 1 ] == ' me ' and bitly_access_token then
local text = bitly_create : get_bitly_user_info ( bitly_access_token )
if text then
utilities.send_reply ( self , msg , text , true )
return
else
return
end
elseif matches [ 1 ] == ' me ' and not bitly_access_token then
utilities.send_reply ( self , msg , ' Du bist nicht eingeloggt! Logge dich ein mit \n /short auth ' , true )
return
end
if not bitly_access_token then
print ( ' Not signed in, will use global bitly access_token ' )
bitly_access_token = cred_data.bitly_access_token
end
2016-07-17 13:22:27 +02:00
2016-06-15 18:00:59 +02:00
if matches [ 2 ] == nil then
long_url = url_encode ( matches [ 1 ] )
domain = ' bit.ly '
else
long_url = url_encode ( matches [ 2 ] )
domain = matches [ 1 ]
end
utilities.send_reply ( self , msg , bitly_create : create_bitlink ( long_url , domain , bitly_access_token ) )
return
end
return bitly_create