From 376bd6ab78276fe7e33662bd89cfdb5fac6052b4 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Thu, 10 Mar 2016 14:11:36 +0100 Subject: Basic example query of the Telegram Bot API using Scheme --- bot.scm | 24 ++++++++++++++++++++++++ telebot.scm | 27 +++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 bot.scm create mode 100644 telebot.scm diff --git a/bot.scm b/bot.scm new file mode 100644 index 0000000..62b45a8 --- /dev/null +++ b/bot.scm @@ -0,0 +1,24 @@ +(include "telebot.scm") +(import (prefix telebot telebot:)) + +(define (assure-list value) + (if (list? value) + value + (list))) + +(define (find-pair symbol nodes) + (find (lambda (x) (equal? symbol (car x))) + (filter pair? nodes))) + +(define (resolve-query query tree) + (fold-right (lambda (x y) (find-pair x (assure-list y))) + tree + (reverse query))) + +(define token (car (command-line-arguments))) + +(print (resolve-query (list 'result 'username) + (telebot:get-me token))) + +(pretty-print (resolve-query (list 'result) + (telebot:get-updates token))) diff --git a/telebot.scm b/telebot.scm new file mode 100644 index 0000000..6d6792d --- /dev/null +++ b/telebot.scm @@ -0,0 +1,27 @@ +(module telebot (get-me get-updates send-message) + (import chicken scheme) + (use openssl) + (use http-client) + (use medea) + + (define api-base "https://api.telegram.org/bot") + + (define (get-query-url token method) + (string-append api-base token "/" method)) + + (define (get-me token) + (with-input-from-request (get-query-url token "getMe") + #f + read-json)) + + (define (get-updates token) + (with-input-from-request (get-query-url token "getUpdates") + #f + read-json)) + + (define (send-message token user message) + (with-input-from-request (get-query-url token "sendMessage") + (list (cons 'chat_id user) + (cons 'text message)) + read-json)) +) -- cgit v1.2.3