You would like to create a telegram bot, that can be invited to channels and chats and can search and answer something for you … This post is for you.
The plan of today is to get through all the steps to get this working properly by yourself.
This bot will be done using a webhook, which is a little bit harder to setup than regular polling due to its use of https, so yes this will be covered as well.
Ask the telegram botfather to create a new bot for you.
This is done from telegram itself. And those are the steps:
- send a message to botfather @BotFather
- check help with /help
- create new bot with /newbot
Give it a name, should end in bot, so you can
Your bot can now accept payments for goods and services via Telegram. 🙂
set up the bot to use a certificate
The original morse library does not support setting self-signed certificates to telegram, so we will do that using curl. Basically a simple http request to the telegram api of your bot, to let it know your custom certificate when doing the SSL handshake. Token is the token received when you have setup your own bot with botfather.
bot settings: join groups and settings
Two more settings need to be done with the botfather, so your bot is available for groups, and can also read messages.
First the join group settings:
Second the privacy settings:
Create a new Clojure project template with Leiningen
Leiningen will be used to generate the clojure project template:
The rest is fun coding…
update the project.clj file
Your bot will be using a set of common and not-so common Clojure libraries. In the original example, the morse library was used for handlers, but it does not
write some clojure code using morse
The bot will simply talk about the current time, when a message is sent to him, either from a direct chat, or from a channel; indeed the bot can also be added to a channel and read channel messages with the settings you have done before.
Settings are kept in a separate file, settings.clj, so you can customize important details outside the app.
Those settings will be slurped after from the main Clojure namespace. Simple sending of text can be see using the morse library, where the token is your bot’s token, and the chatid can be retrieved from the message, (or some previous chats…)
Routing is done through some simple compojure routes, where mostly only the body of the request is of interest in this example:
For convenience json messages are wrapped for you to clojure key-ified collections using wrap-json-body:
The full Clojure code listing is shown below:
run the bot
Running the bot, can be done with the run command of leiningen:
or obviously through the uberjar path.
telegram message format
Message coming to a private chat have the json format below, the top interesting element is message
While message coming through a channel are slightly different, with the top interesting element being channel_post
A mention of the bot’s name in the message has one more extra field:
replay messages with curl
Messages can be replayed using standard curl on your https handler. Note the usage of the curl parameter -k so that curl acknowledge your self-signed certificate. You can of course remove it if you have real ones.