From 2084a1a465e5986b87e0937f1bef70ceec806360 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 10 Mar 2016 22:09:42 +0100 Subject: Implement macro to automatically generate the basic API wrapper --- bot.scm | 2 +- telebot.scm | 87 ++++++++++++++++++++++++------------------------------------- 2 files changed, 35 insertions(+), 54 deletions(-) diff --git a/bot.scm b/bot.scm index 1bc357b..1484fbc 100644 --- a/bot.scm +++ b/bot.scm @@ -31,4 +31,4 @@ (cdr (resolve-query (list 'update_id) u)) ")")) (resolve-query (list 'result) - (telebot:get-updates token))) + (telebot:getUpdates token))) diff --git a/telebot.scm b/telebot.scm index 8d30013..bd0221a 100644 --- a/telebot.scm +++ b/telebot.scm @@ -1,7 +1,7 @@ -(module telebot (get-me - get-updates - send-message - forward-message) +(module telebot (getMe + getUpdates + sendMessage + forwardMessage) (import chicken scheme) (use srfi-1) (use openssl) @@ -24,53 +24,34 @@ ;;; plain API wrappers, returning deserialized JSON - (define (get-me token) - (with-input-from-request (get-query-url token "getMe") - #f - read-json)) - - (define (get-updates token - #!key offset - limit - timeout) - (with-input-from-request - (get-query-url token "getUpdates") - (clean-query-parameters - (list (cons 'offset offset) - (cons 'limit limit) - (cons 'timeout timeout))) - read-json)) - - (define (send-message token - #!key chat-id - text - parse-mode - disable-web-page-preview - disable-notification - reply-to-message-id - reply-markup) - (with-input-from-request - (get-query-url token "sendMessage") - (clean-query-parameters - (list (cons 'chat_id chat-id) - (cons 'text text) - (cons 'parse_mode parse-mode) - (cons 'disable_web_page_preview disable-web-page-preview) - (cons 'disable_notification disable-notification) - (cons 'reply_to_message_id reply-to-message-id) - (cons 'reply_markup reply-markup))) - read-json)) - - (define (forward-message token - #!key chat-id - from-chat-id - message-id - disable-notification) - (with-input-from-request - (get-query-url token "forwardMessage") - (list (cons 'chat_id chat-id) - (cons 'from_chat_id from-chat-id) - (cons 'message_id message-id) - (cons 'disable_notification disable-notification)) - read-json)) + (define-syntax wrap-api-method + (syntax-rules () + ((wrap-api-method method(parameters ...)) + (define (method + token + #!key parameters ...) + (with-input-from-request + (get-query-url token (symbol->string 'method)) + (clean-query-parameters + (map (lambda (l) (cons (first l) (second l))) + (zip '(parameters ...) + (list parameters ...)))) + read-json))))) + + (wrap-api-method getMe()) + + (wrap-api-method getUpdates(offset limit timeout)) + + (wrap-api-method sendMessage(chat_id + text + parse_mode + disable_web_page_preview + disable_notification + reply_to_message_id + reply_markup)) + + (wrap-api-method forwardMessage(chat_id + from_chat_id + message_id + disable_notification)) ) -- cgit v1.2.3