Skip to content
Bill Howe edited this page Oct 21, 2023 · 10 revisions

Getting Started

Get started using the telegrambot-lib.

Adding Dependencies

See the telegrambot-lib Clojars page for details on adding this library to projects for Leiningen, Boot, and CLI/deps.edn.

You will also need to provide your favorite JSON mapper library as an explicit project dependency or just make sure you already have one in the classpath of your project (which is usually the case when you are building a web app).

The following JSON mappers are currently supported:

  • ["cheshire"]
  • ["metosin/jsonista"]
  • ["org.clojure/data.json"]

So, with Leiningen you'll end up with these two entries in the project dependencies:

[telegrambot-lib "2.10.0"]
[cheshire "5.10.1"]
  • Note: The above version numbers are an example and may not be the latest. See each project's github or clojars page.

Requiring the Library

In the REPL:

(require '[telegrambot-lib.core :as tbot])

In your application codebase:

(ns my-app.core
  (:require [telegrambot-lib.core :as tbot]))

Using the Library

Pre-Reqs:

  • You have created a Telegram bot and received an auth token.
  • The library is added as a project dependency.
  • The library is required as indicated above, with a tbot alias for the core namespace.
    • Note: The tbot alias is only important for following the example code in the wiki.

The Bot API auth token can be passed to the bot by either:

  • Environment variable — The lib will look for BOT_TOKEN in the environment.
    The token could be in project.clj, profiles.clj or an environment variable.
    See environ for more details.

    (def mybot (tbot/create))

or

  • Function argument - simple value — The create function has an optional parameter to pass an auth token.

    (def mybot (tbot/create my-token))

or

  • Function argument - map — The create function has an optional parameter to pass a map of config values.

    The advantage of using a map is the ability to change other configuration such as async or the api endpoint url.

    (def mybot (tbot/create {:bot-token my-token}))

Verify your bot is working by firing the getMe Bot API method call, which is now as simple as calling the get-me function.

(tbot/get-me mybot)
=>
{:ok true,
 :result {:id 123,
          :is_bot true,
          :first_name "mybot",
          :username "my_roboto",
          :can_join_groups true,
          :can_read_all_group_messages false,
          :supports_inline_queries false}}
Clone this wiki locally