Visualization of Telegram survey results in Grafana

I would like to share an interesting solution, which I am sure would be useful for cooperatives and partnerships.

Modern technologies are gradually penetrating all areas of our lives. Today it is difficult to imagine neighbors of apartment buildings or land plots of SNT/DNT without a group chat in the messenger. Such groups are convenient for communication; they allow you to quickly resolve issues, publish announcements, share news, and even conduct polls and voting.

By the way, voting in the messenger can have legal force. To do this, you need to decide on the possibility of using electronic means when making decisions at the general meeting of members of the partnership and make appropriate changes to the charter of the partnership (clause 25, part 1, art. 17 of Federal Law No. 217-FZ). This is what we did when creating our partnership, and now we resolve most issues remotely via Telegram.

The results of surveys conducted in Telegram are displayed almost instantly in the messenger interface. However, such results do not take into account cases where the number of votes among the members of the partnership differs. In addition, spouses/relatives of participants may be present in the general chat and may vote without the right to vote. In the end, it all comes down to the fact that it is necessary to recount the votes every time.

In order not to tire the meeting secretary with unnecessary routine, we decided to automate this process. And this is what we got.

The screenshots below show the results of one of the first Telegram surveys we conducted.

Pie Chart and Stat panels

Pie Chart and Stat panels

Geomap panel

Geomap panel

Thus, a member of the partnership simply casts a vote in a Telegram poll, and the system automatically takes into account his vote(s) online and displays the data on the dashboard.

Great, isn’t it?

The visual part of our solution is implemented in Grafana. We used Pie Chart, Stat and Geomap panels. The data source is a database MongoDB with the collections “Neighbors”, “Lands”, “Polls”. To connect MongoDB to Grafana we use a plugin grafana-mongodb-community-plugin.

To programmatically “catch” votes in Telegram, the poll must be created by a Telegram bot. We wrote such a bot in Golang using the library telegram-bot-api.

Votes are taken into account in the handlePollAnswer function. Below is a simplified version of it.

func handlePollAnswer(pollAnswer *tgbotapi.PollAnswer) {
	// Ищем пользователя в БД
	neighbor, err := db.GetNeighborByChatID(pollAnswer.User.ID)
	if err != nil {
		// handle error
        return
	}
	// Если пользователь отменил голос, исключим его голос из базы
	if len(pollAnswer.OptionIDs) == 0 {
		err := db.DeleteVote(pollAnswer.PollID, neighbor.ID)
		if err != nil {
			// handle error
			return
		}
	} else { // Если проголосовал, добавим голоса в базу
		for _, optionID := range pollAnswer.OptionIDs {
			err := db.AddVote(pollAnswer.PollID, neighbor, optionID)
			if err != nil {
				// handle error
				return
			}
		}
	}
}

An example of creating a poll in the Telegram interface is shown in the screenshot below.

An example of creating a survey through the Telegram bot interface

An example of creating a survey through the Telegram bot interface

To display data on a map, we wrote a small web service that, based on the data from the “Polls” and “Lands” collections, generates GeoJSON and returns it via an HTTP request. We specified the URL to our web service in the Geomap panel settings. The display rules were also set up there.

We first asked Yandex for a background for the map (tiles) for our non-profit project, since it contained the names of the streets of our village. But Yandex refused us. As a result, the decision was made to use the Here substrate, for which we are very grateful.

That’s all I have. Write your questions in the comments.
Thank you for your attention!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *