This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/bindings.lua

334 lines
8.2 KiB
Lua
Raw Normal View History

2016-01-15 04:39:24 +01:00
-- bindings.lua
-- Bindings for the Telegram bot API.
-- https://core.telegram.org/bots/api
assert(HTTPS)
assert(JSON)
assert(URL)
2016-01-15 04:39:24 +01:00
local BASE_URL = 'https://api.telegram.org/bot' .. config.bot_api_key
2015-07-03 00:15:52 +02:00
if 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
sendRequest = function(url)
2015-07-03 00:15:52 +02:00
local dat, res = HTTPS.request(url)
local tab = JSON.decode(dat)
2015-07-03 00:15:52 +02:00
if not tab.ok then
return false, tab.description
2015-07-03 00:15:52 +02:00
end
return tab
end
getMe = function()
2015-07-03 00:15:52 +02:00
local url = BASE_URL .. '/getMe'
return sendRequest(url)
2015-07-03 00:15:52 +02:00
end
getUpdates = function(offset)
2015-07-03 00:15:52 +02:00
local url = BASE_URL .. '/getUpdates?timeout=20'
2015-07-03 00:15:52 +02:00
if offset then
url = url .. '&offset=' .. offset
2015-07-03 00:15:52 +02:00
end
return sendRequest(url)
2015-07-03 00:15:52 +02:00
end
2016-02-26 10:40:43 +01:00
sendMessage = function(chat_id, text, disable_web_page_preview, reply_to_message_id, use_markdown, disable_notification)
2015-07-03 00:15:52 +02: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
if use_markdown then
url = url .. '&parse_mode=Markdown'
end
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
url = url .. '&disable_notification=true'
2016-02-26 10:40:43 +01:00
end
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
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
local url = BASE_URL .. '/sendChatAction?chat_id=' .. chat_id .. '&action=' .. action
return sendRequest(url)
2015-07-03 00:15:52 +02:00
end
2016-02-26 10:40:43 +01:00
sendLocation = function(chat_id, latitude, longitude, reply_to_message_id, disable_notification)
2015-07-03 00:15:52 +02:00
if latitude == 0 then latitude = 0.001 end
if longitude == 0 then longitude = 0.001 end
local url = BASE_URL .. '/sendLocation?chat_id=' .. chat_id .. '&latitude=' .. latitude .. '&longitude=' .. longitude
2015-07-03 00:15:52 +02:00
2016-04-15 05:56:32 +02:00
if reply_to_message_id then
url = url .. '&reply_to_message_id=' .. reply_to_message_id
end
if disable_notification then
url = url .. '&disable_notification=true'
end
return sendRequest(url)
end
2016-04-15 14:16:04 +02:00
sendVenue = function(chat_id, latitude, longitude, title, address, foursquare_id, reply_to_message_id, disable_notification)
if latitude == 0 then latitude = 0.001 end
if longitude == 0 then longitude = 0.001 end
local url = BASE_URL .. '/sendVenue?chat_id=' .. chat_id .. '&latitude=' .. latitude .. '&longitude=' .. longitude .. '&title=' .. title .. '&address=' .. address
if foursquare_id then
url = url .. '&foursquare_id=' .. foursquare_id
end
if reply_to_message_id then
url = url .. '&reply_to_message_id=' .. reply_to_message_id
end
if disable_notification then
url = url .. '&disable_notification=true'
end
return sendRequest(url)
end
2016-04-15 05:56:32 +02:00
sendContact = function(chat_id, phone_number, first_name, last_name, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendContact?chat_id=' .. chat_id .. '&phone_number=' .. phone_number .. '&first_name=' .. first_name
if last_name then
url = url .. '&last_name=' .. last_name
end
2016-04-15 14:16:04 +02:00
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
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
url = url .. '&disable_notification=true'
2016-02-26 10:40:43 +01:00
end
return sendRequest(url)
end
2016-02-26 10:40:43 +01:00
forwardMessage = function(chat_id, from_chat_id, message_id, disable_notification)
local url = BASE_URL .. '/forwardMessage?chat_id=' .. chat_id .. '&from_chat_id=' .. from_chat_id .. '&message_id=' .. message_id
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
url = url .. '&disable_notification=true'
2016-02-26 10:40:43 +01:00
end
return sendRequest(url)
end
kickChatMember = function(chat_id, user_id)
local url = BASE_URL .. '/kickChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
return sendRequest(url)
end
unbanChatMember = function(chat_id, user_id)
local url = BASE_URL .. '/unbanChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
return sendRequest(url)
end
-- TODO: More of this.
sendPhotoID = function(chat_id, file_id, caption, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendPhoto?chat_id=' .. chat_id .. '&photo=' .. file_id
if caption then
url = url .. '&caption=' .. URL.escape(caption)
end
if reply_to_message_id then
url = url .. '&reply_to_message_id=' .. reply_to_message_id
end
if disable_notification then
url = url .. '&disable_notification=true'
end
return sendRequest(url)
end
curlRequest = function(curl_command)
-- Use at your own risk. Will not check for success.
io.popen(curl_command)
end
2016-02-26 10:40:43 +01:00
sendPhoto = function(chat_id, photo, caption, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendPhoto'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "photo=@' .. photo .. '"'
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
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
2015-07-03 00:15:52 +02:00
end
2015-07-19 22:46:06 +02:00
2016-02-26 10:40:43 +01:00
sendDocument = function(chat_id, document, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendDocument'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "document=@' .. document .. '"'
if reply_to_message_id then
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
end
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
end
2016-02-26 10:40:43 +01:00
sendSticker = function(chat_id, sticker, reply_to_message_id, disable_notification)
local url = BASE_URL .. '/sendSticker'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "sticker=@' .. sticker .. '"'
2015-07-19 22:46:06 +02:00
if reply_to_message_id then
curl_command = curl_command .. ' -F "reply_to_message_id=' .. reply_to_message_id .. '"'
end
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
end
2016-02-26 10:40:43 +01:00
sendAudio = function(chat_id, audio, reply_to_message_id, duration, performer, title, disable_notification)
local url = BASE_URL .. '/sendAudio'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "audio=@' .. audio .. '"'
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
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
end
2016-02-26 10:40:43 +01:00
sendVideo = function(chat_id, video, reply_to_message_id, duration, caption, disable_notification)
local url = BASE_URL .. '/sendVideo'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "video=@' .. video .. '"'
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
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
end
2016-02-26 10:40:43 +01:00
sendVoice = function(chat_id, voice, reply_to_message_id, duration, disable_notification)
local url = BASE_URL .. '/sendVoice'
local curl_command = 'curl -s "' .. url .. '" -F "chat_id=' .. chat_id .. '" -F "voice=@' .. voice .. '"'
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
2016-02-26 10:40:43 +01:00
if disable_notification then
2016-02-26 17:48:57 +01:00
curl_command = curl_command .. ' -F "disable_notification=true"'
2016-02-26 10:40:43 +01:00
end
return curlRequest(curl_command)
2015-07-19 22:46:06 +02:00
end