Skip to content

Commit

Permalink
Breaking change: get rid of Options. Fix object variants for ChatBoos…
Browse files Browse the repository at this point in the history
…tSource and MessageOrigin
  • Loading branch information
ba0f3 committed Dec 30, 2023
1 parent 76bea3b commit 144b763
Show file tree
Hide file tree
Showing 32 changed files with 862 additions and 823 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ addHandler(L)
const API_KEY = strip(slurp("secret.key"))
proc updateHandler(b: Telebot, u: Update): Future[bool] {.async.} =
if not u.message.isSome:
if u.message.isNil:
# return true will make bot stop process other callbacks
return true
var response = u.message.get
if response.text.isSome:
let text = response.text.get
var response = u.message
if response.text.len > 0:
let text = response.text
discard await b.sendMessage(response.chat.id, text, parseMode = "markdown", disableNotification = true, replyParameters = ReplyParameters(messageId: response.messageId))
let bot = newTeleBot(API_KEY)
Expand Down
22 changes: 12 additions & 10 deletions examples/callback_query.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger()
Expand All @@ -7,21 +7,23 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
if u.message.isSome:
var response = u.message.get
if response.text.isSome:
let text = response.text.get
if u.message != nil:
var response = u.message
if response.text.len > 0:
let text = response.text
var
yes = initInlineKeyboardButton("Yes 🆗", callbackData = "yes " & text)
no = initInlineKeyboardButton("No 🚫", callbackData = "no " & text)
yes = newInlineKeyboardButton("Yes 🆗", callbackData = "yes " & text)
no = newInlineKeyboardButton("No 🚫", callbackData = "no " & text)

let replyMarkup = newInlineKeyboardMarkup(@[yes, no])

echo replyMarkup[]

discard await b.sendMessage(response.chat.id, "Confirm: " & text, replyMarkup = replyMarkup)
elif u.callbackQuery.isSome:
var data = u.callbackQuery.get
elif u.callbackQuery != nil:
var data = u.callbackQuery
try:
discard await b.answerCallbackQuery(data.id, "Response: " & data.data.get, true)
discard await b.answerCallbackQuery(data.id, "Response: " & data.data, true)
except CatchableError:
discard

Expand Down
2 changes: 1 addition & 1 deletion examples/delete_message.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand Down
13 changes: 7 additions & 6 deletions examples/echo_bot.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -7,17 +7,18 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
if u.message.isSome:
var response = u.message.get
if response.text.isSome:
let text = response.text.get
if not u.message.isNil:
var response = u.message
if response.text.len > 0:
let text = response.text
echo text
discard await b.sendMessage(response.chat.id, "text:" & text, parseMode = "markdown", disableNotification = true, replyParameters = ReplyParameters(messageId: response.messageId))
return true


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, replyParameters = ReplyParameters(messageId: c.message.messageId))
if c.message.fromUser != nil:
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.firstname, disableNotification = true, replyParameters = ReplyParameters(messageId: c.message.messageId))
result = true

when isMainModule:
Expand Down
12 changes: 7 additions & 5 deletions examples/echo_bot_with_proxy.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -7,13 +7,15 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

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)
if u.message != nil:
var response = u.message
if response.text.len > 0:
discard await b.sendMessage(response.chat.id, response.text)


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

when isMainModule:
let bot = newTeleBot(API_KEY)
Expand Down
16 changes: 8 additions & 8 deletions examples/file_receive_bot/file_receive_bot.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This Bot receives any Document file, responds on chat with file metadata and file contents.
import telebot, asyncdispatch, options, strformat, httpclient, json
import telebot, asyncdispatch, strformat, httpclient, json
from strutils import strip


Expand All @@ -19,16 +19,16 @@ proc updateHandler(bot: TeleBot, e: Update): Future[bool] {.async.} =
url_getfile = fmt"https://api.telegram.org/bot{API_KEY}/getFile?file_id="
api_file = fmt"https://api.telegram.org/file/bot{API_KEY}/"

