commit
4ffdb03a24
19
README.md
19
README.md
|
@ -161,6 +161,25 @@ This module is for interacting with the room that the commands are being execute
|
|||
* !room kicked: Lists the kicked users and their provided reason
|
||||
* !room state [event type] [optional state key]: Gets a state event with given event type and optional state key
|
||||
|
||||
### Welcome to Room
|
||||
|
||||
When configured in a room, the bot will monitor a room for new users and send new users a welcome message 1:1. It will then notify bot owners of the new user. It will also, optionally, notify of user departure.
|
||||
|
||||
Commands:
|
||||
|
||||
* !welcome_room welcome_message [message] Sets the welcome message, along with other variables needed to detect new users.
|
||||
* !welcome_room notify_departure [True/False] Sets whether bot owners will be notified of departure from room. Defaults to False.
|
||||
* !welcome_room settings Shows the current settings for the welcome_room module.
|
||||
|
||||
### Welcome to Server
|
||||
|
||||
As a server admin, the bot will monitor new user creation on the server and send the welcome message to new users 1:1. It will then notify bot owners of the new user.
|
||||
|
||||
Commands:
|
||||
|
||||
* !welcome_server welcome_message [message] Sets the welcome message, along with other variables needed to detect new users.
|
||||
* !welcome_server settings Shows current settings for the welcome_server module
|
||||
|
||||
### Slow polling services
|
||||
|
||||
These have the same usage - you can add one or more accounts to a room and bot polls the accounts.
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
import itertools
|
||||
import shlex
|
||||
from modules.common.module import BotModule
|
||||
|
||||
|
||||
class MatrixModule(BotModule):
|
||||
"""
|
||||
Detect new users who join the provided room, DM them a welcome message, and
|
||||
alert bot owners that a new user has been welcomed.
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.enabled = False
|
||||
self.rooms = dict()
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
bot.must_be_owner(event)
|
||||
args = shlex.split(event.body)
|
||||
args.pop(0)
|
||||
# Message body possibilities:
|
||||
# ["welcome_message", "notify_departure", "settings"]
|
||||
if args[0] == "welcome_message":
|
||||
users = bot.client.rooms[room.room_id].users
|
||||
welcome_settings = {
|
||||
"last_room_user_count": len(users),
|
||||
"last_room_users": [username for username in users],
|
||||
"welcome_message": event.body.split("welcome_message", 1)[1],
|
||||
"notify_departure": False
|
||||
}
|
||||
self.rooms[room.room_id] = welcome_settings
|
||||
bot.save_settings()
|
||||
await bot.send_text(room, "Welcome settings configured: {settings}".format(settings=welcome_settings))
|
||||
elif args[0] == "notify_departure":
|
||||
notify_departure = True if args[1] == "True" else False
|
||||
self.rooms[room.room_id]["notify_departure"] = notify_departure
|
||||
bot.save_settings()
|
||||
await bot.send_text(room, "notify_departure set to {setting}".format(setting=notify_departure))
|
||||
elif args[0] == "settings":
|
||||
await bot.send_text(room, "Welcome settings: {settings}".format(settings=self.rooms[room.room_id]))
|
||||
|
||||
def get_settings(self):
|
||||
data = super().get_settings()
|
||||
data["rooms"] = self.rooms
|
||||
return data
|
||||
|
||||
def set_settings(self, data):
|
||||
super().set_settings(data)
|
||||
if data.get("rooms"):
|
||||
self.rooms = data["rooms"]
|
||||
|
||||
async def matrix_poll(self, bot, pollcount):
|
||||
for room_id in self.rooms:
|
||||
if room_id in bot.client.rooms:
|
||||
welcome_parameters = self.rooms[room_id]
|
||||
newcomer_room_users = bot.client.rooms[room_id].users
|
||||
newcomer_room_user_delta = self.get_user_list_delta(
|
||||
newcomer_room_users,
|
||||
welcome_parameters["last_room_users"]
|
||||
)
|
||||
self.rooms[room_id]["last_room_user_count"] = len(newcomer_room_users)
|
||||
self.rooms[room_id]["last_room_users"] = [u for u in newcomer_room_users]
|
||||
|
||||
if pollcount != 1:
|
||||
new_users = newcomer_room_user_delta.get("recently_added", [])
|
||||
if welcome_parameters["notify_departure"] and \
|
||||
len(newcomer_room_user_delta.get("recently_removed")) > 0:
|
||||
for owner in bot.owners:
|
||||
await bot.send_msg(
|
||||
owner,
|
||||
"Welcome Bot",
|
||||
"User {user_left} left {channel}".format(
|
||||
user_left=newcomer_room_user_delta.get("recently_removed"),
|
||||
channel=bot.client.rooms[room_id].display_name
|
||||
)
|
||||
)
|
||||
await self.welcome_users(
|
||||
new_users,
|
||||
welcome_parameters["welcome_message"],
|
||||
bot,
|
||||
bot.client.rooms[room_id].display_name
|
||||
)
|
||||
|
||||
def help(self):
|
||||
return "Poll for new users in the room and welcome them"
|
||||
|
||||
async def welcome_users(self, user_list, message, bot, roomname):
|
||||
for user in user_list:
|
||||
await bot.send_msg(
|
||||
user,
|
||||
"Welcome",
|
||||
message
|
||||
)
|
||||
if len(user_list) > 0:
|
||||
for owner in bot.owners:
|
||||
await bot.send_msg(
|
||||
owner,
|
||||
"Welcome Bot",
|
||||
"Sent a welcome message from {channel} to: {users}".format(
|
||||
users=user_list,
|
||||
channel=roomname
|
||||
)
|
||||
)
|
||||
|
||||
def get_user_list_delta(
|
||||
self,
|
||||
current_user_list,
|
||||
previous_user_list
|
||||
):
|
||||
recently_added = list(itertools.filterfalse(
|
||||
lambda u: u in previous_user_list,
|
||||
current_user_list
|
||||
))
|
||||
recently_removed = list(itertools.filterfalse(
|
||||
lambda u: u in current_user_list,
|
||||
previous_user_list
|
||||
))
|
||||
total_change = len(recently_added) + len(recently_removed)
|
||||
|
||||
return {
|
||||
"total_change": total_change,
|
||||
"recently_removed": recently_removed,
|
||||
"recently_added": recently_added
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
import os
|
||||
import itertools
|
||||
import shlex
|
||||
import requests
|
||||
from modules.common.module import BotModule
|
||||
|
||||
|
||||
class MatrixModule(BotModule):
|
||||
"""
|
||||
Detect new users who join the server, DM them a welcome message, and alert
|
||||
bot owners that a new user has been welcomed.
|
||||
|
||||
Note: This module will only work if the bot is a server admin. This
|
||||
privilege level has risks.
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
super().__init__(name)
|
||||
self.enabled = False
|
||||
self.access_token = os.getenv("MATRIX_ACCESS_TOKEN")
|
||||
self.welcome_settings = dict()
|
||||
|
||||
async def matrix_message(self, bot, room, event):
|
||||
bot.must_be_owner(event)
|
||||
args = shlex.split(event.body)
|
||||
args.pop(0)
|
||||
# Message body possibilities:
|
||||
# ["welcome_message", "query_host", "settings"]
|
||||
if args[0] == "welcome_message":
|
||||
users = self.get_server_user_list()
|
||||
welcome_settings = {
|
||||
"last_server_user_count": len(users),
|
||||
"last_server_users": users,
|
||||
"welcome_message": event.body.split("welcome_message", 1)[1],
|
||||
"user_query_host": os.getenv("MATRIX_SERVER")
|
||||
}
|
||||
self.welcome_settings = welcome_settings
|
||||
bot.save_settings()
|
||||
await bot.send_text(room, "Welcome settings configured for server: {settings}".format(settings=welcome_settings))
|
||||
elif args[0] == "settings":
|
||||
await bot.send_text(room, "Welcome settings for server: {settings}".format(settings=self.welcome_settings))
|
||||
|
||||
def get_settings(self):
|
||||
data = super().get_settings()
|
||||
data["welcome_settings"] = self.welcome_settings
|
||||
return data
|
||||
|
||||
def set_settings(self, data):
|
||||
super().set_settings(data)
|
||||
if data.get("welcome_settings"):
|
||||
self.welcome_settings = data["welcome_settings"]
|
||||
|
||||
async def matrix_poll(self, bot, pollcount):
|
||||
server_user_delta = self.get_server_user_delta(bot)
|
||||
|
||||
# The first time this bot runs it will detect all users as new, so
|
||||
# allow it to one once without taking action.
|
||||
if pollcount != 1:
|
||||
new_users = server_user_delta.get("recently_added", [])
|
||||
await self.welcome_users(
|
||||
new_users,
|
||||
self.welcome_settings["welcome_message"],
|
||||
bot
|
||||
)
|
||||
|
||||
def help(self):
|
||||
return "Poll for new users on the server and welcome them"
|
||||
|
||||
async def welcome_users(self, user_list, message, bot):
|
||||
if len(user_list) > 1:
|
||||
print(user_list)
|
||||
return
|
||||
for user in user_list:
|
||||
await bot.send_msg(
|
||||
user,
|
||||
"Welcome",
|
||||
message
|
||||
)
|
||||
if len(user_list) > 0:
|
||||
for owner in bot.owners:
|
||||
await bot.send_msg(
|
||||
owner,
|
||||
"Welcome Bot",
|
||||
"Sent a welcome message to new server user(s): {users}".format(
|
||||
users=user_list
|
||||
)
|
||||
)
|
||||
|
||||
def get_user_list_delta(
|
||||
self,
|
||||
current_user_list,
|
||||
previous_user_list
|
||||
):
|
||||
recently_added = list(itertools.filterfalse(
|
||||
lambda u: u in previous_user_list,
|
||||
current_user_list
|
||||
))
|
||||
recently_removed = list(itertools.filterfalse(
|
||||
lambda u: u in current_user_list,
|
||||
previous_user_list
|
||||
))
|
||||
total_change = len(recently_added) + len(recently_removed)
|
||||
|
||||
return {
|
||||
"total_change": total_change,
|
||||
"recently_removed": recently_removed,
|
||||
"recently_added": recently_added
|
||||
}
|
||||
|
||||
def get_server_user_delta(self, bot):
|
||||
"""
|
||||
Get the full user list for the server and return the change in users
|
||||
since the last run.
|
||||
"""
|
||||
user_list = self.get_server_user_list()
|
||||
user_delta = self.get_user_list_delta(
|
||||
user_list,
|
||||
self.welcome_settings["last_server_users"]
|
||||
)
|
||||
self.welcome_settings["last_server_users"] = [u for u in user_list]
|
||||
self.welcome_settings["last_server_user_count"] = len(user_list)
|
||||
bot.save_settings()
|
||||
return user_delta
|
||||
|
||||
def get_server_user_list(self):
|
||||
user_data = requests.get(
|
||||
self.welcome_settings["user_query_host"] + "/_synapse/admin/v2/users",
|
||||
headers={"Authorization": "Bearer {token}".format(
|
||||
token=self.access_token
|
||||
)}
|
||||
)
|
||||
user_data_json = user_data.json()
|
||||
user_list = [u.get("name") for u in user_data_json.get("users", [])]
|
||||
return user_list
|
Loading…
Reference in New Issue