Skip to content

Commit 75e8b8d

Browse files
committed
v.2.0 Framework extension
- Added Framework extension with callbacks and integrated update handler to help developers get started quicker. - Bugfixes
1 parent 9187e08 commit 75e8b8d

File tree

4 files changed

+348
-15
lines changed

4 files changed

+348
-15
lines changed

README.md

Lines changed: 173 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
# lua-telegram-bot
22
A simple LUA Framework for the [Telegram Bot API](https://https://core.telegram.org/bots/api)
33

4+
Made with ❤️ by [@cosmonawt](https://telegram.me/cosmonawt)
5+
46
## Changelog
57

6-
### Feb 28 2016
8+
### Mar 27 2016 - v2.0
9+
10+
* Added [Framework Extension](https://github.com/cosmonawt/lua-telegram-bot#framework-extension) which includes an internal update and callback handler and several callback functions which can be overridden.
11+
* Added file `bot-example.lua` with examples on how to use the new [Framework Extension](https://github.com/cosmonawt/lua-telegram-bot#framework-extension).
12+
* Minor bug fixes
13+
14+
### Feb 28 2016 v1.1
715

816
* Added `disable_notification` argument to all sending methods to enable [silent messages](https://telegram.org/blog/channels-2-0#silent-messages)
917
* Added `caption` argument to `sendDocument()`
1018

11-
### Jan 22 2016
19+
### Jan 22 2016 - v1.0
1220

13-
* Initial release v0.1-alpha
21+
* Initial release v1.0-alpha
1422

1523

1624
## Installing
@@ -29,13 +37,14 @@ Simply place it in the `lua-telegram-bot` Folder.
2937

3038
To use this module, import it into your bot like this:
3139
```lua
32-
local bot = (require "lua-bot-api").configure(token)
40+
local bot, extension = (require "lua-bot-api").configure(token)
3341
```
3442
Include your bot token as parameter for `configure()`.
3543

3644
At the moment, only getUpdates method (aka polling) is supported, no webhooks.
3745

38-
The `bot` Table exports variables and functions which return the following return values:
46+
The `bot` Table exports variables and functions which return the following return values.
47+
The `extension` Table exports several callback functions as well as an update handler. Check Framework Extension for more information.
3948

4049
### Return values
4150

@@ -44,7 +53,7 @@ This does *not* mean the request was successful, for example in case of a bad `o
4453

4554
A function returns `nil` and an `error description` if it was wrongly called (missing parameters).
4655

47-
### Variables
56+
### Available Variables
4857

4958
```lua
5059
id
@@ -56,7 +65,7 @@ username
5665
first_name
5766
```
5867

59-
### Functions
68+
### Available Functions
6069

6170
```lua
6271
getMe()
@@ -131,3 +140,160 @@ generateForceReply([force_reply] [,selective])
131140
- Generates a `ForceReply` of type `reply_markup` which can be sent optionally in other functions such as `sendMessage()`.
132141
- Forces to reply to the corresponding message from the receivers device.
133142
- `force_reply` can be left out, as it is always `true`.
143+
144+
## Framework Extension
145+
146+
The framework extension was added to help developers focus on the things that actually matter in a bot: It's logic.
147+
It offers serveral callback functions which can be overridden to provide the wanted logic.
148+
149+
### Available Functions
150+
151+
To use the extension, simply add another table variable to the initial `require` call like so:
152+
153+
```lua
154+
local bot, extension = require("lua-bot-api").configure(token)
155+
```
156+
157+
The `extension` Table now stores the following functions:
158+
159+
```lua
160+
run()
161+
```
162+
- Provides an update handler which automatically fetches new updates from the server and calls the respective callback functions.
163+
164+
```lua
165+
onUpdateReceive(update)
166+
```
167+
- Is called every time an update, no matter of what type, is received.
168+
169+
```lua
170+
onMessageReceive(message)
171+
```
172+
- Is called every time a text message is received.
173+
174+
```lua
175+
onPhotoReceive(message)
176+
```
177+
- Is called every time a photo is received.
178+
179+
```lua
180+
onAudioReceive(message)
181+
```
182+
- Is called every time audio is received.
183+
184+
```lua
185+
onDocumentReceive(message)
186+
```
187+
- Is called every time a document is received.
188+
189+
```lua
190+
onStickerReceive(message)
191+
```
192+
- Is called every time a sticker is received.
193+
194+
```lua
195+
onVideoReceive(message)
196+
```
197+
- Is called every time a video is received.
198+
199+
```lua
200+
onVoiceReceive(message)
201+
```
202+
- Is called every time a voice message is received.
203+
204+
```lua
205+
onContactReceive(message)
206+
```
207+
- Is called every time a contact is received.
208+
209+
```lua
210+
onLocationReceive(message)
211+
```
212+
- Is called every time a location is received.
213+
214+
```lua
215+
onLeftChatParticipant(message)
216+
```
217+
- Is called every time a member or the bot itself leaves the chat.
218+
219+
```lua
220+
onNewChatParticipant(message)
221+
```
222+
- Is called when a member joins a chat or the bot itself is added.
223+
224+
```lua
225+
onNewChatTitle(message)
226+
```
227+
- Is called every time the chat title is changed.
228+
229+
```lua
230+
onNewChatPhoto(message)
231+
```
232+
- Is called every time the chat photo is changed.
233+
234+
```lua
235+
onDeleteChatPhoto(message)
236+
```
237+
- Is called every time the chat photo is deleted.
238+
239+
```lua
240+
onGroupChatCreated(message)
241+
```
242+
- Is called every time a group chat is created directly with the bot.
243+
244+
```lua
245+
onSupergroupChatCreated(message)
246+
```
247+
248+
```lua
249+
onChannelChatCreated(message)
250+
```
251+
252+
```lua
253+
onMigrateToChatId(message)
254+
```
255+
- Is called every time a group is upgraded to a supergroup.
256+
257+
```lua
258+
onMigrateFromChatId(message)
259+
```
260+
261+
```lua
262+
onInlineQueryReceive(inlineQuery)
263+
```
264+
- Is called every time an inline query is received.
265+
266+
```lua
267+
onChosenInlineQueryReceive(chosenInlineQuery)
268+
```
269+
- Is called every time a chosen inline query result is received.
270+
271+
```lua
272+
onUnknownTypeReceive(unknownType)
273+
```
274+
- Is called every time when an unknown type is received.
275+
276+
### Using extension functions
277+
278+
In order to provide your own desired behaviour to these callback functions, you need to override them, like so, for example:
279+
280+
```lua
281+
local bot, extension = require("lua-bot-api").configure(token)
282+
283+
extension.onMessageReceive = function (message)
284+
-- Your own desired behaviour here
285+
end
286+
287+
extension.run()
288+
289+
```
290+
291+
You can now use `extension.run()` to use the internal update handler to fetch new updates from the server and call the representive functions.
292+
293+
You can even override `extension.run()` with your own update handler.
294+
295+
See bot-example.lua for some examples on how to use extension functions.
296+
297+
298+
299+

bot-example.lua

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-- pass token as command line argument or insert it into code
2+
local token = arg[1] or ""
3+
4+
-- create and configure new bot with set token
5+
local bot, extension = require("lua-bot-api").configure(token)
6+
7+
-- override onMessageReceive function so it does what we want
8+
extension.onMessageReceive = function (msg)
9+
print("New Message by " .. msg.from.first_name)
10+
11+
if (msg.text == "/start") then
12+
bot.sendMessage(msg.from.id, "Hello there 👋\nMy name is " .. bot.first_name)
13+
elseif (msg.text == "ping") then
14+
bot.sendMessage(msg.chat.id, "pong!")
15+
else
16+
bot.sendMessage(msg.chat.id, "I am just an example, running on the Lua Telegram Framework written with ❤️ by @cosmonawt")
17+
end
18+
end
19+
20+
-- override onPhotoReceive as well
21+
extension.onPhotoReceive = function (msg)
22+
print("Photo received!")
23+
bot.sendMessage(msg.chat.id, "Nice photo! It dimensions are " .. msg.photo[1].width .. "x" .. msg.photo[1].height)
24+
end
25+
26+
-- This runs the internal update and callback handler
27+
-- you can even override run()
28+
extension.run()

lua-bot-api-test.lua

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ for key, query in pairs(updates.result) do
2323
-- get the users profile pictures
2424
local profilePicture = getUserProfilePhotos(query.message.from.id)
2525
-- and send the first one back to him using its file id
26-
sendPhoto(query.message.from.id, profilePicture.result.photos[1][1].file_id)
26+
bot.sendPhoto(query.message.from.id, profilePicture.result.photos[1][1].file_id)
2727
end
2828
end
2929
end

0 commit comments

Comments
 (0)