Skip to content

Commit d52e49f

Browse files
committed
better way (maybe) to send message and other documemts
1 parent 6ead699 commit d52e49f

23 files changed

+630
-259
lines changed

README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# telebot.nim
22
Async Telegram Bot API Client implement in @Nim-Lang
33

4+
Notes
5+
=====
6+
From version 1.0.0, procs likes `newMessage`, `newPhoto`,.. are gone, use `sendMessage`, `sendDocument` instead.
7+
If you still prefer the old/ugly way to send stuffs, just import `telebot/compat` for backward compatible
8+
49
Installation
510
============
611
```
@@ -13,23 +18,23 @@ Usage
1318
## echo bot
1419
```nim
1520
import telebot, asyncdispatch, logging, options
21+
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
22+
addHandler(L)
1623
1724
const API_KEY = slurp("secret.key")
1825
1926
proc updateHandler(b: Telebot, u: Update) {.async.} =
27+
if not u.message.isSome:
28+
return
2029
var response = u.message.get
2130
if response.text.isSome:
22-
let
23-
text = response.text.get
24-
var message = newMessage(response.chat.id, text)
25-
message.disableNotification = true
26-
message.replyToMessageId = response.messageId
27-
message.parseMode = "markdown"
28-
discard await b.send(message)
31+
let text = response.text.get
32+
discard await b.sendMessage(response.chat.id, text, parseMode = "markdown", disableNotification = true, replyToMessageId = response.messageId)
2933
3034
let bot = newTeleBot(API_KEY)
3135
bot.onUpdate(updateHandler)
3236
bot.poll(timeout=300)
37+
3338
```
3439

3540
## send local photo
@@ -39,15 +44,9 @@ import telebot, asyncdispatch, options, logging
3944
const API_KEY = slurp("secret.key")
4045
4146
proc updateHandler(bot: TeleBot, update: Update): UpdateCallback =
42-
var response = update.message.get
43-
if response.text.isSome:
44-
let
45-
text = response.text.get
46-
var message = newPhoto(response.chat.id, "file:///path/to/photo.jpg")
47-
discard await bot.send(message)
47+
discard await bot.sendPhoto(response.chat.id, "file:///path/to/photo.jpg")
4848
49-
let
50-
bot = newTeleBot(API_KEY)
49+
let bot = newTeleBot(API_KEY)
5150
bot.onUpdate(updateHandler)
5251
bot.poll(timeout=300)
5352
```

examples/config.nims

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
switch("path", "../src")

examples/echo_bot.nim

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ../telebot, asyncdispatch, logging, options
1+
import telebot, asyncdispatch, logging, options
22
from strutils import strip
33

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
@@ -12,11 +12,7 @@ proc updateHandler(b: Telebot, u: Update) {.async.} =
1212
var response = u.message.get
1313
if response.text.isSome:
1414
let text = response.text.get
15-
var message = newMessage(response.chat.id, text)
16-
message.disableNotification = true
17-
message.replyToMessageId = response.messageId
18-
message.parseMode = "markdown"
19-
discard await b.send(message)
15+
discard await b.sendMessage(response.chat.id, text, parseMode = "markdown", disableNotification = true, replyToMessageId = response.messageId)
2016

2117

2218
proc greatingHandler(b: Telebot, c: Command) {.async.} =

examples/echo_bot_with_proxy.nim

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ../telebot, asyncdispatch, logging, options
1+
import telebot, asyncdispatch, logging, options
22
from strutils import strip
33

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
@@ -9,21 +9,11 @@ const API_KEY = slurp("secret.key").strip()
99
proc updateHandler(b: Telebot, u: Update) {.async.} =
1010
var response = u.message.get
1111
if response.text.isSome:
12-
let
13-
text = response.text.get
14-
var message = newMessage(response.chat.id, text)
15-
message.disableNotification = true
16-
message.replyToMessageId = response.messageId
17-
message.parseMode = "markdown"
18-
discard await b.send(message)
12+
discard await b.sendMessage(response.chat.id, response.text.get)
1913

2014

2115
proc greatingHandler(b: Telebot, c: Command) {.async.} =
22-
var message = newMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName)
23-
message.disableNotification = true
24-
message.replyToMessageId = c.message.messageId
25-
message.parseMode = "markdown"
26-
discard b.send(message)
16+
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName)
2717

2818
when isMainModule:
2919
let bot = newTeleBot(API_KEY)

