Good day, Habrahabr! The purpose of the article is to tell novice programmers about the possibility of not only developing something interesting based on simple tools, but also placing the project in the public domain, and with diligent efforts to see that not only the author can appreciate the effort spent.
Closer to the point is my first pet-project, which solves the problem of accessing the university’s schedule on mobile devices via Telegram, with an audience of more than one hundred fifty unique visitors per day. Despite the fact that the figure is not transcendental, this is quite a good result, serving as a follow-up long-term environment for real combat development, taking into account all possible features and supporting the product for a long period, which is indisputably useful and valuable for each developer.
UPD : the project was successfully refactored using the author's framework for university timetables - “Rutetider” (
article on Habrahabr ,
GitHub ). Therefore, some points in this article may not coincide, some may not exist or look different, but the concept is preserved.
Not having the ability to expand access to the schedule using mobile applications, it was decided to use one of a number of popular instant messengers, namely, automated algorithms inside, that is, bots.
')
I will say in advance that the programming language is Python, which, however, is not important because of the general availability and cross-platform of both the resources required in this article and the majority of other resources available for use in other projects. And also missed easily accessible explanations, for example, the explanation of the plug-in (library), the registration of the bot Telegram token, or any other obvious point, located directly in Google on the first link.
I provide the following set of required areas involved in the project (
link to github.com ). In the sum of the categories, one can assess the vastness of the knowledge necessary for application, and, better, potentially open to study:
- pyTelegramBotAPI is the main library for interacting with the Telegram API .
- Heroku Cloud Application Platform - support for an impressive range of languages, large and light (sometimes forcing the monitor to pour water) documentation with tutorials for beginners, a user-friendly interface for the complete work with the application, including access via the console, the presence of databases. An important factor is the ability to "spin" on the server around the clock .
- Github - working with application deployment on a server runs with the Git version control system. A nice bonus is to combine it with your repository without time.
- PostgreSQL - SQL database, and nothing to say.
In the process it is worth adding the use of a virtual environment, one of the important frameworks (Flask), an impressive set of imported modules and everything that I did not recall.
About the project as a whole
I did not always have the opportunity to see which items need to be visited today and in which particular classrooms my classmates are located. The only alternatives at the moment is the entrance to the non-adaptive website of the university from the word “absolutely” and a visit to the public in social networks, which does not suit me personally as a user.
It was decided that “one-knot” knowledge of one language, zero real experience in programming projects, and even worse, a lack of development experience, I began to prepare the further path of the application. I was lucky with the business plan, it was on the schedule website, everything that I needed or needed to implement now is already implemented by others, it remains only to steal and think a little.
At the moment, the initial and final menus according to the order of pictures look like this.
1) To make it possible in a few clicks to get the schedule for today and tomorrow - starting with the button “Get the schedule” and ending with the choice of the day (“Today” or “Tomorrow”), the user needs to choose his faculty, course and group.
- Why can not make a schedule for the whole week? Because I want more than thirty people per day to use my bot (most of the good updates I never release will have a bad effect on the online).
- Why do you need to press so many buttons, you can not cut or just write your group once? I know that it is more convenient to click on the mobile phone screen three times than to write your group clearly and correctly (from two letters, two numbers and a hyphen and more) by calling the on-screen keyboard, and the final menu can also be used the next day.
2) Sticker with time intervals of pairs - the sticker is fun, the sticker is cool, and you can exchange them, so they are pressed by a third of users every day, although there is no great need (chat history is saved, it’s not difficult to flip up). At the moment, the sticker can be improved by adding time breaks.
3) Schedule function by subscription - I knowingly, up to the very first line of the code, knew that it was necessary to enter something that would allow to get the schedule in one click. The need to go through all the stages in which all the same faculty is indicated, the course and the group, remained, but to do it all the time - no. In the final menu there is a button “Subscribe to this group”, with which the user, without making “extra movements”, gets a schedule for two days at a time.
4) Feedback and updates are minor, but very important functions, if in the first version you need to be able to hear your users (from requests to enter something new and reporting on bugs to praise and thanks), then in the second - learn how to communicate your community about server issues, new releases and other important information.
5) Go back - this is the first function implemented by the numerous requests of
working users. The obvious advantage over the erroneous transitions was embodied in just a couple of days, given the complex data table architecture for this application.
6) Statistics collection - statistics for each user and his actions. As a result, we obtain information when it is worthwhile to introduce updates that need to be tested load-bearing, and when “on the sly”. Based on the data, you can also designate the scope of relevance of a particular area.
7) All work should be automated - the built-in CRON as a plug-in library on the server is a regular scheduler that performs tasks in a given period of time.
- Daily update of dates - immediately after midnight, updated schedule dates for the current and tomorrow are available.
- Weekly schedule updates - for certain resource reasons, the service does not have the ability to update it every day, so users can expect to be updated once a week (the schedule is updated daily from 04.12).
Application Deployment Aspects
Here I will describe non-obvious instructions on how to deploy to the Heroku Cloud Platform, including the technical code in the form of connecting to the server, implementing databases, and designing the application. Before reading it is advisable to familiarize yourself with the
training instructions for beginners , to “touch” the already prepared project and service interface proposed in it.
For general convenience, all variables and data are contained in one section of the code, but you do not do this, make configs.
We force the cloud service, on which our project has already been created, to see the main script:
import telebot import os from flask import Flask, request bot = telebot.TeleBot( ) server = Flask(__name__) @bot.message_handler(commands=['start']) def start(message): bot.reply_to(message, 'Hello, ' + message.from_user.first_name) @bot.message_handler(func=lambda message: True, content_types=['text']) def echo_message(message): bot.reply_to(message, message.text) @server.route("/ ", methods=['POST']) def getMessage(): bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))]) return "!", 200 @server.route("/") def webhook(): bot.remove_webhook() bot.set_webhook(url="https:// / ") return "!", 200 server.run(host="0.0.0.0", port=os.environ.get('PORT', 5000))
Everything you need to connect is available in Database Credentials on the databases page (user name, password, host, port, and the database name itself). Everything is placed in a small script using convenient modules for development:
import dj_database_url import psycopg2 DATABASELINK = "postgres://username:password@host:port/database" db_info = dj_database_url.config(default=DATABASELINK) connection = psycopg2.connect(database=db_info.get('NAME'), user=db_info.get('USER'), password=db_info.get('PASSWORD'), host=db_info.get('HOST'), port=db_info.get('PORT')) cursor = connection.cursor()
This material is quite enough to independently begin the product development phase.
If the community appreciates the article and shows little interest in continuing, I will definitely please the next part, in which I will describe all the technical logic of the application and tell you how this little success resulted for me in the end (
part two by reference ).