-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
groups.py
209 lines (180 loc) · 7.01 KB
/
groups.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
#!/usr/bin/env python
#
# Semaphore: A simple (rule-based) bot library for Signal Private Messenger.
# Copyright (C) 2023 Lazlo Westerhof <semaphore@lazlo.me>
# Copyright (C) 2023 Shrikrishna Singh <krishnasingh.ss30@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
Signal Bot examples, manage groups.
"""
import io
import os
import re
from semaphore import Bot, ChatContext
async def groups_list(ctx: ChatContext) -> None:
"""Get a list of groups."""
groups = await ctx.bot.list_groups()
group_list = []
for group in groups:
group_list.append(f"{group.title}: {group.id}")
groups_list = "\n".join(group_list)
await ctx.message.reply(body=f"groups: \n{groups_list}")
async def group_add_members(ctx: ChatContext) -> None:
"""Add members to a group."""
try:
group_id = ctx.match.group(1)
members = ctx.match.group(2).split(",")
except ValueError:
await ctx.message.reply(
"Usage: !group add-members --groupid <group_id> --members <member_ids>"
)
return
group = await ctx.bot.add_members(group_id, members)
await ctx.message.reply(body=f"Hi {group.member_detail}!")
async def group_remove_members(ctx: ChatContext) -> None:
"""Remove members from a group."""
try:
group_id = ctx.match.group(1)
members = ctx.match.group(2).split(",")
except ValueError:
await ctx.message.reply(
"Usage: !group remove-members --groupid <group_id> --members <member_ids>"
)
return
group = await ctx.bot.remove_members(group_id, members)
await ctx.message.reply(body=f"Hi {group.member_detail}!")
async def create_group(ctx: ChatContext) -> None:
"""Create a signal group."""
try:
group_title = ctx.match.group(1)
members = ctx.match.group(2).split(",")
except ValueError:
await ctx.message.reply(
"Usage: !group create --title <group_title> --members <member_ids>"
)
return
group = await ctx.bot.create_group(group_title, members)
await ctx.message.reply(body=f"Hi {group.title}!")
async def update_group_title(ctx: ChatContext) -> None:
"""Update title of a group."""
try:
group_id = ctx.match.group(1)
group_title = ctx.match.group(2)
except ValueError:
await ctx.message.reply(
"Usage: !group update title --groupid <group_id> --newtitle <newtitlestring>"
)
return
group = await ctx.bot.update_group_title(group_id, group_title)
await ctx.message.reply(body=f"Hi {group.title}!")
async def update_group_timer(ctx: ChatContext) -> None:
"""Update expiration timer of a group."""
try:
group_id = ctx.match.group(1)
group_timer = ctx.match.group(2)
except ValueError:
await ctx.message.reply(
"Usage: !group update timer --groupid <group_id> --timer <intvalueinseconds>"
)
return
group = await ctx.bot.update_group_timer(group_id, group_timer)
await ctx.message.reply(body=f"Hi {group.timer}!")
async def update_group_role(ctx: ChatContext) -> None:
"""Update member role for a group."""
try:
group_id = ctx.match.group(1)
member_id = ctx.match.group(2)
role = ctx.match.group(3)
except ValueError:
await ctx.message.reply(
("Usage: !group update role --groupid <group_id>"
" --memberid <member_id> --role <DEFAULT|ADMINSTRATION>")
)
return
group = await ctx.bot.update_group_role(group_id, member_id, role)
await ctx.message.reply(body=f"Hi {group.member_detail}!")
async def leave_group(ctx: ChatContext) -> None:
"""Leava a group."""
try:
group_id = ctx.match.group(1)
except ValueError:
await ctx.message.reply("Usage: !group leave <group_id>")
return
await ctx.bot.leave_group(group_id)
async def preview_group(ctx: ChatContext) -> None:
"""Preview a group without joining."""
try:
url = ctx.match.group(1)
except ValueError:
await ctx.message.reply("Usage: !group preview <group_id>")
return
group = await ctx.bot.preview_group(url)
await ctx.message.reply(body=f"title: {group.title}!, {group.member_detail}")
async def group_show(ctx: ChatContext) -> None:
"""Get information for a group."""
try:
group_id = ctx.match.group(1)
except ValueError:
await ctx.message.reply("Usage: !group show <group_id>")
return
group = await ctx.bot.get_group(group_id)
menu = io.StringIO()
menu.write(f"id: {group.id}\n")
menu.write(f"title: {group.title}\n")
menu.write(f"announcements: {group.announcements}\n")
menu.write(f"avatar: {group.avatar}\n")
menu.write(f"description: {group.description}\n")
menu.write(f"inviteLink: {group.inviteLink}\n")
menu.write(f"timer: {group.timer}\n")
menu.write(f"members: {group.member_detail}\n")
menu.write(f"requesting_members: {group.requesting_members}")
await ctx.message.reply(menu.getvalue())
async def main() -> None:
"""Start the bot."""
# Connect the bot to number.
async with Bot(os.environ["SIGNAL_PHONE_NUMBER"]) as bot:
bot.register_handler(re.compile("!group list"), groups_list)
bot.register_handler(re.compile("!group show (.*)"), group_show)
bot.register_handler(re.compile("!group leave (.*)"), leave_group)
bot.register_handler(re.compile("!group preview (.*)"), preview_group)
bot.register_handler(
re.compile("!group add-members --groupid (.*) --members (.*)"),
group_add_members
)
bot.register_handler(
re.compile("!group remove-members --groupid (.*) --members (.*)"),
group_remove_members
)
bot.register_handler(
re.compile("!group create --title (.*) --members (.*)"),
create_group
)
bot.register_handler(
re.compile("!group update title --groupid (.*) --newtitle (.*)"),
update_group_title
)
bot.register_handler(
re.compile("!group update timer --groupid (.*) --timer (.*)"),
update_group_timer
)
bot.register_handler(
re.compile("!group update role --groupid (.*) --memberid (.*) --role (.*)"),
update_group_role
)
# Run the bot until you press Ctrl-C.
await bot.start()
if __name__ == '__main__':
import anyio
anyio.run(main)