-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcv.py
executable file
·852 lines (730 loc) · 27 KB
/
rcv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
#!/usr/bin/env python3
import argparse
import asyncio
import base64
import configparser
import datetime
import hashlib
import io
import json
import logging
import os
import random
import ssl
import sys
import tempfile
import time
import typing
# https://pypi.org/project/aiogram/
import aiogram
import aiogram.contrib.fsm_storage.redis
import aiogram.contrib.middlewares.logging
import aiogram.dispatcher
import aiogram.dispatcher.filters.state
import aiogram.utils.exceptions
import aiogram.utils.executor
import aiogram.utils.markdown as md
# https://pypi.org/project/cryptography/
import cryptography
import cryptography.x509.oid
import cryptography.hazmat.backends
import cryptography.hazmat.primitives
import cryptography.hazmat.primitives.asymmetric
# https://pypi.org/project/PyYAML/
import yaml
log = logging.getLogger("rcv")
#
# Visualization
#
async def result_diagram(options, raw_ballots):
# Calculate votes in each stage:
# - narrow by eliminating the least popular option.
# - stop when one candidate is left.
# - use virtual Exhausted column for ballots that stop short of ranking
# all options.
# Pre-split ballots
ballots = []
for ballot in raw_ballots.values():
ballot = ballot.split(",")
if ballot == [""]:
ballot = []
ballots.append([int(b) for b in ballot])
# Count total votes in each stage, determine who is elminated
exhausted = "exhausted"
paths = {}
stages = []
eliminated = set()
eliminated_nodes = set()
active = set()
for b in ballots:
if len(b) > 0:
active.add(b[0])
stage_index = 0
while len(active) > 1:
stage = {a: 0 for a in active}
stage[exhausted] = 0
stages.append(stage)
for ballot in ballots:
for choice in ballot:
if choice in active:
break
else:
choice = exhausted
stage[choice] += 1
print(f"Stage: {stage}")
# Eliminate the least popular option
# If tie for lowest, eliminate all the lowest together
options_by_score = {}
for option, score in stage.items():
if option == exhausted:
continue
if score not in options_by_score:
options_by_score[score] = set()
options_by_score[score].add(option)
lowest_score = min(options_by_score.keys())
#print(f"OBS: {options_by_score}")
#print(f"Low: {lowest_score}")
print(f"Eliminate: {options_by_score[lowest_score]}")
eliminated.update(options_by_score[lowest_score])
for ballot in ballots:
try:
primary_choice = ballot[0]
except IndexError:
primary_choice = exhausted
ballot_iter = iter(ballot)
for choice in ballot_iter:
if choice in active:
break
else:
choice = exhausted
if choice not in eliminated or choice == exhausted:
# The ballots that chose an option that wasn't eliminated will
# always go to the same choice in the next stage.
next_choice = choice
else:
# The ballots that chose an option that was eliminated will go
# to their next best choice in the next stage, assuming it
# hasn't been removed already.
for next_choice in ballot_iter:
if next_choice in active and next_choice not in eliminated:
break
else:
next_choice = exhausted
k_from = f"{stage_index}-{choice}"
k_to = f"{stage_index+1}-{next_choice}"
if k_from not in paths:
paths[k_from] = {}
if k_to not in paths[k_from]:
paths[k_from][k_to] = {}
if primary_choice not in paths[k_from][k_to]:
paths[k_from][k_to][primary_choice] = 0
paths[k_from][k_to][primary_choice] += 1
for e in eliminated:
eliminated_nodes.add(f"{stage_index}-{e}")
active.discard(e)
stage_index += 1
winner = options[active.pop()] if len(active) > 0 else "Tied (no winner)"
# Convert stages into D3 Sanke data
node_dedup = set()
for node, dests in paths.items():
node_dedup.add(node)
node_dedup.update(dests.keys())
nodes = []
for node in sorted(node_dedup):
index, option = node.split("-")
title = "Exhausted" if option == exhausted else options[int(option)]
if len(title) > 23:
title = f"{title[:21]}..."
if node in eliminated_nodes:
title += " ❌"
nodes.append({
"id": node,
"title": title,
})
links = []
for k_from, dests in paths.items():
for k_to, primary_choices in dests.items():
for primary_choice, count in primary_choices.items():
links.append({
"source": k_from,
"target": k_to,
"value": count,
"type": str(primary_choice),
})
chart = {
"nodes": nodes,
"links": links,
"alignLinkTypes": True,
}
# Summary
summary = yaml.dump(
{
"options": options,
# Wipe out UIDs
"ballots": sorted(raw_ballots.values()),
"stages": stages,
"chart": chart,
},
default_flow_style=False,
encoding="utf-8",
allow_unicode=True,
)
if not links or not nodes:
return (None, None, summary)
# Convert to SVG via layered D3 library
with tempfile.TemporaryDirectory() as td:
d3_json = os.path.join(td, "sanke.json")
svg = os.path.join(td, "sanke.svg")
png = os.path.join(td, "sanke.png")
with open(d3_json, "w") as f:
json.dump(chart, f, ensure_ascii=False)
p = await asyncio.create_subprocess_shell(
f"svg-sankey --size 1280,720 --margins 150,150 {d3_json} > {svg}",
)
await p.communicate()
p = await asyncio.create_subprocess_exec(
"convert",
"-density", "200",
svg,
png,
)
await p.communicate()
#p = await asyncio.create_subprocess_exec("cp", d3_json, "/tmp/a.json")
#await p.communicate()
#p = await asyncio.create_subprocess_exec("cp", svg, "/tmp/a.svg")
#await p.communicate()
#p = await asyncio.create_subprocess_exec("cp", png, "/tmp/a.png")
#await p.communicate()
p = await asyncio.create_subprocess_exec(
"cat",
png,
stdout=asyncio.subprocess.PIPE,
)
sanke, stderr = await p.communicate()
return (sanke, winner, summary)
#
# State
#
class CreatePoll(aiogram.dispatcher.filters.state.StatesGroup):
title = aiogram.dispatcher.filters.state.State()
options = aiogram.dispatcher.filters.state.State()
#
# Handlers
#
def register(dp):
#
# General
#
@dp.message_handler(commands=["start", "help"])
async def send_welcome(message: aiogram.types.Message):
try:
param = message.text.split(' ', 1)[1]
except IndexError:
await dp.bot.send_message(
message.chat.id,
"""Hi, I'm RankedPollBot!
I create polls where you can rank each option instead of just choosing one. This is called Ranked Choice Voting or Instant Runoff. Since voters don't need to guess how everyone else will vote, it captures the voters' preference more accurately than a conventional First Past the Post poll.
Use /newpoll to create a poll
Use /polls to show your polls
Use /results <id> to show intermediate results for a poll
Use /stoppoll <id> to stop a poll and show the results""",
)
else:
await vote_start(message, param)
@dp.message_handler(
state="*",
commands=["cancel"],
)
async def cancel_handler(
message: aiogram.types.Message,
state: aiogram.dispatcher.FSMContext,
):
await state.finish()
await dp.bot.send_message(
message.chat.id,
"Cancelled.",
reply_markup=aiogram.types.ReplyKeyboardRemove(),
)
#
# Create Poll
#
@dp.message_handler(commands=["newpoll"])
async def new_poll(message: aiogram.types.Message):
await CreatePoll.title.set()
await dp.bot.send_message(
message.chat.id,
"What's the question or title of the poll?",
)
@dp.message_handler(
lambda message: not 1 <= len(message.text) <= 100,
state=CreatePoll.title
)
async def title_wrong_length(message: aiogram.types.Message):
await dp.bot.send_message(
message.chat.id,
"Title is too long, try again. (100 characters max)"
)
@dp.message_handler(state=CreatePoll.title)
async def process_title(
message: aiogram.types.Message,
state: aiogram.dispatcher.FSMContext
):
await CreatePoll.options.set()
title = message.text
async with state.proxy() as data:
data["title"] = title
data["options"] = []
await dp.bot.send_message(
message.chat.id,
f"""Ok, now let's fill out the options. Send each option, one at a time. You can also:
Use /done to finish
Use /delete to remove the last entered option
Use /cancel to abort creating this poll""",
)
@dp.message_handler(
lambda message: not 1 <= len(message.text) <= 100,
state=CreatePoll.options
)
async def option_wrong_length(message: aiogram.types.Message):
await dp.bot.send_message(
message.chat.id,
"Option is too long, try again. (100 characters max)",
)
@dp.message_handler(
commands=["done"],
state=CreatePoll.options
)
async def option_done(
message: aiogram.types.Message,
state: aiogram.dispatcher.FSMContext
):
async with state.proxy() as data:
if len(data["options"]) > 0:
title = data["title"]
options = sorted(data["options"])
formatted_options = "\n".join(f"- {o}" for o in options)
vote_code = base64.b32encode(os.urandom(15)).decode("ascii").lower()
await state.finish()
redis = await dp.storage.redis()
poll_id = await redis.rpush(f"user_{message.from_user.id}", vote_code)
await redis.set(f"createtime_{vote_code}", int(time.time()))
await redis.set(f"title_{vote_code}", title)
await redis.rpush(f"options_{vote_code}", *options)
await dp.bot.send_message(
message.chat.id,
f"""Ok, your poll #{poll_id} is ready. Forward the following message to those you want to share it with.""",
)
await dp.bot.send_message(
message.chat.id,
f"""Poll: {title}
{formatted_options}
To vote, [follow this link](http://t.me/RankedPollBot?start={vote_code}) (stays within Telegram) and press **Start**.""",
parse_mode=aiogram.types.ParseMode.MARKDOWN,
)
else:
await dp.bot.send_message(
message.chat.id,
"Your poll doesn't have any options, add some before using /done"
)
@dp.message_handler(
commands=["delete"],
state=CreatePoll.options
)
async def option_delete(
message: aiogram.types.Message,
state: aiogram.dispatcher.FSMContext
):
opt = None
try:
async with state.proxy() as data:
opt = data["options"].pop()
except IndexError:
pass
if opt:
await dp.bot.send_message(
message.chat.id,
f"Ok, last option was deleted: {opt}",
)
else:
await dp.bot.send_message(
message.chat.id,
f"No options exist to delete",
)
@dp.message_handler(state=CreatePoll.options)
async def option_new(
message: aiogram.types.Message,
state: aiogram.dispatcher.FSMContext
):
option = message.text
async with state.proxy() as data:
data["options"].append(option)
await dp.bot.send_message(
message.chat.id,
"Ok, next option?",
)
#
# List Polls
#
@dp.message_handler(commands=["polls"])
async def list_poll(message: aiogram.types.Message):
redis = await dp.storage.redis()
vote_codes = await redis.lrange(f"user_{message.from_user.id}", 0, -1)
reply = "Active polls:"
none = True
for i, vote_code in enumerate(vote_codes):
none = False
title = await redis.get(f"title_{vote_code}")
reply += f"\n{i+1}. {title}"
if none:
reply += "\nNo polls active"
await dp.bot.send_message(
message.chat.id,
reply,
)
#
# Stop Poll
#
@dp.message_handler(commands=["results"])
async def results(message: aiogram.types.Message):
try:
poll_id = int(message.text.split(' ', 1)[1])
except IndexError:
await dp.bot.send_message(
message.chat.id,
"Specify a Poll ID number with the command. Try /polls to see which polls you have",
)
except ValueError:
await dp.bot.send_message(
message.chat.id,
"Poll ID must be a number. Try /polls to see which polls you have",
)
else:
redis = await dp.storage.redis()
vote_code = await redis.lindex(f"user_{message.from_user.id}", poll_id - 1)
if vote_code is None:
await dp.bot.send_message(
message.chat.id,
"Poll ID does not exist. Try /polls to see which polls you have",
)
else:
await results_message(vote_code, message)
async def results_message(vote_code, message, intermediate=True):
redis = await dp.storage.redis()
title = await redis.get(f"title_{vote_code}")
options = await redis.lrange(f"options_{vote_code}", 0, -1)
ballots = await redis.hgetall(f"ballots_{vote_code}")
b, winner, summary = await result_diagram(options, ballots)
if not b or not winner:
await dp.bot.send_message(
message.chat.id,
f"There are no results",
)
return
bio = io.BytesIO(b)
bio.name = "Vote Flow.png"
desc = "Current Leader" if intermediate else "Winner"
vote_link = f"\n[You can vote here](http://t.me/RankedPollBot?start={vote_code})\n" if intermediate else ""
await dp.bot.send_document(
message.chat.id,
bio,
caption=f"""{title}
{desc}: **{winner}**
{vote_link}
Need help?
[Alternative Vote Explained](https://youtu.be/3Y3jE3B8HsE?t=104)
[Instant Runoff](https://en.wikipedia.org/wiki/Instant-runoff_voting#Election_procedure)
[STV Election Example](https://youtu.be/Ac9070OIMUg?t=13)
[Sankey Diagrams](https://en.wikipedia.org/wiki/Sankey_diagram)""",
parse_mode=aiogram.types.ParseMode.MARKDOWN,
)
bio = io.BytesIO(summary)
bio.name = "Poll Data.yaml"
await dp.bot.send_document(
message.chat.id,
bio,
caption=f"Poll data summary: {title}"
)
@dp.message_handler(commands=["stoppoll"])
async def stop_poll(message: aiogram.types.Message):
try:
poll_id = int(message.text.split(' ', 1)[1])
except IndexError:
await dp.bot.send_message(
message.chat.id,
"Specify a Poll ID number with the command. Try /polls to see which polls you have",
)
except ValueError:
await dp.bot.send_message(
message.chat.id,
"Poll ID must be a number. Try /polls to see which polls you have",
)
else:
redis = await dp.storage.redis()
vote_code = await redis.lindex(f"user_{message.from_user.id}", poll_id - 1)
if vote_code is None:
await dp.bot.send_message(
message.chat.id,
"Poll ID does not exist. Try /polls to see which polls you have",
)
else:
await dp.bot.send_message(
message.chat.id,
f"Poll {poll_id} stopped. Forward the following results to those you want to share it with.",
)
await results_message(vote_code, message, intermediate=False)
await redis.lrem(f"user_{message.from_user.id}", -1, vote_code)
await redis.delete(
*(f"k_{vote_code}" for k in ("createtime", "title", "options"))
)
#
# Vote in Poll
#
def vote_text(title, selected=[], finished=False):
if selected:
formatted_options = "\n".join(f"{i+1}. {o}" for i, o in enumerate(selected))
t = f"""{title}
{formatted_options}"""
if finished:
t += """
Finished! Thanks for voting."""
else:
t += """
Select your next best preference. If you need to start over, select Reset. When you're done ranking the options, press Finish.
Your rankings can't be changed after pressing Finish!"""
return t
else:
return f"""You're voting in a poll:
{title}
Select the options below in your order of preference. The first option you pick is what you like the most. If you need to start over, select Reset. If you want to stop before ranking all the options, press Finish.
Your rankings can't be changed after pressing Finish!"""
def vote_markup(vote_code, options, selected=[]):
# Hide options that have been selected, but keep the overall index
# value constant.
selected = set([int(s) for s in selected])
shadowed_options = []
for i, o in enumerate(options):
if i not in selected:
shadowed_options.append((i, o))
random.shuffle(shadowed_options)
markup = aiogram.types.InlineKeyboardMarkup()
for i, option in shadowed_options:
markup.add(aiogram.types.InlineKeyboardButton(
text=option,
callback_data=f"{i}.{vote_code}",
))
markup.add(
aiogram.types.InlineKeyboardButton(
text="Reset",
callback_data=f"reset.{vote_code}",
),
aiogram.types.InlineKeyboardButton(
text="Finish",
callback_data=f"finish.{vote_code}",
),
)
return markup
async def vote_start(message: aiogram.types.Message, vote_code):
redis = await dp.storage.redis()
# Disallow re-start on an active or finished voting session
uid = str(message.from_user.id)
user_ballot = await redis.hget(f"ballots_{vote_code}", uid)
if user_ballot:
await dp.bot.send_message(
message.chat.id,
"You've already voted in this poll",
)
return
title = await redis.get(f"title_{vote_code}")
if title is not None:
title = title
options = await redis.lrange(f"options_{vote_code}", 0, -1)
await dp.bot.send_message(
message.chat.id,
vote_text(title),
reply_markup=vote_markup(vote_code, options),
)
else:
await dp.bot.send_message(
message.chat.id,
"The poll you want to vote in does not exist, maybe it ended? To make your own poll, try /help",
)
@dp.callback_query_handler(lambda callback_query: True)
async def poll_vote(callback_query: aiogram.types.CallbackQuery):
try:
await callback_query.answer()
except aiogram.utils.exceptions.BadRequest:
return
uid = str(callback_query.from_user.id)
index_raw, vote_code = callback_query.data.split(".")
redis = await dp.storage.redis()
title = await redis.get(f"title_{vote_code}")
options = await redis.lrange(f"options_{vote_code}", 0, -1)
raw_selected = await redis.hget(f"ballots_{vote_code}", uid)
if raw_selected:
selected = raw_selected.split(",")
else:
selected = []
message = callback_query.message
if index_raw != "finish":
if index_raw == "reset":
selected = []
else:
index = int(index_raw)
selected.append(str(index))
await redis.hset(f"ballots_{vote_code}", uid, ",".join(selected))
await message.edit_text(vote_text(title, [options[int(s)] for s in selected]))
await message.edit_reply_markup(vote_markup(vote_code, options, selected))
else:
try:
await message.edit_text(vote_text(title, [options[int(s)] for s in selected], True))
await message.edit_reply_markup(aiogram.types.InlineKeyboardMarkup())
except aiogram.utils.exceptions.MessageNotModified:
pass
#
# Catchall
#
@dp.message_handler()
async def catchall(message: aiogram.types.Message):
await dp.bot.send_message(
message.chat.id,
f"Sorry, I don't know how to handle your message. Try /help or /cancel",
)
#
# Main
#
class ContextLogRecord(logging.LogRecord):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
task = asyncio.Task.current_task()
if task is not None:
if not hasattr(task, "uid"):
h = hashlib.md5()
h.update(str(id(task)).encode("ascii"))
task.uid = base64.b32encode(h.digest()).decode("ascii")[:15].lower()
self.log_context = task.uid
else:
self.log_context = "NoTask"
def webhook_bot(config, register_callback, loop=None):
log.info("Applying configuration")
api_token = config["auth"]["token"]
redis_host = config["redis"]["host"]
redis_port = int(config["redis"]["port"])
redis_prefix = config["redis"]["prefix"]
webhook_host = config["webhook"]["host"]
webhook_port = int(config["webhook"]["port"])
webhook_path = os.path.join(
config["webhook"]["root"],
base64.urlsafe_b64encode(os.urandom(21)).decode("ascii")
)
# Setup aiogram
loop = loop or asyncio.get_event_loop()
bot = aiogram.Bot(
token=api_token,
loop=loop,
)
storage = aiogram.contrib.fsm_storage.redis.RedisStorage2(
host=redis_host,
port=redis_port,
prefix=redis_prefix,
encoding="utf-8",
loop=loop,
)
dp = aiogram.dispatcher.Dispatcher(
bot=bot,
storage=storage,
loop=loop,
)
dp.middleware.setup(aiogram.contrib.middlewares.logging.LoggingMiddleware("aiogram"))
log.info("Registering handlers")
register_callback(dp)
# Set up HTTPS
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
webhook_certificate = None
log.info("Generating self-signed certificate")
# https://core.telegram.org/bots/self-signed
key = cryptography.hazmat.primitives.asymmetric.rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=cryptography.hazmat.backends.default_backend(),
)
with tempfile.TemporaryDirectory() as td:
key_path = os.path.join(td, "key.pem")
with open(key_path, "wb") as f:
f.write(key.private_bytes(
encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM,
format=cryptography.hazmat.primitives.serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=cryptography.hazmat.primitives.serialization.NoEncryption(),
))
name = cryptography.x509.Name([
cryptography.x509.NameAttribute(
cryptography.x509.oid.NameOID.COMMON_NAME,
webhook_host
),
])
cert = cryptography.x509.CertificateBuilder()\
.subject_name(name)\
.issuer_name(name)\
.public_key(key.public_key())\
.serial_number(cryptography.x509.random_serial_number())\
.not_valid_before(datetime.datetime.utcnow())\
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=365*5))\
.sign(
key,
cryptography.hazmat.primitives.hashes.SHA256(),
cryptography.hazmat.backends.default_backend(),
)
cert_bytes = cert.public_bytes(
encoding=cryptography.hazmat.primitives.serialization.Encoding.PEM,
)
webhook_certificate=io.BytesIO(cert_bytes)
crt_path = os.path.join(td, "cert.pem")
with open(crt_path, "wb") as f:
f.write(cert_bytes)
ssl_context.load_cert_chain(crt_path, key_path)
log.info("Defining startup and shutdown actions")
async def on_startup(dp):
url = f"https://{webhook_host}:{webhook_port}{webhook_path}"
log.info(f"Registering webhook for {url}")
await bot.set_webhook(
url=url,
certificate=webhook_certificate,
)
async def on_shutdown(dp):
# Remove webhook.
await bot.delete_webhook()
# Close Redis connection.
await dp.storage.close()
await dp.storage.wait_closed()
log.info("Starting webhook")
aiogram.utils.executor.start_webhook(
dispatcher=dp,
webhook_path=webhook_path,
on_startup=on_startup,
on_shutdown=on_shutdown,
skip_updates=True,
ssl_context=ssl_context,
port=webhook_port,
)
def main(argv):
# Parse CLI arguments
parser = argparse.ArgumentParser(description="Run RCV Bot server")
parser.add_argument("-c", "--config", default="config.ini", help="Configuration file path")
parser.add_argument("-v", "--verbose", action="store_true", help="Use DEBUG logging")
args = parser.parse_args(argv)
# Set log level
logging.basicConfig(
format="{log_context} {levelname}:{name}:{message}",
style="{",
level=logging.DEBUG if args.verbose else logging.INFO,
)
logging.setLogRecordFactory(ContextLogRecord)
# Load configuration
config = configparser.ConfigParser()
config.read(args.config)
log.info("Setting up webhook bot")
webhook_bot(
config=config,
register_callback=register,
)
if __name__ == "__main__":
main(sys.argv[1:])