GithubHelp home page GithubHelp logo

axelksh / clobot Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 32 KB

Clojure library for fast and simple creating Telegram bots

Home Page: https://clojars.org/clobot

License: MIT License

Clojure 100.00%

clobot's Introduction

Clobot

Clojars

Library for fast and simple creating Telegram bots with webhook written in Clojure programming language.

Installation

Leiningen

[clobot "0.1.1"]

Clojure CLI/deps.edn

clobot {:mvn/version "0.1.1"}

Maven

<dependency>
   <groupId>clobot</groupId>
   <artifactId>clobot</artifactId>
   <version>0.1.1</version>
</dependency>

Greadle

compile 'clobot:clobot:0.1.1'

Usage

Full code example

(ns clobot.example
  (:require [clobot.core :as bot]
            [clobot.api :as api]
            [clobot.util :as util]
            [clojure.java.io :as io]))


(def token "PUT:YOUR:TOKEN:HERE")


(defn say-hello
  [msg chat-id]
  (api/text
    "Hi there from the Clojure bot!"
    token chat-id))


(defn send-document
  [msg chat-id]
  (api/document
    (io/file "Document.txt")
    token chat-id))


(defn send-photo
  [msg chat-id]
  (api/photo
    (io/file "Photo.jpg")
    token chat-id))


(defn send-video
  [msg chat-id]
  (api/video
    (io/file "Video.avi")
    token chat-id))


(defn send-audio
  [msg chat-id]
  (api/audio
    (io/file "Audio.mp3")
    token chat-id))


(defn send-sticker
  [msg chat-id]
  (api/sticker
    (io/file "Sticker.png")
    token chat-id))


(defn no-command
  [msg chat-id]
  (api/text
    (format "Sorry:( Command %s is not found..." (util/get-text msg))
    token chat-id))


(def handlers {"hello" say-hello
               "document" send-document
               "photo" send-photo
               "video" send-video
               "audio" send-audio
               "sticker" send-sticker
               "no-command" no-command})


(def config {:token token
             :port  8080})


(defn -main
  []
  (bot/start-bot handlers config))

Explanation

Import namespace

(ns clobot.example
  (:require [clobot.core :as bot]
            [clobot.api :as api]
            [clobot.util :as util]
            [clojure.java.io :as io]))

Create config map with your bot's token and port

(def token "PUT:YOUR:TOKEN:HERE")


(def config {:token token
             :port 8080})

Create handlers for the commands you whant your bot to perform.

;; Sends text message to the chat
(defn say-hello
  [msg chat-id]
  (api/text
    "Hi there from the Clojure bot!"
    token chat-id))


;; Sends document to the chat
(defn document
  [msg chat-id]
  (api/document
    (io/file "Document.txt")
    token chat-id))


;; Sends document to the chat
(defn send-photo
  [msg chat-id]
  (api/photo
    (io/file "Photo.jpg")
    token chat-id))


;; Sends video to the chat
(defn send-video
  [msg chat-id]
  (api/video
    (io/file "Video.avi")
    token chat-id))


;; Sends audio to the chat
(defn send-audio
  [msg chat-id]
  (api/audio
    (io/file "Audio.mp3")
    token chat-id))


;; Sends sticker to the chat
(defn send-sticker
  [msg chat-id]
  (api/sticker
    (io/file "Sticker.png")
    token chat-id))


;; Sends bot's response message in case command user sends is not recognized
;; "no-command" is reserved name for comands error.
;; If not set will be used default handler
(defn no-command
  [msg chat-id]
  (api/text
    (format "Sorry:( Command %s is not found..." (util/get-text msg))
    token chat-id))
    

;; Create map with command as key and handler you whant to handles this command as values
(def handlers {"hello" say-hello
               "document" send-document
               "photo" send-photo
               "video" send-video
               "audio" send-audio
               "sticker" send-sticker
               "no-command" no-command})

Every time a handler is applied chat id and message object Telegram sent to the bot will passes to the handler so you can process it

(defn greeting
  [msg chat-id]
  ...
  ...

Util function to retrieve text from the message object Telegram sent to the bot

(util/get-text msg)

Start bot with the handlers and the config as parpams

(defn -main
  []
  (bot/start-bot handlers config))

Web application will start on given port. For example:

to get brief bot info:

http://localhost:8080/info   

URL for webhook:

http://localhost:8080/  

To get webhhok works properly you have to set the URL for webhook according to the Telegram api

License

Distributed under the MIT License

clobot's People

Contributors

axelksh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.