if not e.message:
if e.message.isNil:
return true
var response = e.message.get
if response.document.isSome:
var response = e.message
if response.document != nil:
let
document = response.document.get
file_name = document.file_name.get
mime_type = document.mime_type.get
document = response.document
file_name = document.file_name
mime_type = document.mime_type
file_id = document.file_id
file_size = document.file_size.get
file_size = document.file_size
responz_body = await newAsyncHttpClient().getContent(url_getfile & file_id) # file_id > file_path
file_path = parseJson(responz_body)["result"]["file_path"].getStr()
responx = await newAsyncHttpClient().get(api_file & file_path) # file_path > file
Expand Down
4 changes: 2 additions & 2 deletions examples/file_send_bot/file_send_bot.nim
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This Bot Sends itself as Document file, responds on chat with source file to download.
import telebot, asyncdispatch, options, os, strutils
import telebot, asyncdispatch, os, strutils

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


proc updateHandler(bot: TeleBot, e: Update): Future[bool] {.async.} =
let this_file = "file://" & getAppDir() & "/file_send_bot.nim"
discard await bot.sendDocument(e.message.get.chat.id, this_file, caption = this_file)
discard await bot.sendDocument(e.message.chat.id, this_file, caption = this_file)

let bot = newTeleBot(API_KEY)
bot.onUpdate(updateHandler)
Expand Down
8 changes: 4 additions & 4 deletions examples/force_reply.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger()
Expand All @@ -7,10 +7,10 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
var response = u.message.get
if response.text.isSome:
var response = u.message
if response.text.len > 0:
let
text = response.text.get
text = response.text
replyMarkup = newForceReply(true, "Input:")

discard await b.sendMessage(response.chat.id, text, replyMarkup = replyMarkup)
Expand Down
2 changes: 1 addition & 1 deletion examples/geo_location_bot/geo_bot.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, options, strutils
import telebot, asyncdispatch, strutils

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

Expand Down
6 changes: 3 additions & 3 deletions examples/image_bot.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, json, httpclient, logging, options
import telebot, asyncdispatch, json, httpclient, logging
var L = newConsoleLogger()
addHandler(L)

Expand All @@ -20,12 +20,12 @@ proc queryHandler(b: TeleBot, q: InlineQuery): Future[bool] {.async, gcsafe.} =
for child in data["data"]["children"].items:
if child["data"]["thumbnail"].str[0..3] != "http":
continue
var photo: InlineQueryResultPhoto
var photo = new InlineQueryResultPhoto
photo.kind = "photo"
photo.id = child["data"]["id"].str
photo.photoUrl = child["data"]["url"].str
photo.thumbnailUrl = child["data"]["thumbnail"].str
photo.title = some($child["data"]["title"])
photo.title = $child["data"]["title"]

photos.add(photo)
discard await b.answerInlineQuery(q.id, photos)
Expand Down
8 changes: 4 additions & 4 deletions examples/inline_keyboard.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger()
Expand All @@ -7,10 +7,10 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
var response = u.message.get
if response.text.isSome:
var response = u.message
if response.text.len > 0:
let
text = response.text.get
text = response.text
var
google = initInlineKeyboardButton("Google", url = "https://www.google.com/search?q=" & text)
bing = initInlineKeyboardButton("Bing", url = "https://www.bing.com/search?q=" & text)
Expand Down
6 changes: 3 additions & 3 deletions examples/inline_query_with_input_message_content.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, json, httpclient, logging, options
import telebot, asyncdispatch, json, httpclient, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -12,12 +12,12 @@ proc inlineHandler(b: Telebot, u: InlineQuery): Future[bool]{.async.} =
res.kind = "article"
res.title = "тест\""
res.id = "1"
res.inputMessageContent = InputTextMessageContent("test").some
res.inputMessageContent = InputTextMessageContent("test")

var results: seq[InlineQueryResultArticle]
results.add(res)

echo u
echo u[]

discard waitFor b.answerInlineQuery(u.id, results)

Expand Down
4 changes: 2 additions & 2 deletions examples/photo_send_bot/photo_send_bot.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, options, logging, os, strutils
import telebot, asyncdispatch, logging, os, strutils


