2015-11-25 03:22:04 +01:00
|
|
|
local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if not config.bot_api_key then
|
|
|
|
error('You did not set your bot token in config.lua!')
|
|
|
|
end
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendRequest = function(url)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
local dat, res = HTTPS.request(url)
|
|
|
|
local tab = JSON.decode(dat)
|
|
|
|
|
|
|
|
if res ~= 200 then
|
2015-11-25 03:22:04 +01:00
|
|
|
return false, res
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
if not tab.ok then
|
2015-11-25 03:22:04 +01:00
|
|
|
return false, tab.description
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return tab
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
getMe = function()
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = BASE_URL .. '/getMe'
|
|
|
|
return sendRequest(url)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
getUpdates = function(offset)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = BASE_URL .. '/getUpdates?timeout=20'
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
if offset then
|
2015-08-04 22:11:55 +02:00
|
|
|
url = url .. '&offset=' .. offset
|
2015-07-03 00:15:52 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return sendRequest(url)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-12-05 15:30:52 +01:00
|
|
|
sendMessage = function(chat_id, text, disable_web_page_preview, reply_to_message_id, use_markdown)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = BASE_URL .. '/sendMessage?chat_id=' .. chat_id .. '&text=' .. URL.escape(text)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
if disable_web_page_preview == true then
|
|
|
|
url = url .. '&disable_web_page_preview=true'
|
|
|
|
end
|
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
url = url .. '&reply_to_message_id=' .. reply_to_message_id
|
|
|
|
end
|
|
|
|
|
2015-12-05 15:30:52 +01:00
|
|
|
if use_markdown then
|
|
|
|
url = url .. '&parse_mode=Markdown'
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return sendRequest(url)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendReply = function(msg, text)
|
|
|
|
|
|
|
|
return sendMessage(msg.chat.id, text, true, msg.message_id)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendChatAction = function(chat_id, action)
|
|
|
|
-- Support actions are typing, upload_photo, record_video, upload_video, record_audio, upload_audio, upload_document, find_location
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = BASE_URL .. '/sendChatAction?chat_id=' .. chat_id .. '&action=' .. action
|
|
|
|
return sendRequest(url)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendLocation = function(chat_id, latitude, longitude, reply_to_message_id)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local url = BASE_URL .. '/sendLocation?chat_id=' .. chat_id .. '&latitude=' .. latitude .. '&longitude=' .. longitude
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
url = url .. '&reply_to_message_id=' .. reply_to_message_id
|
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return sendRequest(url)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
forwardMessage = function(chat_id, from_chat_id, message_id)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/forwardMessage?chat_id=' .. chat_id .. '&from_chat_id=' .. from_chat_id .. '&message_id=' .. message_id
|
|
|
|
|
|
|
|
return sendRequest(url)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
curlRequest = function(curl_command)
|
2016-01-08 04:30:12 +01:00
|
|
|
-- Use at your own risk. Will not check for success.
|
2015-11-25 03:22:04 +01:00
|
|
|
|
2016-01-08 04:30:12 +01:00
|
|
|
io.popen(curl_command)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendPhoto = function(chat_id, photo, caption, reply_to_message_id)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendPhoto'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "photo=@' .. photo .. '"'
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if caption then
|
|
|
|
curl_command = curl_command .. ' -F "caption=' .. caption .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
return curlRequest(curl_command)
|
2015-07-03 00:15:52 +02:00
|
|
|
|
|
|
|
end
|
2015-07-19 22:46:06 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
sendDocument = function(chat_id, document, reply_to_message_id)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendDocument'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "document=@' .. document .. '"'
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
return curlRequest(curl_command)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendSticker = function(chat_id, sticker, reply_to_message_id)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendSticker'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "sticker=@' .. sticker .. '"'
|
2015-07-19 22:46:06 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
return curlRequest(curl_command)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendAudio = function(chat_id, audio, reply_to_message_id, duration, performer, title)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendAudio'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "audio=@' .. audio .. '"'
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if duration then
|
|
|
|
curl_command = curl_command .. ' -F "duration=' .. duration .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if performer then
|
|
|
|
curl_command = curl_command .. ' -F "performer=' .. performer .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if title then
|
|
|
|
curl_command = curl_command .. ' -F "title=' .. title .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
return curlRequest(curl_command)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendVideo = function(chat_id, video, reply_to_message_id, duration, performer, title)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendVideo'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "video=@' .. video .. '"'
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if caption then
|
|
|
|
curl_command = curl_command .. ' -F "caption=' .. caption .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if duration then
|
|
|
|
curl_command = curl_command .. ' -F "duration=' .. duration .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
return curlRequest(curl_command)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
sendVoice = function(chat_id, voice, reply_to_message_id)
|
|
|
|
|
|
|
|
local url = BASE_URL .. '/sendVoice'
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "voice=@' .. voice .. '"'
|
2015-11-25 03:22:04 +01:00
|
|
|
|
|
|
|
if reply_to_message_id then
|
|
|
|
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
|
|
|
|
end
|
|
|
|
|
|
|
|
if duration then
|
|
|
|
curl_command = curl_command .. ' -F "duration=' .. duration .. '"'
|
|
|
|
end
|
2015-07-19 22:46:06 +02:00
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
return curlRequest(curl_command)
|
2015-07-19 22:46:06 +02:00
|
|
|
|
|
|
|
end
|