chgksuite 0.24.0b3__py3-none-any.whl → 0.24.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,115 @@
1
+ import asyncio
2
+ import json
3
+ import os
4
+ import sqlite3
5
+ import threading
6
+ from datetime import datetime
7
+
8
+ import requests
9
+ import toml
10
+ from telegram import Update
11
+ from telegram.ext import Application, ContextTypes, MessageHandler, filters
12
+
13
+ from chgksuite.common import get_chgksuite_dir
14
+
15
+
16
+ class TelegramSidecarBot:
17
+ def __init__(self, bot_token, db_path):
18
+ self.db_path = db_path
19
+ self._local = threading.local()
20
+ self.token = bot_token
21
+
22
+ @property
23
+ def conn(self):
24
+ if not hasattr(self._local, "connection"):
25
+ self._local.connection = sqlite3.connect(self.db_path)
26
+ return self._local.connection
27
+
28
+ async def handle_message(self, update: Update, _: ContextTypes.DEFAULT_TYPE):
29
+ cursor = self.conn.cursor()
30
+ raw_data = json.dumps(update.to_dict(), ensure_ascii=False)
31
+ cursor.execute(
32
+ "INSERT INTO messages (raw_data, chat_id, created_at) VALUES (?, ?, ?)",
33
+ (raw_data, update.message.chat.id, datetime.now().isoformat()),
34
+ )
35
+ self.conn.commit()
36
+
37
+ async def error_handler(self, update, context):
38
+ print(f"Update {update} caused error: {context.error}")
39
+
40
+ async def check_connectivity(self):
41
+ url = f"https://api.telegram.org/bot{self.token}/getMe"
42
+ req = requests.get(url)
43
+ cursor = self.conn.cursor()
44
+ if req.status_code == 200 and "ok" in req.json():
45
+ cursor.execute(
46
+ "INSERT INTO bot_status (raw_data, created_at) VALUES (?, ?)",
47
+ (json.dumps({"status": "ok"}), datetime.now().isoformat()),
48
+ )
49
+ self.conn.commit()
50
+ else:
51
+ print(f"couldn't check status, req: {req.text}")
52
+ cursor.execute(
53
+ "INSERT INTO bot_status (raw_data, created_at) VALUES (?, ?)",
54
+ (
55
+ json.dumps(
56
+ {
57
+ "status": "bad",
58
+ "error": req.text,
59
+ "status_code": req.status_code,
60
+ }
61
+ ),
62
+ datetime.now().isoformat(),
63
+ ),
64
+ )
65
+ self.conn.commit()
66
+ return True
67
+
68
+ def run(self):
69
+ loop = asyncio.get_event_loop()
70
+ application = (
71
+ Application.builder()
72
+ .token(self.token)
73
+ .connect_timeout(30.0)
74
+ .read_timeout(30.0)
75
+ .write_timeout(30.0)
76
+ .build()
77
+ )
78
+ application.add_handler(MessageHandler(filters.ALL, self.handle_message))
79
+ application.add_error_handler(self.error_handler)
80
+ loop.run_until_complete(application.initialize())
81
+ loop.run_until_complete(application.start())
82
+ loop.run_until_complete(
83
+ application.updater.start_polling(
84
+ allowed_updates=Update.ALL_TYPES, drop_pending_updates=True
85
+ )
86
+ )
87
+ loop.run_forever()
88
+
89
+
90
+ def run_bot_in_thread(bot_token, db_path):
91
+ """Run the bot in a daemon thread."""
92
+
93
+ def thread_function():
94
+ loop = asyncio.new_event_loop()
95
+ asyncio.set_event_loop(loop)
96
+ bot = TelegramSidecarBot(bot_token, db_path)
97
+ connectivity_ok = loop.run_until_complete(bot.check_connectivity())
98
+ if not connectivity_ok:
99
+ raise Exception("bot couldn't connect")
100
+ bot.run()
101
+
102
+ bot_thread = threading.Thread(target=thread_function, daemon=True)
103
+ bot_thread.start()
104
+ return bot_thread
105
+
106
+
107
+ def main():
108
+ toml_path = os.path.join(get_chgksuite_dir(), "telegram.toml")
109
+ with open(toml_path, "r", encoding="utf8") as f:
110
+ bot_token = toml.load(f)["bot_token"]
111
+ run_bot_in_thread(bot_token, "test_bot.db")
112
+
113
+
114
+ if __name__ == "__main__":
115
+ main()
Binary file
chgksuite/version.py CHANGED
@@ -1 +1 @@
1
- __version__ = "0.24.0b3"
1
+ __version__ = "0.24.2"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: chgksuite
3
- Version: 0.24.0b3
3
+ Version: 0.24.2
4
4
  Summary: A package for chgk automation
