Git Product home page Git Product logo

londibot-prototype's Introduction

LondiBot

Telegram/Slack bot which reports the status of London TFL services.

This project was started as an exercise to get in touch with Clojure and its ecosystem.

Live demo

If you want to see how the bot works in Slack, simply add it to your workspace: link. A live version has been deployed to Heroku – be aware that it is using free dynos, so it make take time to awake it.

Getting started

Disclaimer: Before continuing with the below instructions I assume you are aquainted with either the Slack API or the Telegram API in order to develop bots. If that is not the case, please make sure you are familiar with them.

First make sure you have leiningen installed, otherwise visit leiningen.org.

Secondly, in order to be able to run any of the bots you will need a PostgreSQL database which contains a jobs table. To install PostgreSQL visit https://www.postgresql.org/download/, and to replicate the jobs table, take the following script:

CREATE TABLE jobs (
  id serial PRIMARY KEY, 
  userId VARCHAR(50), 
  cronExpression VARCHAR(50), 
  service VARCHAR(50), 
  submittedDate TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

To clone the database for the testing environment you might find this script handy:

CREATE DATABASE londibot_test WITH TEMPLATE londibot OWNER your_user;

You can name the database as you wish, but make sure that the connection strings are properly configured in the :profiles section of leiningen's project map (project.clj). In this repository the DB's are set as londibot and londibot_test.

Lastly, once you have the database set up and leiningen install, simply run:

export TELEGRAM_TOKEN=<your_token>
lein telegram

or, in case you prefer to run the Slack bot:

export SLACK_TOKEN=<your_token>
lein slack

And that should get you started.

Explanation

As you can see in the code, this repository has 3 main sections: the first one is the core functionality to interact with the TFL API. It is a set of Clojure functions which retrieve, parse and expose the status of the tube in London via the TFL API and also allow the application to save scheduled notifications.

The scheduled notifications or jobs represent future notifications which users can request. They are a combination of the channelId to which notifications will be sent, a cron expression which specifies the regularity of the notifications and the service or application through which the request has been made (that's Telegram or Slack). Whenever a user requests a scheduled notification of the status of the tube, a job will be saved in the database.

The other two sections of the repository are the platform-specific code for the bots; Telegram and Slack. In both of them a ring server is implemented to process requests and return TFL data. For this reason, there are two main functions, one for each of the bots – for this, two aliases have been set in the project.clj for convenience.

Its important to note that both bots work with the same database. That's why the jobs table has a service column. The column allows the applications to distinguish which scheduled jobs correspond to which users. Records will have the values of either telegram or slack. This allows them, in case the use both applications, to schedule different jobs from the different services.

TFL API

In order to make this bot work it uses the current TFL Unified API. For more information, visit: https://api.tfl.gov.uk/.

Documentation/Where I learnt

  1. How to set up environment profiles

  2. Using JDBC Clojure wrapper

  3. Setting up testing fixtures

  4. Regexes in Clojure

  5. The complete guide to Clojure destructuring

License

Copyright © 2019 Manzanit0

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

londibot-prototype's People

Contributors

manzanit0 avatar

Watchers

 avatar

Forkers

massimiliano76

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.