Skip to content

Commit 0db9662

Browse files
committed
add simple webhook server
1 parent 3d14c0d commit 0db9662

File tree

4 files changed

+64
-3
lines changed

4 files changed

+64
-3
lines changed

examples/webhook_simple.nim

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import ../telebot, asyncdispatch, logging, options, sam
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 updateHandler(b: Telebot, u: Update) {.async.} =
10+
if not u.message.isSome:
11+
return
12+
var response = u.message.get
13+
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)
20+
21+
22+
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)
28+
29+
when isMainModule:
30+
let bot = newTeleBot(API_KEY)
31+
32+
bot.onUpdate(updateHandler)
33+
bot.onCommand("hello", greatingHandler)
34+
35+
bot.startWebhook("rNFRWJqqmxteVtjciNFxuNoznphWzxYFNscicjrYPHVjrnYrFVtdKNkckk3hjAYH", "https://0899ba14.ngrok.io/rNFRWJqqmxteVtjciNFxuNoznphWzxYFNscicjrYPHVjrnYrFVtdKNkckk3hjAYH")

telebot/api.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import httpclient, sam, asyncdispatch, utils, strutils, options, strtabs
2+
from tables import hasKey, `[]`
3+
import types, keyboard
24

35
magic Message:
46
chatId: int64

telebot/keyboard.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import types, sam, strutils, utils, options, tables
1+
import types, strutils, utils, options
22

33
proc initKeyBoardButton*(text: string): KeyboardButton =
44
result.text = text

telebot/webhook.nim

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import asyncdispatch, httpclient, strutils, sam
2-
import utils, types
1+
import asyncdispatch, asynchttpserver, httpclient, strutils, sam, options
2+
import utils, types, api
33

44
type
55
WebhookInfo* = object
@@ -50,3 +50,27 @@ proc getWebhookInfo*(b: TeleBot): Future[WebhookInfo] {.async.} =
5050
END_POINT("getWebhookInfo")
5151
let res = await makeRequest(b, endpoint % b.token)
5252
result = getWebhookInfo(res)
53+
54+
55+
proc startWebhook*(b: Telebot, secret, url: string, port = 8080) =
56+
try:
57+
let me = waitFor b.getMe()
58+
if me.username.isSome:
59+
b.username = me.username.get().toLowerAscii()
60+
except IOError, OSError:
61+
d("Unable to fetch my info ", getCurrentExceptionMsg())
62+
63+
waitFor b.setWebhook(url)
64+
65+
proc callback(req: Request) {.async, gcsafe.} =
66+
if req.url.path == "/" & secret:
67+
let
68+
json = parse(req.body)
69+
update = unmarshal(json, Update)
70+
await b.handleUpdate(update)
71+
await req.respond(Http200, "OK")
72+
else:
73+
await req.respond(Http404, "Not Found")
74+
75+
var server = newAsyncHttpServer()
76+
waitFor server.serve(port=port.Port, callback=callback)

0 commit comments

Comments
 (0)