A Clojure(Script) library for Telegram Bot API.
Add [dev.meinside/clogram "0.25.0"]
to the dependency of your project.clj file.
(require '[meinside.clogram :as cg])
;; generate your bot token with this guide: https://core.telegram.org/bots#3-how-do-i-create-a-bot
(def token "0123456789:abcdefghijklmnopqrstuvwxyz")
;; create a new bot
(def bot (cg/new-bot token
:verbose? true))
;; get updates from your bot
(cg/get-update bot)
;; send 'typing...' to chat id: 123456
(cg/send-chat-action bot 123456 :typing)
;; send a message to chat id: 123456
(cg/send-message bot 123456 "this is a message from bot")
;; clogram-sample/src/core.clj
;;
;; run with: $ lein run -m clogram-sample.core
(ns clogram-sample.core
(:gen-class)
(:require [meinside.clogram :as cg]))
(def token "0123456789:abcdefghijklmnopqrstuvwxyz")
(def interval 1)
(def verbose? false)
;(def verbose? true)
(def my-bot (cg/new-bot token :verbose? verbose?))
(defn echo
"echo function"
[bot update]
(println ">>> received update:" update)
(let [chat-id (get-in update [:message :chat :id])
reply-to (get-in update [:message :message-id])
text (get-in update [:message :text])]
;; 'typing...'
(let [result (cg/send-chat-action bot chat-id :typing)]
(when (not (:ok result))
(println "*** failed to send chat action:" (:reason-pharse result))))
(if (= text "/terminate")
;; process /terminate command
(do
(println ">>> received: /terminate")
(cg/stop-polling-updates bot)) ;; stop polling
;; or other texts
;; and reply to the received message
(let [echoed-text (str "echo: " text)
result (cg/send-message bot chat-id echoed-text
:reply-parameters {"message_id" reply-to})]
(when (not (:ok result))
(println "*** failed to send message:" (:reason-phrase result)))))))
(defn -main
"main function"
[& _]
(println ">>> launching application...")
;; add shutdown hook
(.addShutdownHook (Runtime/getRuntime)
(Thread. #(do
(println ">>> terminating application...")
(cg/stop-polling-updates my-bot)))) ;; stop polling
;; busy-wait for polling
(cg/poll-updates my-bot interval echo))
TODO - Add guides here.
MIT
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close