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)
All Classes Namespaces Files Functions Variables Typedefs Macros
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 
11 class GitBot : public tgbotxx::Bot {
12  using ChatId = decltype(tgbotxx::Chat::id);
13  using UserId = decltype(tgbotxx::User::id);
14 
15 public:
16  GitBot();
17  virtual ~GitBot() = default;
18 
19 private:
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 
50 private:
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 
65 private:
69  void notifyAdmin(const std::string& msg, const std::source_location& loc = std::source_location::current());
70 
71 private:
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 
88 public:
90  static bool isRepositoryFullName(const std::string &str);
91 
93  static bool isRepositoryFullURL(const std::string &str, std::string &fullname);
94 
95 private:
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".