5
5
  Home-page: https://gitlab.com/peczony/chgksuite
6
6
  Author: Alexander Pecheny
@@ -24,8 +24,7 @@ Requires-Dist: pyperclip
24
24
  Requires-Dist: python-docx
25
25
  Requires-Dist: python-pptx
26
26
  Requires-Dist: requests
27
- Requires-Dist: telethon
28
- Requires-Dist: TgCrypto
27
+ Requires-Dist: python-telegram-bot
29
28
  Requires-Dist: toml
30
29
  Dynamic: author
31
30
  Dynamic: author-email
@@ -1,25 +1,26 @@
1
1
  chgksuite/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  chgksuite/__main__.py,sha256=0-_jfloveTW3SZYW5XEagbyaHKGCiDhGNgcLxsT_dMs,140
3
3
  chgksuite/cli.py,sha256=8YoHWfc-wv6gdYMPH3X8HxV9SG70S7M6IQqavJaBXV4,31990
4
- chgksuite/common.py,sha256=lHQG72HghaBudr1X6ognGbrKmStW5k9nGSLdoxydMSA,10834
4
+ chgksuite/common.py,sha256=FkEntLcVTApjLtTFTDf2QiMOath05i322wcmJIFjztQ,11093
5
5
  chgksuite/parser.py,sha256=AVNeTUgv0aGHsxWunV_7bM4Ul2hbDslkzqg0dU49lic,37285
6
6
  chgksuite/parser_db.py,sha256=W1--OcDnx18mehH1T2ISHu3Saeq-9mqHo-xJopNySXI,11135
7
7
  chgksuite/trello.py,sha256=BG1Qb_W7Uu4o3Mfc_tK71ElU8ysdSplGlj_sAKfvUn4,14730
8
8
  chgksuite/typotools.py,sha256=Jdk65Wn_bXqpQtOT7PkBZyD2ZG1MBeeZFPMzcHEPkf4,12771
9
- chgksuite/version.py,sha256=_g51twxO95ihIudz5fXK0W3t40T0co_jzKU7dVW0AN0,25
9
+ chgksuite/version.py,sha256=UBF3OYTcBAovta3ux5ybxb0MZYAGpGO79WH_ax2NGeI,23
10
10
  chgksuite/vulture_whitelist.py,sha256=P__p_X0zt10ivddIf81uyxsobV14vFg8uS2lt4foYpc,3582
11
11
  chgksuite/composer/__init__.py,sha256=MAOVZIYXmZmD6nNQSo9DueV6b5RgxF7_HGeLvsAhMJs,6490
12
- chgksuite/composer/chgksuite_parser.py,sha256=gOFAqPXRh-I5_GAPM6j1Jki-Wcdald5vPp2vFU1NwOM,8303
12
+ chgksuite/composer/chgksuite_parser.py,sha256=MFcLUWbccMqo3OYEuaAIA0loEvWM_PNS9vR7c1z_c60,8843
13
13
  chgksuite/composer/composer_common.py,sha256=S5ipehxep6LlGIZ9dcBnifbVaMYXijMhq6-pUHRISo8,15309
14
14
  chgksuite/composer/db.py,sha256=71cINE_V8s6YidvqpmBmmlWbcXraUEGZA1xpVFAUENw,8173
15
15
  chgksuite/composer/docx.py,sha256=5MASXACM-ztWrr3VdO8HZ-W-hWWQ5TY1jXMsCQIufGc,18346
16
16
  chgksuite/composer/latex.py,sha256=WtLdUICxeX4_5vHEJRF0YhFLpTsOUwBkQFunQS488FA,9248
17
17
  chgksuite/composer/lj.py,sha256=nty3Zs3N1H0gNK378U04aAHo71_5cABhCM1Mm9jiUEA,15213
18
18
  chgksuite/composer/openquiz.py,sha256=CWGxb_cPG2Qh3dQ3hu0pS5fOfhKc8klZuO38Lo9hUF8,7020
19
- chgksuite/composer/pptx.py,sha256=05JxulgU80jBmxRqC3cFh0q_B3bMUCe31JzLYvnNZYM,19119
19
+ chgksuite/composer/pptx.py,sha256=lmLld5CCNfndmLwWz2rRqZFyPIUxl0Dvk6wO1jW_c5s,19116
20
20
  chgksuite/composer/reddit.py,sha256=-Eg4CqMHhyGGfCteVwdQdtE1pfUXQ42XcP5OYUrBXmo,3878