examples/file_receive_bot/file_receive_bot.nim

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,9 @@ proc updateHandler(bot: TeleBot, e: Update) {.async.} =
2323
responx = await newAsyncHttpClient().get(api_file & file_path) # file_path > file
2424
file_content = await responx.body
2525
msg0_text = fmt"file_name: {file_name}, mime_type: {mime_type}, file_id: {file_id}, file_size: {file_size}, file_path: {file_path}"
26-
var
27-
message0 = newMessage(response.chat.id, msg0_text) # metadata
28-
message1 = newMessage(response.chat.id, file_content) # file contents
29-
discard await bot.send(message0)
30-
discard await bot.send(message1)
26+
27+
discard await bot.sendMessage(response.chat.id, msg0_text)
28+
discard await bot.sendMessage(response.chat.id, file_content)
3129

3230
let bot = newTeleBot(API_KEY)
3331

examples/file_send_bot/file_send_bot.nim

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# This Bot Sends itself as Document file, responds on chat with source file to download.
22
import telebot, asyncdispatch, options, os
33

4-
const API_KEY = slurp("secret.key")
4+
const API_KEY = slurp("../secret.key")
55

66

77
proc updateHandler(bot: TeleBot, e: Update) {.async.} =
8-
let this_file = "file://" & getCurrentDir() & "/file_send_bot.nim"
9-
10-
var document = newDocument(e.message.get.chat.id, this_file)
11-
document.caption = this_file
12-
discard await bot.send(document)
8+
let this_file = "file://" & getAppDir() & "/file_send_bot.nim"
9+
discard await bot.sendDocument(e.message.get.chat.id, this_file, caption = this_file)
1310

1411
let bot = newTeleBot(API_KEY)
1512
bot.onUpdate(updateHandler)

examples/geo_location_bot/geo_bot.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import telebot, asyncdispatch, options
22

3-
const API_KEY = slurp("secret.key")
3+
const API_KEY = slurp("../secret.key")
44

55
proc geoHandler(bot: TeleBot, e: Command) {.async.} =
6-
let message = newLocation(e.message.chat.id, longitude=42.0, latitude=42.0)
7-
discard await bot.send(message)
6+
discard await bot.sendLocation(e.message.chat.id, longitude=42.0, latitude=42.0)
87

98
let bot = newTeleBot(API_KEY)
109
bot.onCommand("geo", geoHandler) # Use /geo on Telegram chat to trigger.

examples/inline_keyboard.nim

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
1-
import ../telebot, asyncdispatch, logging, options
1+
import telebot, asyncdispatch, logging, options
22
from strutils import strip
33

44
var L = newConsoleLogger()
55
addHandler(L)
66

77
const API_KEY = slurp("secret.key").strip()
88

9-
var updates: seq[Update]
10-
11-
129
proc updateHandler(bot: TeleBot, e: Update) {.async.} =
1310
var response = e.message.get
1411
if response.text.isSome:
1512
let
1613
text = response.text.get
1714
var
18-
message = newMessage(response.chat.id, text)
1915
google = initInlineKeyboardButton("Google")
2016
bing = initInlineKeyboardButton("Bing")
2117
ddg = initInlineKeyboardButton("DuckDuckGo")
@@ -26,8 +22,9 @@ proc updateHandler(bot: TeleBot, e: Update) {.async.} =
2622
ddg.url = some("https://duckduckgo.com/?q=" & text)
2723
searx.url = some("https://searx.me/?q=" & text)
2824

29-
message.replyMarkup = newInlineKeyboardMarkup(@[google, bing], @[ddg, searx])
30-
discard await bot.send(message)
25+
let replyMarkup = newInlineKeyboardMarkup(@[google, bing], @[ddg, searx])
26+
27+
discard await bot.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)
3128

3229
when isMainModule:
3330
let bot = newTeleBot(API_KEY)

examples/photo_send_bot/photo_send_bot.nim

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import telebot, asyncdispatch, options, logging, os
22

33

4-
const API_KEY = slurp("secret.key")
4+
const API_KEY = slurp("../secret.key")
55

66

77
addHandler(newConsoleLogger(fmtStr="$levelname, [$time] "))
88

99
proc updateHandler(bot: TeleBot, e: Update) {.async.} =
10-
let message = newPhoto(e.message.get.chat.id, "file://" & getCurrentDir() & "/sample.jpg")
11-
discard await bot.send(message)
10+
discard await bot.sendPhoto(e.message.get.chat.id, "file://" & getAppDir() & "/sample.jpg")
1211