const API_KEY = slurp("../secret.key").strip()
Expand All @@ -7,7 +7,7 @@ const API_KEY = slurp("../secret.key").strip()
addHandler(newConsoleLogger(fmtStr="$levelname, [$time] "))

proc updateHandler(bot: TeleBot, e: Update): Future[bool] {.async.} =
discard await bot.sendPhoto(e.message.get.chat.id, "file://" & getAppDir() & "/sample.jpg")
discard await bot.sendPhoto(e.message.chat.id, "file://" & getAppDir() & "/sample.jpg")

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,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -8,7 +8,7 @@ const API_KEY = slurp("secret.key").strip()

proc promoteHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
try:
echo waitFor b.promoteChatMember($c.message.chat.id, c.message.fromUser.get().id,
echo waitFor b.promoteChatMember($c.message.chat.id, c.message.fromUser.id,
canChangeInfo=true,
canPostMessages = true,
canEditMessages = true,
Expand Down
4 changes: 2 additions & 2 deletions examples/seamless_login_bot.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -13,7 +13,7 @@ const API_KEY = slurp("secret.key").strip()

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


Expand Down
10 changes: 5 additions & 5 deletions examples/set_chat_permissions.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

import ../src/telebot/private/utils
Expand All @@ -10,10 +10,10 @@ const API_KEY = slurp("secret.key").strip()

proc commandHandler(b: Telebot, c: Command): Future[bool] {.gcsafe, async.} =
let perms = ChatPermissions(
canSendMessages: some(true),
canSendAudios: some(true),
canSendOtherMessages: some(true),
canAddWebPagePreviews: some(true))
canSendMessages: true,
canSendAudios: true,
canSendOtherMessages: true,
canAddWebPagePreviews: true)

var json = ""
marshal(perms, json)
Expand Down
4 changes: 2 additions & 2 deletions examples/set_command_scope.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger(fmtStr="$levelname, [$time] ")
Expand All @@ -7,7 +7,7 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

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, replyParameters = ReplyParameters(messageId: c.message.messageId))
discard b.sendMessage(c.message.chat.id, "hello " & c.message.fromUser.firstname, disableNotification = true, replyParameters = ReplyParameters(messageId: c.message.messageId))
result = true


Expand Down
12 changes: 6 additions & 6 deletions examples/share_contact.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options, strutils
import telebot, asyncdispatch, logging, strutils

#[
this example has been shared by @hamidb80
Expand All @@ -14,14 +14,14 @@ func singleReply*(btn: KeyboardButton): ReplyKeyboardMarkup =
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
if u.message.isSome:
var response = u.message.get
if u.message != nil:
var response = u.message

if issome response.contact:
if response.contact != nil:
# send received contact info
discard await b.sendMessage(
response.chat.id,
$response.contact.get,
$response.contact[],
)

else:
Expand All @@ -30,7 +30,7 @@ proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
response.chat.id,
"check the virtual keyboard",
replyMarkup = singleReply(
KeyboardButton(text: "send your contact", requestContact: some true)
KeyboardButton(text: "send your contact", requestContact: true)
))

return true
Expand Down
10 changes: 5 additions & 5 deletions examples/webapp.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import telebot, asyncdispatch, logging, options
import telebot, asyncdispatch, logging
from strutils import strip

var L = newConsoleLogger()
Expand All @@ -7,10 +7,10 @@ addHandler(L)
const API_KEY = slurp("secret.key").strip()

proc updateHandler(b: Telebot, u: Update): Future[bool] {.gcsafe, async.} =
var response = u.message.get
if response.text.isSome:
let text = response.text.get
var google = KeyboardButton(text: "Search the web", webApp: some WebAppInfo(url: "https://quick-loops-prove-116-110-41-44.loca.lt/webapp.html"))
var response = u.message
if response.text.len > 0:
let text = response.text
var google = KeyboardButton(text: "Search the web", webApp: WebAppInfo(url: "https://quick-loops-prove-116-110-41-44.loca.lt/webapp.html"))

let replyMarkup = ReplyKeyboardMarkup(kind: kReplyKeyboardMarkup, keyboard: @[@[google]])

Expand Down
Loading

0 comments on commit 144b763

Please sign in to comment.