Skip to content

Commit

Permalink
Switch from sam to std/json, refs #67 #68
Browse files Browse the repository at this point in the history
  • Loading branch information
ba0f3 committed Jan 1, 2022
1 parent 4ac945d commit d4be7aa
Show file tree
Hide file tree
Showing 13 changed files with 132 additions and 82 deletions.
5 changes: 3 additions & 2 deletions examples/delete_message.nim
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import ../telebot, asyncdispatch, logging, options, sam
import telebot, asyncdispatch, logging, options
from strutils import strip

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

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

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

when isMainModule:
let bot = newTeleBot(API_KEY)
Expand Down
28 changes: 28 additions & 0 deletions examples/dynamic_commands.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import telebot, asyncdispatch, logging
from strutils import strip

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

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

proc getCommandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
let commands = await b.getMyCommands()
var output = ""
if commands.len == 0:
output = "No command registered yet"
else:
for command in commands:
output.add(command.command & "\t" & command.description & "\n")
discard await b.sendMessage(c.message.chat.id, "```\n" & output & "\n```", parseMode="markdown")
result = true

proc setCommandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
discard await b.sendDice(c.message.chat.id)
result = true

when isMainModule:
let bot = newTeleBot(API_KEY)
bot.onCommand("get", getCommandHandler)
bot.onCommand("set", setCommandHandler)
bot.poll(timeout=300)
4 changes: 2 additions & 2 deletions examples/echo_bot_with_proxy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ addHandler(L)

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

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


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

when isMainModule:
Expand Down
6 changes: 3 additions & 3 deletions examples/inline_keyboard.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ addHandler(L)

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

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

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

discard await bot.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)
discard await b.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)

when isMainModule:
let bot = newTeleBot(API_KEY)
Expand Down
4 changes: 2 additions & 2 deletions examples/promote_chat_member.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import ../telebot, asyncdispatch, logging, options, sam
import telebot, asyncdispatch, logging, options
from strutils import strip

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

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

proc promoteHandler(b: Telebot, c: Command) {.async.} =
proc promoteHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
try:
echo waitFor b.promoteChatMember($c.message.chat.id, c.message.fromUser.get().id,
canChangeInfo=true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const API_KEY = slurp("secret.key").strip()
SEND /setdomain COMMAND TO @BotFatheR
]#

proc loginHandler(b: Telebot, c: Command) {.async.} =
proc loginHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
var loginButton = initInlineKeyBoardButton("Login")
loginButton.loginUrl = some(newLoginUrl("https://huy.im"))
discard await b.sendMessage(c.message.chat.id, "Welcome to Seamless Web Bots", replyMarkup = newInlineKeyboardMarkup(@[loginButton]))
Expand Down
2 changes: 1 addition & 1 deletion examples/send_dice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ addHandler(L)

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

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

when isMainModule:
Expand Down
6 changes: 3 additions & 3 deletions examples/set_chat_permissions.nim
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import ../telebot, asyncdispatch, logging, options, sam
import telebot, asyncdispatch, logging, options
from strutils import strip

import ../telebot/utils
import ../src/telebot/private/utils

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

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

proc commandHandler(b: Telebot, c: Command) {.async.} =
proc commandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
let perms = ChatPermissions(
canSendMessages: some(true),
canSendMediaMessages: some(true),
Expand Down
4 changes: 2 additions & 2 deletions examples/webhook_simple.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options, sam
import telebot, asyncdispatch, logging, options
from strutils import strip

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


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

when isMainModule:
Expand Down
Loading

0 comments on commit d4be7aa

Please sign in to comment.