From cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 13 Mar 2016 12:26:15 +0100 Subject: Move basic update polling into `pollUpdates` framework function --- bot.scm | 21 ++++----------------- telebot.scm | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/bot.scm b/bot.scm index a6d34cc..79b1b6e 100644 --- a/bot.scm +++ b/bot.scm @@ -1,8 +1,6 @@ (include "telebot.scm") (import (prefix telebot telebot:)) -(use loops) -(use vector-lib) (use data-structures) (define (resolve-query query tree) @@ -10,11 +8,6 @@ tree query)) -(define (updates-for-each func updates) - (vector-for-each (lambda (i u) (func u)) - updates)) - -(define offset 0) (define token (car (command-line-arguments))) (define (print-message msg) @@ -32,13 +25,7 @@ chat_id: chat_id text: text))) -(do-forever - (updates-for-each (lambda (u) - (begin (print-message u) - (echo-message u) - (set! offset - (+ 1 (resolve-query '(update_id) u))))) - (resolve-query '(result) - (telebot:getUpdates token - offset: offset - timeout: 60)))) +(telebot:pollUpdates token + (lambda (u) + (begin (print-message u) + (echo-message u)))) diff --git a/telebot.scm b/telebot.scm index 7453cb1..d419c79 100644 --- a/telebot.scm +++ b/telebot.scm @@ -4,12 +4,15 @@ sendLocation sendChatAction forwardMessage - getUserProfilePhotos) + getUserProfilePhotos + pollUpdates) (import chicken scheme) (use srfi-1) (use openssl) (use http-client) (use medea) + (use loops) + (use vector-lib) (define-constant api-base "https://api.telegram.org/bot") @@ -70,4 +73,17 @@ (wrap-api-method getUserProfilePhotos(user_id offset limit)) + + ;;; framework + + (define (pollUpdates token handler) + (define offset 0) + (define process (lambda (i u) + (begin (handler u) + (set! offset (+ 1 (cdr (assv 'update_id u))))))) + (do-forever + (vector-for-each process + (cdr (assv 'result (getUpdates token + offset: offset + timeout: 60)))))) ) -- cgit v1.2.3