@@ -136,6 +136,7 @@ protected static function defineTables()
136
136
'edited_message ' ,
137
137
'inline_query ' ,
138
138
'message ' ,
139
+ 'request_limiter ' ,
139
140
'telegram_update ' ,
140
141
'user ' ,
141
142
'user_chat ' ,
@@ -1065,4 +1066,83 @@ public static function selectChats(
1065
1066
throw new TelegramException ($ e ->getMessage ());
1066
1067
}
1067
1068
}
1069
+
1070
+ /**
1071
+ * Get Telegram API request count for current chat / message
1072
+ *
1073
+ * @param integer $chat_id
1074
+ * @param string $inline_message_id
1075
+ *
1076
+ * @return array|bool (Array containing TOTAL and CURRENT fields or false on invalid arguments)
1077
+ * @throws \Longman\TelegramBot\Exception\TelegramException
1078
+ */
1079
+ public static function getTelegramRequestCount ($ chat_id = null , $ inline_message_id = null )
1080
+ {
1081
+ if (!self ::isDbConnected ()) {
1082
+ return false ;
1083
+ }
1084
+
1085
+ try {
1086
+ $ sth = self ::$ pdo ->prepare ('SELECT
1087
+ (SELECT COUNT(*) FROM ` ' . TB_REQUEST_LIMITER . '` WHERE `created_at` >= :date) as LIMIT_PER_SEC_ALL,
1088
+ (SELECT COUNT(*) FROM ` ' . TB_REQUEST_LIMITER . '` WHERE ((`chat_id` = :chat_id AND `inline_message_id` IS NULL) OR (`inline_message_id` = :inline_message_id AND `chat_id` IS NULL)) AND `created_at` >= :date) as LIMIT_PER_SEC,
1089
+ (SELECT COUNT(*) FROM ` ' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE
1090
+ ' );
1091
+
1092
+ $ date = self ::getTimestamp (time ());
1093
+ $ date_minute = self ::getTimestamp (strtotime ('-1 minute ' ));
1094
+
1095
+ $ sth ->bindParam (':chat_id ' , $ chat_id , \PDO ::PARAM_STR );
1096
+ $ sth ->bindParam (':inline_message_id ' , $ inline_message_id , \PDO ::PARAM_STR );
1097
+ $ sth ->bindParam (':date ' , $ date , \PDO ::PARAM_STR );
1098
+ $ sth ->bindParam (':date_minute ' , $ date_minute , \PDO ::PARAM_STR );
1099
+
1100
+ $ sth ->execute ();
1101
+
1102
+ return $ sth ->fetch ();
1103
+ } catch (\Exception $ e ) {
1104
+ throw new TelegramException ($ e ->getMessage ());
1105
+ }
1106
+ }
1107
+
1108
+ /**
1109
+ * Insert Telegram API request in db
1110
+ *
1111
+ * @param string $method
1112
+ * @param array $data
1113
+ *
1114
+ * @return bool If the insert was successful
1115
+ * @throws \Longman\TelegramBot\Exception\TelegramException
1116
+ */
1117
+ public static function insertTelegramRequest ($ method , $ data )
1118
+ {
1119
+ if (!self ::isDbConnected ()) {
1120
+ return false ;
1121
+ }
1122
+
1123
+ $ chat_id = ((isset ($ data ['chat_id ' ])) ? $ data ['chat_id ' ] : null );
1124
+ $ inline_message_id = (isset ($ data ['inline_message_id ' ]) ? $ data ['inline_message_id ' ] : null );
1125
+
1126
+ try {
1127
+ $ sth = self ::$ pdo ->prepare ('INSERT INTO ` ' . TB_REQUEST_LIMITER . '`
1128
+ (
1129
+ `method`, `chat_id`, `inline_message_id`, `created_at`
1130
+ )
1131
+ VALUES (
1132
+ :method, :chat_id, :inline_message_id, :date
1133
+ );
1134
+ ' );
1135
+
1136
+ $ created_at = self ::getTimestamp ();
1137
+
1138
+ $ sth ->bindParam (':chat_id ' , $ chat_id , \PDO ::PARAM_STR );
1139
+ $ sth ->bindParam (':inline_message_id ' , $ inline_message_id , \PDO ::PARAM_STR );
1140
+ $ sth ->bindParam (':method ' , $ method , \PDO ::PARAM_STR );
1141
+ $ sth ->bindParam (':date ' , $ created_at , \PDO ::PARAM_STR );
1142
+
1143
+ return $ sth ->execute ();
1144
+ } catch (\Exception $ e ) {
1145
+ throw new TelegramException ($ e ->getMessage ());
1146
+ }
1147
+ }
1068
1148
}
0 commit comments