├── .gitmodules ├── .gitignore ├── bot-example.lua ├── lua-bot-api-test.lua ├── README.md ├── LICENSE └── lua-bot-api.lua /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "multipart"] 2 | path = multipart 3 | url = https://github.com/cosmonawt/lua-multipart-post.git 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Lua sources 2 | luac.out 3 | dump.lua 4 | JSON.lua 5 | 6 | # luarocks build files 7 | *.src.rock 8 | *.zip 9 | *.tar.gz 10 | 11 | # Object files 12 | *.o 13 | *.os 14 | *.ko 15 | *.obj 16 | *.elf 17 | 18 | # Precompiled Headers 19 | *.gch 20 | *.pch 21 | 22 | # Libraries 23 | *.lib 24 | *.a 25 | *.la 26 | *.lo 27 | *.def 28 | *.exp 29 | 30 | # Shared objects (inc. Windows DLLs) 31 | *.dll 32 | *.so 33 | *.so.* 34 | *.dylib 35 | 36 | # Executables 37 | *.exe 38 | *.out 39 | *.app 40 | *.i*86 41 | *.x86_64 42 | *.hex 43 | -------------------------------------------------------------------------------- /bot-example.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 3 | bot-example.lua - Example code provided with the lua-telegram-bot library. 4 | 5 | Copyright (C) 2016 @cosmonawt 6 | 7 | This program is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation; either version 2 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along 18 | with this program; if not, write to the Free Software Foundation, Inc., 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 | 21 | ]] 22 | 23 | -- pass token as command line argument or insert it into code 24 | local token = arg[1] or "" 25 | 26 | -- create and configure new bot with set token 27 | local bot, extension = require("lua-bot-api").configure(token) 28 | 29 | -- override onMessageReceive function so it does what we want 30 | extension.onTextReceive = function (msg) 31 | print("New Message by " .. msg.from.first_name) 32 | 33 | if (msg.text == "/start") then 34 | bot.sendMessage(msg.from.id, "Hello there 👋\nMy name is " .. bot.first_name) 35 | elseif (msg.text == "ping") then 36 | bot.sendMessage(msg.chat.id, "pong!") 37 | else 38 | bot.sendMessage(msg.chat.id, "I am just an example, running on the Lua Telegram Framework written with ❤️ by @cosmonawt") 39 | end 40 | end 41 | 42 | -- override onPhotoReceive as well 43 | extension.onPhotoReceive = function (msg) 44 | print("Photo received!") 45 | bot.sendMessage(msg.chat.id, "Nice photo! It dimensions are " .. msg.photo[1].width .. "x" .. msg.photo[1].height) 46 | end 47 | 48 | -- This runs the internal update and callback handler 49 | -- you can even override run() 50 | extension.run() 51 | -------------------------------------------------------------------------------- /lua-bot-api-test.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 3 | lua-bot-api-test.lua - Example code provided with the lua-telegram-bot library. 4 | 5 | Copyright (C) 2016 @cosmonawt 6 | 7 | This program is free software; you can redistribute it and/or modify 8 | it under the terms of the GNU General Public License as published by 9 | the Free Software Foundation; either version 2 of the License, or 10 | (at your option) any later version. 11 | 12 | This program is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | GNU General Public License for more details. 16 | 17 | You should have received a copy of the GNU General Public License along 18 | with this program; if not, write to the Free Software Foundation, Inc., 19 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 | 21 | ]] 22 | 23 | -- pass token as command line argument or insert it into code 24 | local token = arg[1] or "" 25 | 26 | -- create and configure new bot with set token 27 | local bot = require("lua-bot-api").configure(token) 28 | 29 | -- get table of updates 30 | local updates = bot.getUpdates() 31 | 32 | -- for each update, check the message 33 | -- Note: processing a message does not prevent it from appearing again, unless 34 | -- you feed it's update id (incremented by one) back into getUpdates() 35 | for key, query in pairs(updates.result) do 36 | -- only reply to private chats, not groups 37 | if(query.message.chat.type == "private") then 38 | -- if message text was 'ping' 39 | if query.message.text == "ping" then 40 | -- reply with 'pong' 41 | bot.sendMessage(query.message.from.id, "pong") 42 | 43 | -- if message text was 'photo' 44 | elseif query.message.text == "photo" then 45 | -- get the users profile pictures 46 | local profilePicture = getUserProfilePhotos(query.message.from.id) 47 | -- and send the first one back to him using its file id 48 | bot.sendPhoto(query.message.from.id, profilePicture.result.photos[1][1].file_id) 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # lua-telegram-bot 2 | A simple LUA Library for the [Telegram Bot API](https://core.telegram.org/bots/api) 3 | 4 | ## Important 5 | **This library is not under active development anymore.** 6 | 7 | ## Changelog 8 | 9 | ### May 26 2016 - v2.1beta 10 | 11 | * Finally finished changes of [Telegram Bot API 2.0](https://core.telegram.org/bots/2-0-intro) 12 | * Implemented changes of Bot API 2.1 (also why I stayed with 2.1 version tag) 13 | * Code is still completely untested, but proper tests should be coming soon 14 | 15 | ### Apr 20 2016 - v2.1alpha 16 | 17 | * Added changes of [Telegram Bot API 2.0](https://core.telegram.org/bots/2-0-intro) 18 | * This update is still not ready, hence the alpha tag 19 | * `editMessageText`, `editMessageCaption`, `editMessageReplyMarkup` are *not* yet fully ready. 20 | * *Important:* Renamed `onMessageReceive` to `onTextReceive` 21 | * Created a discussion[group](https://telegram.me/luatelegrambot) you can join to ask questions. 22 | 23 | ### Mar 27 2016 - v2.0 24 | 25 | * Added [Library Extension](https://github.com/cosmonawt/lua-telegram-bot#Library-extension) which includes an internal update and callback handler and several callback functions which can be overridden. 26 | * Added file `bot-example.lua` with examples on how to use the new [Library Extension](https://github.com/cosmonawt/lua-telegram-bot#Library-extension). 27 | * Minor bug fixes 28 | 29 | ### Feb 28 2016 - v1.1 30 | 31 | * Added `disable_notification` argument to all sending methods to enable [silent messages](https://telegram.org/blog/channels-2-0#silent-messages) 32 | * Added `caption` argument to `sendDocument()` 33 | 34 | ### Jan 22 2016 - v1.0 35 | 36 | * Initial release v1.0-alpha 37 | 38 | 39 | ## Installing 40 | 41 | To install this module, place it inside the same folder your bot is located. 42 | 43 | This modules requires [luasec](https://github.com/brunoos/luasec) to work. 44 | You can easily install it with luarocks using `luarocks install luasec`. 45 | 46 | 47 | You will also need a Module for JSON en- and decoding, which can be found [here](http://regex.info/code/JSON.lua). 48 | Simply place it in the `lua-telegram-bot` Folder. 49 | 50 | 51 | ## Using 52 | 53 | To use this module, import it into your bot like this: 54 | ```lua 55 | local bot, extension = (require "lua-bot-api").configure(token) 56 | ``` 57 | Include your bot token as parameter for `configure()`. 58 | 59 | At the moment, only getUpdates method (aka polling) is supported, no webhooks. 60 | 61 | The `bot` Table exports variables and functions which return the following return values. 62 | The `extension` Table exports several callback functions as well as an update handler. Check [Library Extension](https://github.com/cosmonawt/lua-telegram-bot#Library-extension) for more information. 63 | 64 | ### Return values 65 | 66 | All functions return a table as received from the server if called successfully as their first return value. 67 | This does *not* mean the request was successful, for example in case of a bad `offset` in `getUpdates()`. 68 | 69 | A function returns `nil` and an `error description` if it was wrongly called (missing parameters). 70 | 71 | ### Available Variables 72 | 73 | ```lua 74 | id 75 | ``` 76 | ```lua 77 | username 78 | ``` 79 | ```lua 80 | first_name 81 | ``` 82 | 83 | ### Available Functions 84 | 85 | ```lua 86 | getMe() 87 | ``` 88 | ```lua 89 | getUpdates([offset] [,limit] [,timeout]) 90 | ``` 91 | ```lua 92 | sendMessage(chat_id, text [,parse_mode] [,disable_web_page_preview] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 93 | ``` 94 | ```lua 95 | forwardMessage(chat_id, from_chat_id [,disable_notification], message_id) 96 | ``` 97 | ```lua 98 | sendPhoto(chat_id, photo [,caption] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 99 | ``` 100 | ```lua 101 | sendAudio(chat_id, audio, duration [,performer] [,title] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 102 | ``` 103 | ```lua 104 | sendDocument(chat_id, document [,caption] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 105 | ``` 106 | ```lua 107 | sendSticker(chat_id, sticker [,disable_notification] [,reply_to_message_id] [,reply_markup]) 108 | ``` 109 | ```lua 110 | sendVideo(chat_id, video [,duration] [,caption] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 111 | ``` 112 | ```lua 113 | sendVoice(chat_id, voice [,duration] [,disable_notification] [,reply_to_message_id] [,reply_markup]) 114 | ``` 115 | ```lua 116 | sendLocation(chat_id, latitude, longitude [,disable_notification] [,reply_to_message_id] [,reply_markup]) 117 | ``` 118 | ```lua 119 | sendChatAction(chat_id, action) 120 | ``` 121 | ```lua 122 | getUserProfilePhotos(user_id [,offset] [,limit]) 123 | ``` 124 | ```lua 125 | getFile(file_id) 126 | ``` 127 | 128 | #### Inline Mode functions 129 | 130 | ```lua 131 | answerInlineQuery(inline_query_id, results [,cache_time] [,is_personal] [,next_offset]) 132 | ``` 133 | 134 | ```lua 135 | answerCallbackQuery(callback_query_id, text [, show_alert]) 136 | ``` 137 | 138 | #### Bot API 2.0 functions 139 | 140 | ```lua 141 | kickChatMember(chat_id, user_id) 142 | ``` 143 | 144 | ```lua 145 | unbanChatMember(chat_id, user_id) 146 | ``` 147 | 148 | ```lua 149 | editMessageText(chat_id, message_id, inline_message_id, text [, parse_mode] [, disable_web_page_preview] [, reply_markup]) 150 | ``` 151 | 152 | ```lua 153 | editMessageCaption(chat_id, message_id, inline_message_id, caption [, reply_markup]) 154 | ``` 155 | 156 | ```lua 157 | editMessageReplyMarkup(chat_id, message_id, inline_message_id [, reply_markup]) 158 | ``` 159 | 160 | #### Bot API 2.1 functions 161 | 162 | ```lua 163 | getChat(chat_id) 164 | ``` 165 | 166 | ```lua 167 | leaveChat(chat_id) 168 | ``` 169 | 170 | ```lua 171 | getChatAdministrators(chat_id) 172 | ``` 173 | 174 | ```lua 175 | getChatMembersCount(chat_id) 176 | ``` 177 | 178 | ```lua 179 | getChatMember(chat_id, user_id) 180 | ``` 181 | 182 | ### Helper functions: 183 | 184 | 185 | ```lua 186 | downloadFile(file_id [,download_path]) 187 | ``` 188 | - Downloads file from Telegram Servers. 189 | - `download_path` is an optional path where the file can be saved. If not specified, it will be saved in `/downloads/`. In both cases make sure the path already exists, since LUA can not create folders without additional modules. 190 | 191 | ```lua 192 | generateReplyKeyboardMarkup(keyboard [,resize_keyboard] [,one_time_keyboard] [,selective]) 193 | ``` 194 | - Generates a `ReplyKeyboardMarkup` of type `reply_markup` which can be sent optionally in other functions such as `sendMessage()`. 195 | - Displays the custom `keyboard` on the receivers device. 196 | 197 | ```lua 198 | generateReplyKeyboardHide([hide_keyboard] [,selective]) 199 | ``` 200 | - Generates a `ReplyKeyboardHide` of type `reply_markup` which can be sent optionally in other functions such as `sendMessage()`. 201 | - Forces to hide the custom `keyboard` on the receivers device. 202 | - `hide_keyboard` can be left out, as it is always `true`. 203 | 204 | ```lua 205 | generateForceReply([force_reply] [,selective]) 206 | ``` 207 | - Generates a `ForceReply` of type `reply_markup` which can be sent optionally in other functions such as `sendMessage()`. 208 | - Forces to reply to the corresponding message from the receivers device. 209 | - `force_reply` can be left out, as it is always `true`. 210 | 211 | ## Library Extension 212 | 213 | The Library extension was added to help developers focus on the things that actually matter in a bot: It's logic. 214 | It offers serveral callback functions which can be overridden to provide the wanted logic. 215 | 216 | ### Available Functions 217 | 218 | To use the extension, simply add another table variable to the initial `require` call like so: 219 | 220 | ```lua 221 | local bot, extension = require("lua-bot-api").configure(token) 222 | ``` 223 | 224 | The `extension` Table now stores the following functions: 225 | 226 | ```lua 227 | run() 228 | ``` 229 | - Provides an update handler which automatically fetches new updates from the server and calls the respective callback functions. 230 | 231 | ```lua 232 | onUpdateReceive(update) 233 | ``` 234 | - Is called every time an update, no matter of what type, is received. 235 | 236 | ```lua 237 | onTextReceive(message) 238 | ``` 239 | - Is called every time a text message is received. 240 | 241 | ```lua 242 | onPhotoReceive(message) 243 | ``` 244 | - Is called every time a photo is received. 245 | 246 | ```lua 247 | onAudioReceive(message) 248 | ``` 249 | - Is called every time audio is received. 250 | 251 | ```lua 252 | onDocumentReceive(message) 253 | ``` 254 | - Is called every time a document is received. 255 | 256 | ```lua 257 | onStickerReceive(message) 258 | ``` 259 | - Is called every time a sticker is received. 260 | 261 | ```lua 262 | onVideoReceive(message) 263 | ``` 264 | - Is called every time a video is received. 265 | 266 | ```lua 267 | onVoiceReceive(message) 268 | ``` 269 | - Is called every time a voice message is received. 270 | 271 | ```lua 272 | onContactReceive(message) 273 | ``` 274 | - Is called every time a contact is received. 275 | 276 | ```lua 277 | onLocationReceive(message) 278 | ``` 279 | - Is called every time a location is received. 280 | 281 | ```lua 282 | onLeftChatParticipant(message) 283 | ``` 284 | - Is called every time a member or the bot itself leaves the chat. 285 | 286 | ```lua 287 | onNewChatParticipant(message) 288 | ``` 289 | - Is called when a member joins a chat or the bot itself is added. 290 | 291 | ```lua 292 | onNewChatTitle(message) 293 | ``` 294 | - Is called every time the chat title is changed. 295 | 296 | ```lua 297 | onNewChatPhoto(message) 298 | ``` 299 | - Is called every time the chat photo is changed. 300 | 301 | ```lua 302 | onDeleteChatPhoto(message) 303 | ``` 304 | - Is called every time the chat photo is deleted. 305 | 306 | ```lua 307 | onGroupChatCreated(message) 308 | ``` 309 | - Is called every time a group chat is created directly with the bot. 310 | 311 | ```lua 312 | onSupergroupChatCreated(message) 313 | ``` 314 | 315 | ```lua 316 | onChannelChatCreated(message) 317 | ``` 318 | 319 | ```lua 320 | onMigrateToChatId(message) 321 | ``` 322 | - Is called every time a group is upgraded to a supergroup. 323 | 324 | ```lua 325 | onMigrateFromChatId(message) 326 | ``` 327 | 328 | ```lua 329 | onInlineQueryReceive(inlineQuery) 330 | ``` 331 | - Is called every time an inline query is received. 332 | 333 | ```lua 334 | onChosenInlineQueryReceive(chosenInlineQuery) 335 | ``` 336 | - Is called every time a chosen inline query result is received. 337 | 338 | ```lua 339 | onUnknownTypeReceive(unknownType) 340 | ``` 341 | - Is called every time when an unknown type is received. 342 | 343 | ### Using extension functions 344 | 345 | In order to provide your own desired behaviour to these callback functions, you need to override them, like so, for example: 346 | 347 | ```lua 348 | local bot, extension = require("lua-bot-api").configure(token) 349 | 350 | extension.onTextReceive = function (message) 351 | -- Your own desired behaviour here 352 | end 353 | 354 | extension.run(limit, timeout) 355 | 356 | ``` 357 | 358 | You can now use `extension.run()` to use the internal update handler to fetch new updates from the server and call the representive functions. 359 | It lets you pass the same `limit` and `timeout` parameters as in `getUpdates()` to control the handlers behaviour without rewriting it. 360 | 361 | You can even override `extension.run()` with your own update handler. 362 | 363 | See bot-example.lua for some examples on how to use extension functions. 364 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /lua-bot-api.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 3 | lua-bot-api.lua - A Lua library to the Telegram Bot API 4 | (https://core.telegram.org/bots/api) 5 | 6 | Copyright (C) 2016 @cosmonawt 7 | 8 | This program is free software; you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation; either version 2 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License along 19 | with this program; if not, write to the Free Software Foundation, Inc., 20 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 21 | 22 | ]] 23 | 24 | -- Import Libraries 25 | local https = require("ssl.https") 26 | local ltn12 = require("ltn12") 27 | local encode = require("multipart.multipart-post").encode 28 | local JSON = require("JSON") 29 | 30 | local M = {} -- Main Bot Framework 31 | local E = {} -- Extension Framework 32 | local C = {} -- Configure Constructor 33 | 34 | -- JSON Error handlers 35 | function JSON:onDecodeError(message, text, location, etc) 36 | if text then 37 | if location then 38 | message = string.format("%s at char %d of: %s", message, location, text) 39 | else 40 | message = string.format("%s: %s", message, text) 41 | end 42 | end 43 | --print((os.date("%x %X")), "Error while decoding JSON:\n", message) 44 | local datefile = os.date("%d-%m-%Y.txt") 45 | print((os.date("%x %X")), "Error: decode JSON, logged in ".. datefile) 46 | local log = io.open("errors/" .. datefile,"a+") -- open log 47 | log:write((os.date("%x %X")), "Error while decoding JSON:\n", message .. "\n") -- write in log 48 | log:close() 49 | 50 | end 51 | 52 | function JSON:onDecodeOfHTMLError(message, text, _nil, etc) 53 | if text then 54 | if location then 55 | message = string.format("%s at char %d of: %s", message, location, text) 56 | else 57 | message = string.format("%s: %s", message, text) 58 | end 59 | end 60 | --print((os.date("%x %X")), "Error while decoding JSON [HTML]:\n", message) 61 | local datefile = os.date("%d-%m-%Y.txt") 62 | print((os.date("%x %X")), "Error: decode JSON [HTML], logged in ".. datefile) 63 | local log = io.open("errors/" .. datefile,"a+") -- open log 64 | log:write((os.date("%x %X")), "Error while decoding JSON [HTML]:\n", message .. "\n") -- write in log 65 | log:close() 66 | end 67 | 68 | function JSON:onDecodeOfNilError(message, _nil, _nil, etc) 69 | if text then 70 | if location then 71 | message = string.format("%s at char %d of: %s", message, location, text) 72 | else 73 | message = string.format("%s: %s", message, text) 74 | end 75 | end 76 | print((os.date("%x %X")), "Error while decoding JSON [nil]:\n", message) 77 | end 78 | 79 | function JSON:onEncodeError(message, etc) 80 | print((os.date("%x %X")), "Error while encoding JSON:\n", message) 81 | end 82 | 83 | -- configure and initialize bot 84 | local function configure(token) 85 | 86 | if (token == "") then 87 | token = nil 88 | end 89 | 90 | M.token = assert(token, "No token specified!") 91 | local bot_info = M.getMe() 92 | if (bot_info) then 93 | M.id = bot_info.result.id 94 | M.username = bot_info.result.username 95 | M.first_name = bot_info.result.first_name 96 | end 97 | return M, E 98 | end 99 | 100 | C.configure = configure 101 | 102 | local function makeRequest(method, request_body) 103 | 104 | local response = {} 105 | local body, boundary = encode(request_body) 106 | 107 | local success, code, headers, status = https.request{ 108 | url = "https://api.telegram.org/bot" .. M.token .. "/" .. method, 109 | method = "POST", 110 | headers = { 111 | ["Content-Type"] = "multipart/form-data; boundary=" .. boundary, 112 | ["Content-Length"] = string.len(body), 113 | }, 114 | source = ltn12.source.string(body), 115 | sink = ltn12.sink.table(response), 116 | } 117 | 118 | local r = { 119 | success = success or "false", 120 | code = code or "0", 121 | headers = table.concat(headers or {"no headers"}), 122 | status = status or "0", 123 | body = table.concat(response or {"no response"}), 124 | } 125 | return r 126 | end 127 | 128 | -- Helper functions 129 | 130 | local function downloadFile(file_id, download_path) 131 | 132 | if not file_id then return nil, "file_id not specified" end 133 | if not download_path then return nil, "download_path not specified" end 134 | 135 | local response = {} 136 | 137 | local file_info = getFile(file_id) 138 | local download_file_path = download_path or "downloads/" .. file_info.result.file_path 139 | 140 | local download_file = io.open(download_file_path, "w") 141 | 142 | if not download_file then return nil, "download_file could not be created" 143 | else 144 | local success, code, headers, status = https.request{ 145 | url = "https://api.telegram.org/file/bot" .. M.token .. "/" .. file_info.result.file_path, 146 | --source = ltn12.source.string(body), 147 | sink = ltn12.sink.file(download_file), 148 | } 149 | 150 | local r = { 151 | success = true, 152 | download_path = download_file_path, 153 | file = file_info.result 154 | } 155 | return r 156 | end 157 | end 158 | 159 | M.downloadFile = downloadFile 160 | 161 | local function generateReplyKeyboardMarkup(keyboard, resize_keyboard, one_time_keyboard, selective) 162 | 163 | if not keyboard then return nil, "keyboard not specified" end 164 | if #keyboard < 1 then return nil, "keyboard is empty" end 165 | 166 | local response = {} 167 | 168 | response.keyboard = keyboard 169 | response.resize_keyboard = resize_keyboard 170 | response.one_time_keyboard = one_time_keyboard 171 | response.selective = selective 172 | 173 | 174 | local responseString = JSON:encode(response) 175 | return responseString 176 | end 177 | 178 | M.generateReplyKeyboardMarkup = generateReplyKeyboardMarkup 179 | 180 | 181 | local function generateReplyKeyboardHide(hide_keyboard, selective) 182 | 183 | local response = {} 184 | 185 | response.hide_keyboard = true 186 | response.selective = selective 187 | 188 | local responseString = JSON:encode(response) 189 | return responseString 190 | end 191 | 192 | M.generateReplyKeyboardHide = generateReplyKeyboardHide 193 | 194 | 195 | local function generateForceReply(force_reply, selective) 196 | 197 | local response = {} 198 | 199 | response.force_reply = true 200 | response.selective = selective 201 | 202 | local responseString = JSON:encode(response) 203 | return responseString 204 | end 205 | 206 | M.generateForceReply = generateForceReply 207 | 208 | -- Bot API 1.0 209 | 210 | local function getUpdates(offset, limit, timeout, allowed_updates) 211 | 212 | local request_body = {} 213 | 214 | request_body.offset = offset 215 | request_body.limit = limit 216 | request_body.timeout = timeout or 0 217 | request_body.allowed_updates = allowed_updates or nil 218 | 219 | local response = makeRequest("getUpdates", request_body) 220 | 221 | if (response.success == 1) then 222 | return JSON:decode(response.body) 223 | else 224 | return nil, "Request Error" 225 | end 226 | end 227 | 228 | M.getUpdates = getUpdates 229 | 230 | 231 | local function getMe() 232 | local request_body = {""} 233 | 234 | local response = makeRequest("getMe",request_body) 235 | 236 | if (response.success == 1) then 237 | return JSON:decode(response.body) 238 | else 239 | return nil, "Request Error" 240 | end 241 | end 242 | 243 | M.getMe = getMe 244 | 245 | 246 | local function sendMessage(chat_id, text, parse_mode, disable_web_page_preview, disable_notification, reply_to_message_id, reply_markup) 247 | 248 | if not chat_id then return nil, "chat_id not specified" end 249 | if not text then return nil, "text not specified" end 250 | 251 | local allowed_parse_mode = { 252 | ["Markdown"] = true, 253 | ["HTML"] = true 254 | } 255 | 256 | if (not allowed_parse_mode[parse_mode]) then parse_mode = "" end 257 | 258 | local request_body = {} 259 | 260 | request_body.chat_id = chat_id 261 | request_body.text = tostring(text) 262 | request_body.parse_mode = parse_mode 263 | request_body.disable_web_page_preview = tostring(disable_web_page_preview) 264 | request_body.disable_notification = tostring(disable_notification) 265 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 266 | request_body.reply_markup = reply_markup or "" 267 | 268 | local response = makeRequest("sendMessage",request_body) 269 | 270 | if (response.success == 1) then 271 | return JSON:decode(response.body) 272 | else 273 | return nil, "Request Error" 274 | end 275 | end 276 | 277 | M.sendMessage = sendMessage 278 | 279 | local function forwardMessage(chat_id, from_chat_id, disable_notification, message_id) 280 | 281 | if not chat_id then return nil, "chat_id not specified" end 282 | if not from_chat_id then return nil, "from_chat_id not specified" end 283 | if not message_id then return nil, "message_id not specified" end 284 | 285 | local request_body = {""} 286 | 287 | request_body.chat_id = chat_id 288 | request_body.from_chat_id = from_chat_id 289 | request_body.disable_notification = tostring(disable_notification) 290 | request_body.message_id = tonumber(message_id) 291 | 292 | local response = makeRequest("forwardMessage",request_body) 293 | 294 | if (response.success == 1) then 295 | return JSON:decode(response.body) 296 | else 297 | return nil, "Request Error" 298 | end 299 | end 300 | 301 | M.forwardMessage = forwardMessage 302 | 303 | 304 | local function sendPhoto(chat_id, photo, caption, disable_notification, reply_to_message_id, reply_markup) 305 | 306 | if not chat_id then return nil, "chat_id not specified" end 307 | if not photo then return nil, "photo not specified" end 308 | 309 | local request_body = {""} 310 | local file_id = "" 311 | local photo_data = {} 312 | 313 | if not(string.find(photo, "%.")) then 314 | file_id = photo 315 | else 316 | file_id = nil 317 | local photo_file = io.open(photo, "r") 318 | 319 | photo_data.filename = photo 320 | photo_data.data = photo_file:read("*a") 321 | photo_data.content_type = "image" 322 | 323 | photo_file:close() 324 | end 325 | 326 | request_body.chat_id = chat_id 327 | request_body.photo = file_id or photo_data 328 | request_body.caption = caption 329 | request_body.disable_notification = tostring(disable_notification) 330 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 331 | request_body.reply_markup = reply_markup 332 | 333 | local response = makeRequest("sendPhoto",request_body) 334 | 335 | if (response.success == 1) then 336 | return JSON:decode(response.body) 337 | else 338 | return nil, "Request Error" 339 | end 340 | end 341 | 342 | M.sendPhoto = sendPhoto 343 | 344 | 345 | local function sendAudio(chat_id, audio, caption, duration, performer, title, disable_notification, reply_to_message_id, reply_markup) 346 | 347 | if not chat_id then return nil, "chat_id not specified" end 348 | if not audio then return nil, "audio not specified" end 349 | 350 | local request_body = {} 351 | local file_id = "" 352 | local audio_data = {} 353 | 354 | if not(string.find(audio, "%.mp3")) then 355 | file_id = audio 356 | else 357 | file_id = nil 358 | local audio_file = io.open(audio, "r") 359 | 360 | audio_data.filename = audio 361 | audio_data.data = audio_file:read("*a") 362 | audio_data.content_type = "audio/mpeg" 363 | 364 | audio_file:close() 365 | end 366 | 367 | request_body.chat_id = chat_id 368 | request_body.audio = file_id or audio_data 369 | request_body.duration = duration 370 | request_body.caption = caption 371 | request_body.performer = performer 372 | request_body.title = title 373 | request_body.disable_notification = tostring(disable_notification) 374 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 375 | request_body.reply_markup = reply_markup 376 | 377 | local response = makeRequest("sendAudio",request_body) 378 | 379 | if (response.success == 1) then 380 | return JSON:decode(response.body) 381 | else 382 | return nil, "Request Error" 383 | end 384 | end 385 | 386 | M.sendAudio = sendAudio 387 | 388 | 389 | local function sendDocument(chat_id, document, caption, disable_notification, reply_to_message_id, reply_markup) 390 | 391 | if not chat_id then return nil, "chat_id not specified" end 392 | if not document then return nil, "document not specified" end 393 | 394 | local request_body = {} 395 | local file_id = "" 396 | local document_data = {} 397 | 398 | if not(string.find(document, "%.")) then 399 | file_id = document 400 | else 401 | file_id = nil 402 | local document_file = io.open(document, "r") 403 | 404 | document_data.filename = document 405 | document_data.data = document_file:read("*a") 406 | 407 | document_file:close() 408 | end 409 | 410 | request_body.chat_id = chat_id 411 | request_body.document = file_id or document_data 412 | request_body.caption = caption 413 | request_body.disable_notification = tostring(disable_notification) 414 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 415 | request_body.reply_markup = reply_markup 416 | 417 | local response = makeRequest("sendDocument",request_body) 418 | 419 | if (response.success == 1) then 420 | return JSON:decode(response.body) 421 | else 422 | return nil, "Request Error" 423 | end 424 | end 425 | 426 | M.sendDocument = sendDocument 427 | 428 | 429 | local function sendSticker(chat_id, sticker, disable_notification, reply_to_message_id, reply_markup) 430 | 431 | if not chat_id then return nil, "chat_id not specified" end 432 | if not sticker then return nil, "sticker not specified" end 433 | 434 | local request_body = {} 435 | local file_id = "" 436 | local sticker_data = {} 437 | 438 | if not(string.find(sticker, "%.webp")) then 439 | file_id = sticker 440 | else 441 | file_id = nil 442 | local sticker_file = io.open(sticker, "r") 443 | 444 | sticker_data.filename = sticker 445 | sticker_data.data = sticker_file:read("*a") 446 | sticker_data.content_type = "image/webp" 447 | 448 | sticker_file:close() 449 | end 450 | 451 | request_body.chat_id = chat_id 452 | request_body.sticker = file_id or sticker_data 453 | request_body.disable_notification = tostring(disable_notification) 454 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 455 | request_body.reply_markup = reply_markup 456 | 457 | local response = makeRequest("sendSticker",request_body) 458 | 459 | if (response.success == 1) then 460 | return JSON:decode(response.body) 461 | else 462 | return nil, "Request Error" 463 | end 464 | end 465 | 466 | M.sendSticker = sendSticker 467 | 468 | 469 | local function sendVideo(chat_id, video, duration, caption, disable_notification, reply_to_message_id, reply_markup) 470 | 471 | if not chat_id then return nil, "chat_id not specified" end 472 | if not video then return nil, "video not specified" end 473 | 474 | local request_body = {} 475 | local file_id = "" 476 | local video_data = {} 477 | 478 | if not(string.find(video, "%.")) then 479 | file_id = video 480 | else 481 | file_id = nil 482 | local video_file = io.open(video, "r") 483 | 484 | video_data.filename = video 485 | video_data.data = video_file:read("*a") 486 | video_data.content_type = "video" 487 | 488 | video_file:close() 489 | end 490 | 491 | request_body.chat_id = chat_id 492 | request_body.video = file_id or video_data 493 | request_body.duration = duration 494 | request_body.caption = caption 495 | request_body.disable_notification = tostring(disable_notification) 496 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 497 | request_body.reply_markup = reply_markup 498 | 499 | local response = makeRequest("sendVideo",request_body) 500 | 501 | if (response.success == 1) then 502 | return JSON:decode(response.body) 503 | else 504 | return nil, "Request Error" 505 | end 506 | end 507 | 508 | M.sendVideo = sendVideo 509 | 510 | 511 | local function sendVoice(chat_id, voice, caption, duration, disable_notification, reply_to_message_id, reply_markup) 512 | 513 | if not chat_id then return nil, "chat_id not specified" end 514 | if not voice then return nil, "voice not specified" end 515 | 516 | local request_body = {} 517 | local file_id = "" 518 | local voice_data = {} 519 | 520 | if not(string.find(voice, "%.ogg")) then 521 | file_id = voice 522 | else 523 | file_id = nil 524 | local voice_file = io.open(voice, "r") 525 | 526 | voice_data.filename = voice 527 | voice_data.data = voice_file:read("*a") 528 | voice_data.content_type = "audio/ogg" 529 | 530 | voice_file:close() 531 | end 532 | 533 | request_body.chat_id = chat_id 534 | request_body.voice = file_id or voice_data 535 | request_body.duration = duration 536 | request_body.caption = caption 537 | request_body.disable_notification = tostring(disable_notification) 538 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 539 | request_body.reply_markup = reply_markup 540 | 541 | local response = makeRequest("sendVoice",request_body) 542 | 543 | if (response.success == 1) then 544 | return JSON:decode(response.body) 545 | else 546 | return nil, "Request Error" 547 | end 548 | end 549 | 550 | M.sendVoice = sendVoice 551 | 552 | local function sendLocation(chat_id, latitude, longitude, disable_notification, reply_to_message_id, reply_markup) 553 | 554 | if not chat_id then return nil, "chat_id not specified" end 555 | if not latitude then return nil, "latitude not specified" end 556 | if not longitude then return nil, "longitude not specified" end 557 | 558 | local request_body = {} 559 | 560 | request_body.chat_id = chat_id 561 | request_body.latitude = tonumber(latitude) 562 | request_body.longitude = tonumber(longitude) 563 | request_body.disable_notification = tostring(disable_notification) 564 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 565 | request_body.reply_markup = reply_markup 566 | 567 | local response = makeRequest("sendLocation",request_body) 568 | 569 | if (response.success == 1) then 570 | return JSON:decode(response.body) 571 | else 572 | return nil, "Request Error" 573 | end 574 | end 575 | 576 | M.sendLocation = sendLocation 577 | 578 | local function sendChatAction(chat_id, action) 579 | 580 | if not chat_id then return nil, "chat_id not specified" end 581 | if not action then return nil, "action not specified" end 582 | 583 | local request_body = {} 584 | 585 | local allowedAction = { 586 | ["typing"] = true, 587 | ["upload_photo"] = true, 588 | ["record_video"] = true, 589 | ["upload_video"] = true, 590 | ["record_audio"] = true, 591 | ["upload_audio"] = true, 592 | ["upload_document"] = true, 593 | ["find_location"] = true, 594 | } 595 | 596 | if (not allowedAction[action]) then action = "typing" end 597 | 598 | request_body.chat_id = chat_id 599 | request_body.action = action 600 | 601 | local response = makeRequest("sendChatAction",request_body) 602 | 603 | if (response.success == 1) then 604 | return JSON:decode(response.body) 605 | else 606 | return nil, "Request Error" 607 | end 608 | end 609 | 610 | M.sendChatAction = sendChatAction 611 | 612 | local function getUserProfilePhotos(user_id, offset, limit) 613 | 614 | if not user_id then return nil, "user_id not specified" end 615 | 616 | local request_body = {} 617 | 618 | request_body.user_id = tonumber(user_id) 619 | request_body.offset = offset 620 | request_body.limit = limit 621 | 622 | local response = makeRequest("getUserProfilePhotos",request_body) 623 | 624 | if (response.success == 1) then 625 | return JSON:decode(response.body) 626 | else 627 | return nil, "Request Error" 628 | end 629 | end 630 | 631 | M.getUserProfilePhotos = getUserProfilePhotos 632 | 633 | local function getFile(file_id) 634 | 635 | if not file_id then return nil, "file_id not specified" end 636 | 637 | local request_body = {} 638 | 639 | request_body.file_id = file_id 640 | 641 | local response = makeRequest("getFile",request_body) 642 | 643 | if (response.success == 1) then 644 | return JSON:decode(response.body) 645 | else 646 | return nil, "Request Error" 647 | end 648 | end 649 | 650 | M.getFile = getFile 651 | 652 | local function answerInlineQuery(inline_query_id, results, cache_time, is_personal, next_offset, switch_pm_text, switch_pm_parameter) 653 | 654 | if not inline_query_id then return nil, "inline_query_id not specified" end 655 | if not results then return nil, "results not specified" end 656 | 657 | local request_body = {} 658 | 659 | request_body.inline_query_id = tostring(inline_query_id) 660 | request_body.results = JSON:encode(results) 661 | request_body.cache_time = tonumber(cache_time) 662 | request_body.is_personal = tostring(is_personal) 663 | request_body.next_offset = tostring(next_offset) 664 | request_body.switch_pm_text = tostring(switch_pm_text) 665 | request_body.switch_pm_parameter = tostring(switch_pm_text) 666 | 667 | local response = makeRequest("answerInlineQuery",request_body) 668 | 669 | if (response.success == 1) then 670 | return JSON:decode(response.body) 671 | else 672 | return nil, "Request Error" 673 | end 674 | end 675 | 676 | M.answerInlineQuery = answerInlineQuery 677 | 678 | -- Bot API 2.0 679 | 680 | local function sendVenue(chat_id, latitude, longitude, title, adress, foursquare_id, disable_notification, reply_to_message_id, reply_markup) 681 | 682 | if not chat_id then return nil, "chat_id not specified" end 683 | if not latitude then return nil, "latitude not specified" end 684 | if not longitude then return nil, "longitude not specified" end 685 | if not title then return nil, "title not specified" end 686 | if not adress then return nil, "adress not specified" end 687 | 688 | local request_body = {} 689 | 690 | request_body.chat_id = chat_id 691 | request_body.latitude = tonumber(latitude) 692 | request_body.longitude = tonumber(longitude) 693 | request_body.title = title 694 | request_body.adress = adress 695 | request_body.foursquare_id = foursquare_id 696 | request_body.disable_notification = tostring(disable_notification) 697 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 698 | request_body.reply_markup = reply_markup 699 | 700 | local response = makeRequest("sendVenue",request_body) 701 | 702 | if (response.success == 1) then 703 | return JSON:decode(response.body) 704 | else 705 | return nil, "Request Error" 706 | end 707 | end 708 | 709 | M.sendVenue = sendVenue 710 | 711 | local function sendContact(chat_id, phone_number, first_name, last_name, disable_notification, reply_to_message_id, reply_markup) 712 | 713 | if not chat_id then return nil, "chat_id not specified" end 714 | if not phone_number then return nil, "phone_number not specified" end 715 | if not first_name then return nil, "first_name not specified" end 716 | 717 | request_body.chat_id = chat_id 718 | request_body.phone_number = tostring(phone_number) 719 | request_body.first_name = tostring(first_name) 720 | request_body.last_name = tostring(last_name) 721 | request_body.disable_notification = tostring(disable_notification) 722 | request_body.reply_to_message_id = tonumber(reply_to_message_id) 723 | request_body.reply_markup = reply_markup 724 | 725 | local response = makeRequest("sendContact",request_body) 726 | 727 | if (response.success == 1) then 728 | return JSON:decode(response.body) 729 | else 730 | return nil, "Request Error" 731 | end 732 | end 733 | 734 | M.sendContact = sendContact 735 | 736 | local function kickChatMember(chat_id, user_id) 737 | if not chat_id then return nil, "chat_id not specified" end 738 | if not user_id then return nil, "user_id not specified" end 739 | 740 | local request_body = {} 741 | 742 | request_body.chat_id = chat_id 743 | request_body.user_id = tonumber(user_id) 744 | 745 | local response = makeRequest("kickChatMember",request_body) 746 | 747 | if (response.success == 1) then 748 | return JSON:decode(response.body) 749 | else 750 | return nil, "Request Error" 751 | end 752 | end 753 | 754 | M.kickChatMember = kickChatMember 755 | 756 | local function unbanChatMember(chat_id, user_id) 757 | if not chat_id then return nil, "chat_id not specified" end 758 | if not user_id then return nil, "user_id not specified" end 759 | 760 | local request_body = {} 761 | 762 | request_body.chat_id = chat_id 763 | request_body.user_id = tonumber(user_id) 764 | 765 | local response = makeRequest("unbanChatMember",request_body) 766 | 767 | if (response.success == 1) then 768 | return JSON:decode(response.body) 769 | else 770 | return nil, "Request Error" 771 | end 772 | end 773 | 774 | M.unbanChatMember = unbanChatMember 775 | 776 | local function answerCallbackQuery(callback_query_id, text, show_alert, cache_time) 777 | 778 | if not callback_query_id then return nil, "callback_query_id not specified" end 779 | 780 | local request_body = {} 781 | 782 | request_body.callback_query_id = tostring(callback_query_id) 783 | request_body.text = tostring(text) 784 | request_body.show_alert = tostring(show_alert) 785 | request_body.cache_time = tostring(cache_time) 786 | 787 | local response = makeRequest("answerCallbackQuery",request_body) 788 | 789 | if (response.success == 1) then 790 | return JSON:decode(response.body) 791 | else 792 | return nil, "Request Error" 793 | end 794 | end 795 | 796 | M.answerCallbackQuery = answerCallbackQuery 797 | 798 | local function editMessageText(chat_id, message_id, inline_message_id, text, parse_mode, disable_web_page_preview, reply_markup) 799 | 800 | if not chat_id and not inline_message_id then return nil, "chat_id not specified" end 801 | if not message_id and not inline_message_id then return nil, "message_id not specified" end 802 | if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end 803 | if not text then return nil, "text not specified" end 804 | 805 | local request_body = {} 806 | 807 | request_body.chat_id = chat_id 808 | request_body.message_id = tonumber(message_id) 809 | request_body.inline_message_id = tostring(inline_message_id) 810 | request_body.text = tostring(text) 811 | request_body.parse_mode = tostring(parse_mode) 812 | request_body.disable_web_page_preview = disable_web_page_preview 813 | request_body.reply_markup = reply_markup 814 | 815 | local response = makeRequest("editMessageText",request_body) 816 | 817 | if (response.success == 1) then 818 | return JSON:decode(response.body) 819 | else 820 | return nil, "Request Error" 821 | end 822 | end 823 | 824 | M.editMessageText = editMessageText 825 | 826 | local function editMessageCaption(chat_id, message_id, inline_message_id, caption, reply_markup) 827 | 828 | if not chat_id and not inline_message_id then return nil, "chat_id not specified" end 829 | if not message_id and not inline_message_id then return nil, "message_id not specified" end 830 | if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end 831 | if not caption then return nil, "caption not specified" end 832 | 833 | local request_body = {} 834 | 835 | request_body.chat_id = chat_id 836 | request_body.message_id = tonumber(message_id) 837 | request_body.inline_message_id = tostring(inline_message_id) 838 | request_body.caption = tostring(caption) 839 | request_body.reply_markup = reply_markup 840 | 841 | local response = makeRequest("editMessageCaption",request_body) 842 | 843 | if (response.success == 1) then 844 | return JSON:decode(response.body) 845 | else 846 | return nil, "Request Error" 847 | end 848 | end 849 | 850 | M.editMessageCaption = editMessageCaption 851 | 852 | local function editMessageReplyMarkup(chat_id, message_id, inline_message_id, reply_markup) 853 | 854 | if not chat_id and not inline_message_id then return nil, "chat_id not specified" end 855 | if not message_id and not inline_message_id then return nil, "message_id not specified" end 856 | if not inline_message_id and not (chat_id and message_id) then return nil, "inline_message_id not specified" end 857 | 858 | local request_body = {} 859 | 860 | request_body.chat_id = chat_id 861 | request_body.message_id = tonumber(message_id) 862 | request_body.inline_message_id = tostring(inline_message_id) 863 | request_body.reply_markup = reply_markup 864 | 865 | local response = makeRequest("editMessageReplyMarkup",request_body) 866 | 867 | if (response.success == 1) then 868 | return JSON:decode(response.body) 869 | else 870 | return nil, "Request Error" 871 | end 872 | end 873 | 874 | M.editMessageReplyMarkup = editMessageReplyMarkup 875 | 876 | -- Bot API 2.1 877 | 878 | local function getChat(chat_id) 879 | 880 | if not chat_id then return nil, "chat_id not specified" end 881 | 882 | local request_body = {} 883 | request_body.chat_id = chat_id 884 | 885 | local response = makeRequest("getChat", request_body) 886 | 887 | if (response.success == 1) then 888 | return JSON:decode(response.body) 889 | else 890 | return nil, "Request Error" 891 | end 892 | end 893 | 894 | M.getChat = getChat 895 | 896 | local function leaveChat(chat_id) 897 | 898 | if not chat_id then return nil, "chat_id not specified" end 899 | 900 | local request_body = {} 901 | request_body.chat_id = chat_id 902 | 903 | local response = makeRequest("leaveChat", request_body) 904 | 905 | if (response.success == 1) then 906 | return JSON:decode(response.body) 907 | else 908 | return nil, "Request Error" 909 | end 910 | end 911 | 912 | M.leaveChat = leaveChat 913 | 914 | local function getChatAdministrators(chat_id) 915 | 916 | if not chat_id then return nil, "chat_id not specified" end 917 | 918 | local request_body = {} 919 | request_body.chat_id = chat_id 920 | 921 | local response = makeRequest("getChatAdministrators", request_body) 922 | 923 | if (response.success == 1) then 924 | return JSON:decode(response.body) 925 | else 926 | return nil, "Request Error" 927 | end 928 | end 929 | 930 | M.getChatAdministrators = getChatAdministrators 931 | 932 | local function getChatMembersCount(chat_id) 933 | 934 | if not chat_id then return nil, "chat_id not specified" end 935 | 936 | local request_body = {} 937 | request_body.chat_id = chat_id 938 | 939 | local response = makeRequest("getChatMembersCount", request_body) 940 | 941 | if (response.success == 1) then 942 | return JSON:decode(response.body) 943 | else 944 | return nil, "Request Error" 945 | end 946 | end 947 | 948 | M.getChatMembersCount = getChatMembersCount 949 | 950 | local function getChatMember(chat_id, user_id) 951 | 952 | if not chat_id then return nil, "chat_id not specified" end 953 | if not user_id then return nil, "user_id not specified" end 954 | 955 | 956 | local request_body = {} 957 | request_body.chat_id = chat_id 958 | request_body.user_id = user_id 959 | 960 | local response = makeRequest("getChatMember", request_body) 961 | 962 | if (response.success == 1) then 963 | return JSON:decode(response.body) 964 | else 965 | return nil, "Request Error" 966 | end 967 | end 968 | 969 | M.getChatMember = getChatMember 970 | 971 | -- Extension Framework 972 | 973 | local function onUpdateReceive(update) end 974 | E.onUpdateReceive = onUpdateReceive 975 | 976 | local function onTextReceive(message) end 977 | E.onMessageReceive = onMessageReceive 978 | 979 | local function onPhotoReceive(message) end 980 | E.onPhotoReceive = onPhotoReceive 981 | 982 | local function onAudioReceive(message) end 983 | E.onAudioReceive = onAudioReceive 984 | 985 | local function onDocumentReceive(message) end 986 | E.onDocumentReceive = onDocumentReceive 987 | 988 | local function onStickerReceive(message) end 989 | E.onStickerReceive = onStickerReceive 990 | 991 | local function onVideoReceive(message) end 992 | E.onVideoReceive = onVideoReceive 993 | 994 | local function onVoiceReceive(message) end 995 | E.onVoiceReceive = onVoiceReceive 996 | 997 | local function onContactReceive(message) end 998 | E.onContactReceive = onContactReceive 999 | 1000 | local function onLocationReceive(message) end 1001 | E.onLocationReceive = onLocationReceive 1002 | 1003 | local function onLeftChatParticipant(message) end 1004 | E.onLeftChatParticipant = onLeftChatParticipant 1005 | 1006 | local function onNewChatParticipant(message) end 1007 | E.onNewChatParticipant = onNewChatParticipant 1008 | 1009 | local function onNewChatTitle(message) end 1010 | E.onNewChatTitle = onNewChatTitle 1011 | 1012 | local function onNewChatPhoto(message) end 1013 | E.onNewChatPhoto = onNewChatPhoto 1014 | 1015 | local function onDeleteChatPhoto(message) end 1016 | E.onDeleteChatPhoto = onDeleteChatPhoto 1017 | 1018 | local function onGroupChatCreated(message) end 1019 | E.onGroupChatCreated = onGroupChatCreated 1020 | 1021 | local function onSupergroupChatCreated(message) end 1022 | E.onsuperGroupChatCreated = onsuperGroupChatCreated 1023 | 1024 | local function onChannelChatCreated(message) end 1025 | E.onChannelChatCreated = onChannelChatCreated 1026 | 1027 | local function onMigrateToChatId(message) end 1028 | E.onMigrateToChatId = onMigrateToChatId 1029 | 1030 | local function onMigrateFromChatId(message) end 1031 | E.onMigrateFromChatId = onMigrateFromChatId 1032 | 1033 | local function onEditedMessageReceive(message) end 1034 | E.onEditedMessageReceive = onEditedMessageReceive 1035 | 1036 | local function onInlineQueryReceive(inlineQuery) end 1037 | E.onInlineQueryReceive = onInlineQueryReceive 1038 | 1039 | local function onChosenInlineQueryReceive(chosenInlineQuery) end 1040 | E.onChosenInlineQueryReceive = onChosenInlineQueryReceive 1041 | 1042 | local function onCallbackQueryReceive(CallbackQuery) end 1043 | E.onCallbackQueryReceive = onCallbackQueryReceive 1044 | 1045 | local function onChannelPost(post) end 1046 | E.onChannelPost = onChannelPost 1047 | 1048 | local function onChannelEditPost(post) end 1049 | E.onChannelEditPost = onChannelEditPost 1050 | 1051 | local function onUnknownTypeReceive(unknownType) 1052 | print("new unknownType!") 1053 | end 1054 | E.onUnknownTypeReceive = onUnknownTypeReceive 1055 | 1056 | local function parseUpdateCallbacks(update) 1057 | if (update) then 1058 | E.onUpdateReceive(update) 1059 | end 1060 | if (update.message) then 1061 | if (update.message.text) then 1062 | E.onTextReceive(update.message) 1063 | elseif (update.message.photo) then 1064 | E.onPhotoReceive(update.message) 1065 | elseif (update.message.audio) then 1066 | E.onAudioReceive(update.message) 1067 | elseif (update.message.document) then 1068 | E.onDocumentReceive(update.message) 1069 | elseif (update.message.sticker) then 1070 | E.onStickerReceive(update.message) 1071 | elseif (update.message.video) then 1072 | E.onVideoReceive(update.message) 1073 | elseif (update.message.voice) then 1074 | E.onVoiceReceive(update.message) 1075 | elseif (update.message.contact) then 1076 | E.onContactReceive(update.message) 1077 | elseif (update.message.location) then 1078 | E.onLocationReceive(update.message) 1079 | elseif (update.message.left_chat_participant) then 1080 | E.onLeftChatParticipant(update.message) 1081 | elseif (update.message.new_chat_participant) then 1082 | E.onNewChatParticipant(update.message) 1083 | elseif (update.message.new_chat_photo) then 1084 | E.onNewChatPhoto(update.message) 1085 | elseif (update.message.delete_chat_photo) then 1086 | E.onDeleteChatPhoto(update.message) 1087 | elseif (update.message.group_chat_created) then 1088 | E.onGroupChatCreated(update.message) 1089 | elseif (update.message.supergroup_chat_created) then 1090 | E.onSupergroupChatCreated(update.message) 1091 | elseif (update.message.channel_chat_created) then 1092 | E.onChannelChatCreated(update.message) 1093 | elseif (update.message.migrate_to_chat_id) then 1094 | E.onMigrateToChatId(update.message) 1095 | elseif (update.message.migrate_from_chat_id) then 1096 | E.onMigrateFromChatId(update.message) 1097 | else 1098 | E.onUnknownTypeReceive(update) 1099 | end 1100 | elseif (update.edited_message) then 1101 | E.onEditedMessageReceive(update.edited_message) 1102 | elseif (update.inline_query) then 1103 | E.onInlineQueryReceive(update.inline_query) 1104 | elseif (update.chosen_inline_result) then 1105 | E.onChosenInlineQueryReceive(update.chosen_inline_result) 1106 | elseif (update.callback_query) then 1107 | E.onCallbackQueryReceive(update.callback_query) 1108 | elseif (update.channel_post) then 1109 | E.onChannelPost(update.channel_post) 1110 | elseif (update.edited_channel_post) then 1111 | E.onChannelEditPost(update.edited_channel_post) 1112 | else 1113 | E.onUnknownTypeReceive(update) 1114 | end 1115 | end 1116 | 1117 | local function run(limit, timeout) 1118 | if limit == nil then limit = 1 end 1119 | if timeout == nil then timeout = 0 end 1120 | local offset = 0 1121 | while true do 1122 | local updates = M.getUpdates(offset, limit, timeout) 1123 | if(updates) then 1124 | if (updates.result) then 1125 | for key, update in pairs(updates.result) do 1126 | parseUpdateCallbacks(update) 1127 | offset = update.update_id + 1 1128 | end 1129 | end 1130 | end 1131 | end 1132 | end 1133 | 1134 | E.run = run 1135 | 1136 | return C 1137 | --------------------------------------------------------------------------------