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.5 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
local bindings = {}
2016-01-15 04:39:24 +01:00
local HTTPS = require('ssl.https')
local JSON = require('dkjson')
local URL = require('socket.url')
2015-07-03 00:15:52 +02:00
function bindings.sendRequest(url)
2015-07-03 00:15:52 +02:00
local dat, res = HTTPS.request(url)
2016-04-15 07:44:24 +02:00
if not dat then return false, res end
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
function bindings:getMe()
2015-07-03 00:15:52 +02:00
local url = self.BASE_URL .. '/getMe'
return bindings.sendRequest(url)
2015-07-03 00:15:52 +02:00
end
function bindings:getUpdates(offset)
2015-07-03 00:15:52 +02:00
local url = self.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 bindings.sendRequest(url)
2015-07-03 00:15:52 +02:00
end
function bindings:sendMessage(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 = self.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 bindings.sendRequest(url)
end
function bindings:sendReply(msg, text)
return bindings.sendMessage(self, msg.chat.id, text, true, msg.message_id)
2015-07-03 00:15:52 +02:00
end
function bindings:sendChatAction(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 = self.BASE_URL .. '/sendChatAction?chat_id=' .. chat_id .. '&action=' .. action
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
2015-07-03 00:15:52 +02:00
end
function bindings:sendLocation(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 = self.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
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
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
end
function bindings:sendVenue(chat_id, latitude, longitude, title, address, foursquare_id, reply_to_message_id, disable_notification)
2016-04-15 14:16:04 +02:00
if latitude == 0 then latitude = 0.001 end
if longitude == 0 then longitude = 0.001 end
local url = self.BASE_URL .. '/sendVenue?chat_id=' .. chat_id .. '&latitude=' .. latitude .. '&longitude=' .. longitude .. '&title=' .. title .. '&address=' .. address
2016-04-15 14:16:04 +02:00
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 bindings.sendRequest(url)
2016-04-15 14:16:04 +02:00
end
function bindings.sendContact(chat_id, phone_number, first_name, last_name, reply_to_message_id, disable_notification)
2016-04-15 05:56:32 +02:00
local url = self.BASE_URL .. '/sendContact?chat_id=' .. chat_id .. '&phone_number=' .. phone_number .. '&first_name=' .. first_name
2016-04-15 05:56:32 +02:00
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 bindings.sendRequest(url)
end
function bindings:forwardMessage(chat_id, from_chat_id, message_id, disable_notification)
local url = self.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
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
end
2016-04-12 15:47:30 +02:00
function bindings:kickChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/kickChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
end
2016-04-12 15:47:30 +02:00
function bindings:unbanChatMember(chat_id, user_id)
local url = self.BASE_URL .. '/unbanChatMember?chat_id=' .. chat_id .. '&user_id=' .. user_id
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
end
-- TODO: More of this.
function bindings:sendPhotoID(chat_id, file_id, caption, reply_to_message_id, disable_notification)
local url = self.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
2016-04-14 05:48:20 +02:00
return bindings.sendRequest(url)
end
function bindings.curlRequest(curl_command)
-- Use at your own risk. Will not check for success.
io.popen(curl_command)
end
function bindings:sendPhoto(chat_id, photo, caption, reply_to_message_id, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
2015-07-03 00:15:52 +02:00
end
2015-07-19 22:46:06 +02:00
function bindings:sendDocument(chat_id, document, reply_to_message_id, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
end
function bindings:sendSticker(chat_id, sticker, reply_to_message_id, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
end
function bindings:sendAudio(chat_id, audio, reply_to_message_id, duration, performer, title, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
end
function bindings:sendVideo(chat_id, video, reply_to_message_id, duration, caption, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
end
function bindings:sendVoice(chat_id, voice, reply_to_message_id, duration, disable_notification)
local url = self.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 bindings.curlRequest(curl_command)
2015-07-19 22:46:06 +02:00
end
return bindings