Skip to content

Commit d4be7aa

Browse files
committed
Switch from sam to std/json, refs #67 #68
1 parent 4ac945d commit d4be7aa

13 files changed

+132
-82
lines changed

examples/delete_message.nim

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

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
55
addHandler(L)
66

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

9-
proc commandHandler(b: Telebot, c: Command) {.async.} =
9+
proc commandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1010
discard await b.deleteMessage($c.message.chat.id, c.message.messageId)
11+
result = true
1112

1213
when isMainModule:
1314
let bot = newTeleBot(API_KEY)

examples/dynamic_commands.nim

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import telebot, asyncdispatch, logging
2+
from strutils import strip
3+
4+
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
5+
addHandler(L)
6+
7+
const API_KEY = slurp("secret.key").strip()
8+
9+
proc getCommandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
10+
let commands = await b.getMyCommands()
11+
var output = ""
12+
if commands.len == 0:
13+
output = "No command registered yet"
14+
else:
15+
for command in commands:
16+
output.add(command.command & "\t" & command.description & "\n")
17+
discard await b.sendMessage(c.message.chat.id, "```\n" & output & "\n```", parseMode="markdown")
18+
result = true
19+
20+
proc setCommandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
21+
discard await b.sendDice(c.message.chat.id)
22+
result = true
23+
24+
when isMainModule:
25+
let bot = newTeleBot(API_KEY)
26+
bot.onCommand("get", getCommandHandler)
27+
bot.onCommand("set", setCommandHandler)
28+
bot.poll(timeout=300)

examples/echo_bot_with_proxy.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ addHandler(L)
66

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

9-
proc updateHandler(b: Telebot, u: Update) {.async.} =
9+
proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
1010
var response = u.message.get
1111
if response.text.isSome:
1212
discard await b.sendMessage(response.chat.id, response.text.get)
1313

1414

15-
proc greatingHandler(b: Telebot, c: Command) {.async.} =
15+
proc greatingHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1616
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName)
1717

1818
when isMainModule:

examples/inline_keyboard.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ addHandler(L)
66

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

9-
proc updateHandler(bot: TeleBot, e: Update) {.async.} =
10-
var response = e.message.get
9+
proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
10+
var response = u.message.get
1111
if response.text.isSome:
1212
let
1313
text = response.text.get
@@ -24,7 +24,7 @@ proc updateHandler(bot: TeleBot, e: Update) {.async.} =
2424

2525
let replyMarkup = newInlineKeyboardMarkup(@[google, bing], @[ddg, searx])
2626

27-
discard await bot.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)
27+
discard await b.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)
2828

2929
when isMainModule:
3030
let bot = newTeleBot(API_KEY)

examples/promote_chat_member.nim

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

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
55
addHandler(L)
66

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

9-
proc promoteHandler(b: Telebot, c: Command) {.async.} =
9+
proc promoteHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1010
try:
1111
echo waitFor b.promoteChatMember($c.message.chat.id, c.message.fromUser.get().id,
1212
canChangeInfo=true,

examples/seamless_bot.nim renamed to examples/seamless_login_bot.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const API_KEY = slurp("secret.key").strip()
1111
SEND /setdomain COMMAND TO @BotFatheR
1212
]#
1313

14-
proc loginHandler(b: Telebot, c: Command) {.async.} =
14+
proc loginHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1515
var loginButton = initInlineKeyBoardButton("Login")
1616
loginButton.loginUrl = some(newLoginUrl("https://huy.im"))
1717
discard await b.sendMessage(c.message.chat.id, "Welcome to Seamless Web Bots", replyMarkup = newInlineKeyboardMarkup(@[loginButton]))

examples/send_dice.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ addHandler(L)
66

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

9-
proc commandHandler(b: Telebot, c: Command): Future[bool] {.async.} =
9+
proc commandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1010
discard await b.sendDice(c.message.chat.id)
1111

1212
when isMainModule:

examples/set_chat_permissions.nim

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

4-
import ../telebot/utils
4+
import ../src/telebot/private/utils
55

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

99
const API_KEY = slurp("secret.key").strip()
1010

11-
proc commandHandler(b: Telebot, c: Command) {.async.} =
11+
proc commandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1212
let perms = ChatPermissions(
1313
canSendMessages: some(true),
1414
canSendMediaMessages: some(true),

examples/webhook_simple.nim

Lines changed: 2 additions & 2 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
22
from strutils import strip
33

44
var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
@@ -14,7 +14,7 @@ proc updateHandler(b: Telebot, u: Update) {.async.} =
1414
discard b.sendMessage(response.chat.id, response.text.get, disableNotification = true, replyToMessageId = response.messageId, parseMode = "markdown")
1515

1616

17-
proc greatingHandler(b: Telebot, c: Command) {.async.} =
17+
proc greatingHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
1818
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.get().firstName, disableNotification = true, replyToMessageId = c.message.messageId, parseMode = "markdown")
1919

2020
when isMainModule:

0 commit comments

Comments
 (0)