21
21
  chgksuite/composer/stats.py,sha256=GbraSrjaZ8Mc2URs5aGAsI4ekboAKzlJJOqsbe96ELA,3995
22
- chgksuite/composer/telegram.py,sha256=Af0LJIjz3vO5OiQikV7XQObx4ABJewBp7YqHRt0P4GY,25670
22
+ chgksuite/composer/telegram.py,sha256=H65LS79JZ3aCcqeq_SrnUU4E4yro1f-EZxCM3IrIyyQ,46526
23
+ chgksuite/composer/telegram_bot.py,sha256=xT5D39m4zGmIbHV_ZfyQ9Rc8PAmG2V5FGUeDKpkgyTw,3767
23
24
  chgksuite/composer/telegram_parser.py,sha256=50WqOuvzzdMJJm5wsSLS49oURAQRYToPnbPJjQbMYC4,8096
24
25
  chgksuite/resources/cheader.tex,sha256=Jfe3LESk0VIV0HCObbajSQpEMljaIDAIEGSs6YY9rTk,3454
25
26
  chgksuite/resources/fix-unnumbered-sections.sty,sha256=FN6ZSWC6MvoRoThPm5AxCF98DdgcxbxyBYG6YImM05s,1409
@@ -42,10 +43,11 @@ chgksuite/resources/regexes_ua.json,sha256=IXtJtoUY15OoBu5Y5yW6adhJUhsDRkwfxWsPY
42
43
  chgksuite/resources/regexes_uz_cyr.json,sha256=mIvWjyZZXQ3lr_ntuC7-ybknnYJxSSQEKxqnGluTvow,1106
43
44
  chgksuite/resources/template.docx,sha256=Do29TAsg3YbH0rRSaXhVzKEoh4pwXkklW_idWA34HVE,11189
44
45
  chgksuite/resources/template.pptx,sha256=hEFWqE-yYpwZ8ejrMCJIPEyoMT3eDqaqtiEeQ7I4fyk,29777
46
+ chgksuite/resources/template_shorin.pptx,sha256=hEFWqE-yYpwZ8ejrMCJIPEyoMT3eDqaqtiEeQ7I4fyk,29777
45
47
  chgksuite/resources/trello.json,sha256=M5Q9JR-AAJF1u16YtNAxDX-7c7VoVTXuq4POTqYvq8o,555
46
- chgksuite-0.24.0b3.dist-info/licenses/LICENSE,sha256=_a1yfntuPmctLsuiE_08xMSORuCfGS8X5hQph2U_PUw,1081
47
- chgksuite-0.24.0b3.dist-info/METADATA,sha256=onWj7gdTXF2zHFbAeD7CSg09Tx619sywCW33HtvNRFk,1309
48
- chgksuite-0.24.0b3.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
49
- chgksuite-0.24.0b3.dist-info/entry_points.txt,sha256=lqjX6ULQZGDt0rgouTXBuwEPiwKkDQkSiNsT877A_Jg,54
50
- chgksuite-0.24.0b3.dist-info/top_level.txt,sha256=cSWiRBOGZW9nIO6Rv1IrEfwPgV2ZWs87QV9wPXeBGqM,10
51
- chgksuite-0.24.0b3.dist-info/RECORD,,
48
+ chgksuite-0.24.2.dist-info/licenses/LICENSE,sha256=_a1yfntuPmctLsuiE_08xMSORuCfGS8X5hQph2U_PUw,1081
49
+ chgksuite-0.24.2.dist-info/METADATA,sha256=EfR9tuFPhFwdMjYtaExqh1P-CuoXBPSuU-_7avOAvYs,1294
50
+ chgksuite-0.24.2.dist-info/WHEEL,sha256=pxyMxgL8-pra_rKaQ4drOZAegBVuX-G_4nRHjjgWbmo,91
51
+ chgksuite-0.24.2.dist-info/entry_points.txt,sha256=lqjX6ULQZGDt0rgouTXBuwEPiwKkDQkSiNsT877A_Jg,54
52
+ chgksuite-0.24.2.dist-info/top_level.txt,sha256=cSWiRBOGZW9nIO6Rv1IrEfwPgV2ZWs87QV9wPXeBGqM,10
53
+ chgksuite-0.24.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.0)
2
+ Generator: setuptools (79.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5