GitWatcherBot 1.0.0
A Telegram Bot that notifies you when a new change is made in your repositories (issues, pull requests, stars, forks, and watches)
Loading...
Searching...
No Matches
GitBot.hpp
Go to the documentation of this file.
1#pragma once
2#include <chrono>
3#include <condition_variable>
4#include <cstdint>
5#include <tgbotxx/tgbotxx.hpp>
6#include <type_traits>
7#include "api/GitApi.hpp"
8#include <cpr/threadpool.h>
9
11class GitBot : public tgbotxx::Bot {
12 using ChatId = decltype(tgbotxx::Chat::id);
13 using UserId = decltype(tgbotxx::User::id);
14
15public:
17 virtual ~GitBot() = default;
18
19private:
25 bool middleware(const tgbotxx::Ptr<tgbotxx::Message> &message);
26
28 void onStart() override;
30 void onStop() override;
32 void onCommand(const tgbotxx::Ptr<tgbotxx::Message> &message) override;
34 void onNonCommandMessage(const tgbotxx::Ptr<tgbotxx::Message> &message) override;
36 void onCallbackQuery(const tgbotxx::Ptr<tgbotxx::CallbackQuery> &callbackQuery) override;
38 void onLongPollError(const std::string &errorMessage, tgbotxx::ErrorCode errorCode) override;
39
41 void onStartCommand(const tgbotxx::Ptr<tgbotxx::Message> message);
42 void onWatchRepoCommand(const tgbotxx::Ptr<tgbotxx::Message> message);
43 void onUnwatchRepoCommand(const tgbotxx::Ptr<tgbotxx::Message> message);
44 void onMyReposCommand(const tgbotxx::Ptr<tgbotxx::Message> message);
45
48 void onUserBlockedBot(const UserId userId);
49
50private:
52 void watchDog();
53
55 void alertUserRepositoryStarsChange(UserId userId, const std::string& repositoryName, std::int64_t oldStarsCount, std::int64_t newStarsCount);
57 void alertUserRepositoryWatchersChange(UserId userId, const std::string& repositoryName, std::int64_t oldWatchersCount, std::int64_t newWatchersCount);
59 void alertUserRepositoryIssuesChange(UserId userId, const std::string& repositoryName, std::int64_t oldIssuesCount, std::int64_t newIssuesCount);
61 void alertUserRepositoryForksChange(UserId userId, const std::string& repositoryName, std::int64_t oldForksCount, std::int64_t newForksCount);
63 void alertUserRepositoryPullRequestsChange(UserId userId, const std::string& repositoryName, std::int64_t oldPullsCount, std::int64_t newPullsCount);
64
65private:
69 void notifyAdmin(const std::string& msg, const std::source_location& loc = std::source_location::current());
70
71private:
74 void safeSendMessage(UserId userId, std::string messageText,
75 std::int32_t messageThreadId = 0,
76 const std::string &parseMode = "",
77 const std::vector<tgbotxx::Ptr<tgbotxx::MessageEntity>> &entities = std::vector<tgbotxx::Ptr<tgbotxx::MessageEntity>>(),
78 bool disableWebPagePreview = false,
79 bool disableNotification = false,
80 bool protectContent = false,
81 std::int32_t replyToMessageId = 0,
82 bool allowSendingWithoutReply = false,
83 const tgbotxx::Ptr<tgbotxx::IReplyMarkup> &replyMarkup = nullptr);
84
86 void safeSendLargeMessage(UserId userId, const std::string &messageText);
87
88public:
90 static bool isRepositoryFullName(const std::string &str);
91
93 static bool isRepositoryFullURL(const std::string &str, std::string &fullname);
94
95private:
96 UserId m_adminUserId{};
97 std::unique_ptr<GitApi> m_gitApi;
98 std::unique_ptr<std::thread> m_watchdogThread;
99 std::mutex m_sleepMutex;
100 std::atomic<bool> m_watchdogRunning;
101 std::condition_variable m_watchdogCv;
102 std::unique_ptr<cpr::ThreadPool> m_threadPool;
103
104 inline static constexpr std::size_t kTelegramMessageMax = 4096;
105 inline static constexpr std::size_t kMaxWatchListRepositories = 25;
106};
Bot class.
Definition GitBot.hpp:11
virtual ~GitBot()=default
static bool isRepositoryFullURL(const std::string &str, std::string &fullname)
Returns true if str is a repository full url e.g "https://github.com/torvalds/linux".
static bool isRepositoryFullName(const std::string &str)
Returns true if str is a repository full name e.g "torvalds/linux".