1312
let bot = newTeleBot(API_KEY)
1413

examples/seamless_bot.nim

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ../telebot, asyncdispatch, logging, options, sam, telebot/utils
1+
import telebot, asyncdispatch, logging, options
22
from strutils import strip
33

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
@@ -12,16 +12,13 @@ const API_KEY = slurp("secret.key").strip()
1212
]#
1313

1414
proc loginHandler(b: Telebot, c: Command) {.async.} =
15-
var
16-
message = newMessage(c.message.chat.id, "Welcome to Seamless Web Bots")
17-
loginButton = initInlineKeyBoardButton("Login")
18-
loginButton.loginUrl = some(newLoginUrl("https://huy.im"))
19-
message.replyMarkup = newInlineKeyboardMarkup(@[loginButton])
20-
discard await b.send(message)
15+
var loginButton = initInlineKeyBoardButton("Login")
16+
loginButton.loginUrl = some(newLoginUrl("https://huy.im"))
17+
discard await b.sendMessage(c.message.chat.id, "Welcome to Seamless Web Bots", replyMarkup = newInlineKeyboardMarkup(@[loginButton]))
2118

2219

2320
when isMainModule:
24-
let bot = newTeleBot(API_KEY)
21+
let bot = newTeleBot(API_KEY)
2522

26-
bot.onCommand("login", loginHandler)
27-
bot.poll(timeout=300)
23+
bot.onCommand("login", loginHandler)
24+
bot.poll(timeout=300)

examples/set_chat_permissions.nim

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ../telebot, asyncdispatch, logging, options, sam
2+
from strutils import strip
3+
4+
import ../telebot/utils
5+
6+
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
7+
addHandler(L)
8+
9+
const API_KEY = slurp("secret.key").strip()
10+
11+
proc commandHandler(b: Telebot, c: Command) {.async.} =
12+
let perms = ChatPermissions(
13+
canSendMessages: some(true),
14+
canSendMediaMessages: some(true),
15+
canSendOtherMessages: some(true),
16+
canAddWebPagePreviews: some(true))
17+
18+
var json = ""
19+
marshal(perms, json)
20+
echo json
21+
discard await restrictChatMember(b, $c.message.chat.id, 50535480, perms)
22+
23+
discard await getChatMember(b, $c.message.chat.id, 50535480)
24+
25+
26+
when isMainModule:
27+
let bot = newTeleBot(API_KEY)
28+
bot.onCommand("perms", commandHandler)
29+
bot.poll(timeout=300)

examples/webhook_simple.nim

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import ../telebot, asyncdispatch, logging, options, sam
1+
import telebot, asyncdispatch, logging, options, sam
22
from strutils import strip
33

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
@@ -11,20 +11,11 @@ proc updateHandler(b: Telebot, u: Update) {.async.} =
1111
return
1212
var response = u.message.get
1313
if response.text.isSome:
14-
let text = response.text.get
15-
var message = newMessage(response.chat.id, text)
16-
message.disableNotification = true
17-
message.replyToMessageId = response.messageId
18-
message.parseMode = "markdown"
19-
discard await b.send(message)
14+
discard b.sendMessage(response.chat.id, response.text.get, disableNotification = true, replyToMessageId = response.messageId, parseMode = "markdown")
2015

2116

2217
proc greatingHandler(b: Telebot, c: Command) {.async.} =
23-
var message = newMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName)
24-
message.disableNotification = true
25-
message.replyToMessageId = c.message.messageId
26-
message.parseMode = "markdown"
27-
discard b.send(message)
18+
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName, disableNotification = true, replyToMessageId = c.message.messageId, parseMode = "markdown")
2819

2920
when isMainModule:
3021
let bot = newTeleBot(API_KEY)

telebot.nim renamed to src/telebot.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import tables, httpclient
22

3-
import telebot/[types, keyboard, webhook, inputmedia, helpers]
3+
import telebot/private/[types, keyboard, webhook, inputmedia, helpers]
44
export types, webhook, keyboard, inputmedia, helpers
55

66
proc setProxy*(b: Telebot, url: string, auth = "") {.inline.} =
@@ -12,5 +12,5 @@ proc newTeleBot*(token: string): TeleBot =
1212
result.token = token
1313
result.commandCallbacks = newTable[string, seq[CommandCallback]]()
1414

15-
include telebot/api
16-
include telebot/events
15+
include telebot/private/api
16+
include telebot/private/events

0 commit comments

Comments
 (0)