-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
telebot: use http-easy for http requests
- Loading branch information
Showing
6 changed files
with
42 additions
and
64 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters