A chat-bot for Telegram that can add comments to Trello cards, your TickTick todo-list and your Diigo bookmarks, from any Telegram client. (see list of supported commands, below)
This chat-bot consists of a webhook served by Express.js and designed to run on Firebase Functions. It uses Telegram's setWebhook function.
The first version of this bot was developed by following the steps provided in Serverless Telegram Bot with Firebase - Francisco Gutiérrez - Medium.
/openwhyd <youtube_video_url> [description]post a YouTube video on Openwhyd.org
/todo <task> [#tag [#...]]adds a ToDo/task to TickTick's inbox, for sorting/today <task> [#tag [#...]]adds a ToDo/task to TickTick, due today
/tagslists the tags associated to each Trello card/nextlists the nexttaskfor each Trello card/next <task> [#tag]adds ataskto the top of the check-list of the Trello card associated with#tag/note <text> [#card [#...]]adds a comment to the specified Trello card(s), for journaling
/shelf <spotify_album_url>proposes the addition of an album to the adrienjoly/album-shelf GitHub repository (requires options:spotify.clientid,spotify.secretandgithub.tokenwith "public repo" permissions)
/versionreturns the version of the Bot
Notes:
- You can find an up-to-date list of commands in the
commandHandlersconstant defined insrc/messageHandler.ts; - You can find commands that I plan to add later in the ToDo / Next steps section of this page.
To get started, you just need git, NodeJS and to follow these instructions:
$ git clone https://github.com/adrienjoly/telegram-scribe-bot.git
$ cd telegram-scribe-bot
$ nvm use # to use the project's expected Node.js version
$ make install
$ make test # run automated test suitesBefore making your chat-bot accessible through Telegram, you can test it locally:
$ make test-botThis command will start a CLI that will allow you to interact with the bot (e.g. to test the connection with Trello and other services) without having to put it online.
Let's see how to set it up.
Credentials of your services must be provided in a .config.json file, at the root directory of the project.
Initialize it based on the provided template: $ cp .config.example.json .config.json
Trello credentials must be provided in your .config.json file.
- Copy your Trello API Key (from trello.com/app-key) and paste it as the value of the
trello.apikeyvariable, in your.config.jsonfile - Manually generate a Token (a link is provided on trello.com/app-key, below the Trello API Key) and paste it as the value of the
trello.usertokenvariable, still in your.config.jsonfile - Run
$ tools/trello-boards.tsto make sure that these credentials give access to Trello's API and display the list of the Trello boards you have access to - Copy the 24-characters-long identifier of the Trello board that you want your bot to edit, and paste it as the value of the
trello.boardidvariable of your.config.jsonfile
In order to add comments and tasks to your Trello cards, you must associate one or more hashtags to these cards.
How to achieve this?
- Open one of your Trello cards
- In the description of that card, add the following text:
telegram-scribe-bot:addCommentsFromTaggedNotes(#mytag1,mytag2,...), and save your changes - After doing so, you'll be able to add a comment to that card, by sending the following message through your Telegram app:
/note hello world! #mytag1
For instance, if you have a card in which you want to store your #diary notes as comments, add the following line to the description of that card:
telegram-scribe-bot:addCommentsFromTaggedNotes(#diary)
After doing that, sending the following chat message should add a comment to that card:
/note I had a great day today! #diary
Complete your .config.json file by providing the credentials of the services you want the bot to interact with:
- Your Telegram user ID can be retrieved by sending
/startto https://web.telegram.org/k/#@userinfobot - Spotify credentials can be generated from https://developer.spotify.com/dashboard/applications
- GitHub token (with
public_reposcope) can be generated from https://github.com/settings/tokens
Follow these steps to deploy your bot to Firebase and make it accessible through Telegram.
- In your Telegram app, start a conversation with @BotFather
- Write the command
/newBotand follow the provided steps - Initialize the
.envfile (at the root directory of the project), based on the provided template:$ cp .env.example .env - In the
.envfile, replace the defaultBOT_TOKENvalue by the Secret Token provided by that bot - Also, take note of the name of your bot (ends with
bot), we'll need it later
- Go to your firebase console
- Add a new project
- In the
.firebasercfile, replacetelegram-scribe-botby the id of that project
$ make setup-firebase(to login to your Firebase account)$ make deploy-firebase(will upload the source code to your Firebase project)- In the
.envfile, replace the defaultROUTER_URLvalue by the one printed when deploying (previous step), it must end with/router/ $ make test-firebase(to check that the function deployed on Firebase responds)$ make bind-firebase-webhook(to bind that function to your Telegram bot)$ make test-firebase-webhook(to check that the function's router URL was properly bound to your Telegram bot)
After making any change to your bot, don't forget to deploy again it using $ make deploy-firebase.
- In your Telegram app, start a conversation with your bot (e.g. mine is @aj_scribe_bot)
- Send "hello"
- The bot should reply with your name
You can troubleshoot your bot using your firebase console.
Set telegram.onlyfromuserid in your .config.json file and call $ make deploy-firebase again if you want the bot to only respond to that Telegram user identifier.
The steps are listed in the order I usually follow:
-
In the
commandHandlersarray ofsrc/messageHandler.ts, add an entry for your command. At first, make it return a simplestring, like we did for the/versioncommand. Deploy it and test it in production, just to make sure that you won't be blocked later at that critical step. -
Write an automated test in
src/use-cases/, to define the expected reponse for a sample command. (see example) -
Write a minimal
CommandHandler, just to make the test pass, without calling any 3rd-party API yet. (see example) -
Write a small tool to examine the response from the 3rd-party API. (see example)
-
Update the implementation of your
CommandHandler, so it relies on the actual API response. Make sure that the test passes, when you provide your API credentials. (see example) -
Make the automated test mock the API request(s) so that it doesn't require API credentials to run. (see example)
In that step, you can leverage the
⚠ no match for [...]logs displayed when running your test from step 5, in order to know which URL(s) to mock. -
Test your command locally, using
$ make test-bot. -
Deploy and test your command in production, as explained above.
- Make setup easier and faster, e.g. by automatizing some of the steps
- ideas of "command" use cases to implement:
/next [#tag]will list the nexttaskfor each Trello card associated with#tag/search <text> [#tag [#...]]will search occurrences oftextin comments of Trello cards, optionally filtered by#tags/openwhyd <youtube_video_url> [description]will post a YouTube track to Openwhyd.org, withdescription (optional)/issue <repo>will create a github issue on the provided repo/bk <url> [desc] [#tag]>will create a Diigo bookmark to that URL/met "<person name>" [@place] [#tag] [desc]will create or update a Google Contact/convertunits of measure into others (e.g. timezones, sizes, currencies, data formats...)
- ideas of "request" use cases to implement:
- when waking up: invite to keep a note of the dream you were having
- before going to sleep: invite to keep a note of how was your day (i.e. mood) and of what you did that day (i.e. journal), possibly with a photo to illustrate it
- read issues for more.
No. It's actually pretty dumb. Think of it more like a terminal, or like MacOS' Spotlight feature: it uses Telegram as a way to save text to other services, through their API. That's it.
Sending data from a mobile terminal to a server is far from trivial. For instance, your internet connection may be unstable (or unexistent) at the time when you want to save something. In that case, you'd expect your message to be automatically re-sent as soon as your internet connectivity is back. Telegram provides that out of the box! ✨