Skip to content

Commit

Permalink
telebot: use http-easy for http requests
Browse files Browse the repository at this point in the history
  • Loading branch information
yfzhe committed Jan 14, 2024
1 parent dcbe7da commit 0e07698
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 64 deletions.
52 changes: 0 additions & 52 deletions telebot/bot.rkt

This file was deleted.

6 changes: 4 additions & 2 deletions telebot/info.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

(define collection "telebot")

(define deps '("base"))
(define deps '("base"
"http-easy-lib"
"threading-lib"))
(define build-deps '("scribble-lib" "racket-doc" "rackunit-lib"))

;; (define scribblings '(("scribblings/telebot.scrbl" ())))

(define pkg-desc "Telegram bot API")
(define version "0.1")
(define version "0.2")

(define pkg-authors '(yfzhe))
(define license '(Apache-2.0 OR MIT))
18 changes: 9 additions & 9 deletions telebot/main.rkt
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
#lang racket/base
(require "bot.rkt"
"error.rkt"
(require "private/bot.rkt"
"private/error.rkt"
"schema.rkt"
"api.rkt")

(provide (all-from-out "bot.rkt")
(provide bot? make-bot
exn:fail:bot? exn:fail:bot:api?
(all-from-out "api.rkt")
ref :
bot-get-updates
bot-set-webhook
bot-start/poll)

(define (bot-get-updates bot [offset 0])
(bot-get bot "/getUpdates"
`((offset . ,(number->string offset)))))
(bot-post bot "/getUpdates"
(hasheq 'offset (number->string offset))))

(define (bot-set-webhook bot webhook-url)
;; TODO: getWebhookInfo and compare to the target
(bot-post bot "/setWebhook"
`((url . ,webhook-url))))
(hasheq 'url webhook-url)))

(define (bot-start/poll bot handle-update)
(let loop ([offset 0] [updates '()])
(cond
[(null? updates)
(loop offset (bot-get-updates bot offset))]
[else
(define update (car updates))
(define update (jsexpr->update (car updates)))
(define resp (handle-update update))
(bot-send-message bot resp)
(loop (add1 (hash-ref update 'update_id)) (cdr updates))])))
(loop (add1 (update-id update)) (cdr updates))])))
28 changes: 28 additions & 0 deletions telebot/private/bot.rkt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#lang racket/base
(require net/http-easy
"error.rkt")

(provide bot? make-bot
bot-post)

(struct bot (token))

(define (make-bot token)
(bot token))

(define *tg-api-base* "https://api.telegram.org/bot")

(define (make-api-url bot method)
(string-append *tg-api-base*
(bot-token bot)
method))

(define (bot-post bot method [params (make-hash)])
(define resp
(post (make-api-url bot method) #:json params))
(define json (response-json resp))
(if (hash-ref json 'ok)
(hash-ref json 'result)
(raise-bot-api-error method
(hash-ref json 'description)
(hash-ref json 'error_code))))
File renamed without changes.
2 changes: 1 addition & 1 deletion telebot/schema.rkt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#lang racket/base
(require "bot.rkt"
(require "private/bot.rkt"
(for-syntax racket/base
syntax/parse
racket/syntax
Expand Down

0 comments on commit 0e07698

Please sign in to comment.