aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2016-03-13 12:26:15 +0100
committerAdrian Kummerlaender2016-03-13 12:26:15 +0100
commitcbe9488ff46b05f196708c25d15bfa3c0b5ab0a0 (patch)
tree4d38210edd1aa356e1cc77889853e42ef017dbf8
parent5f641ea6e243dc070e3e041d513a9d02996337c2 (diff)
downloadTelebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar.gz
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar.bz2
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar.lz
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar.xz
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.tar.zst
Telebot-cbe9488ff46b05f196708c25d15bfa3c0b5ab0a0.zip
Move basic update polling into `pollUpdates` framework function
-rw-r--r--bot.scm21
-rw-r--r--telebot.scm18
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))))))
)