Skip to content

Commit

Permalink
feat: add auto apply whitelist in channel
Browse files Browse the repository at this point in the history
  • Loading branch information
madawei2699 committed Mar 23, 2023
1 parent ba5b85d commit 268ae35
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from app.daily_hot_news import *
from app.gpt import get_answer_from_chatGPT, get_answer_from_llama_file, get_answer_from_llama_web, get_text_from_whisper, get_voice_file_from_text, index_cache_file_dir
from app.slash_command import register_slack_slash_commands
from app.ttl_set import TtlSet
from app.util import md5

class Config:
Expand Down Expand Up @@ -104,10 +105,15 @@ def extract_urls_from_event(event):

whitelist_file = "app/data//vip_whitelist.txt"

filetype_extension_allowed = ['epub', 'pdf', 'text', 'docx', 'markdown', 'm4a', 'webm']
filetype_voice_extension_allowed = ['m4a', 'webm']
filetype_extension_allowed = ['epub', 'pdf', 'text', 'docx', 'markdown', 'm4a', 'webm', 'mp3', 'wav']
filetype_voice_extension_allowed = ['m4a', 'webm', 'mp3', 'wav']
max_file_size = 3 * 1024 * 1024
temp_whitelist_users = TtlSet()
temp_whitelist_channle_id = 'C04VARAS1S7'

def is_authorized(user_id: str) -> bool:
if user_id in temp_whitelist_users:
return True
with open(whitelist_file, "r") as f:
return user_id in f.read().splitlines()

Expand All @@ -130,6 +136,11 @@ def handle_mentions(event, say, logger):
file_md5_name = None
voicemessage = None

# temp whitelist handle
if channel == temp_whitelist_channle_id:
# add 1 hour play time, refresh temp whitelist when user mention bot in temp whitelist channel
temp_whitelist_users.add(user, 60 * 60)

if event.get('files'):
if not is_authorized(event['user']):
say(f'<@{user}>, this feature is only allowed by whitelist user, please contact the admin to open it.', thread_ts=thread_ts)
Expand All @@ -141,6 +152,9 @@ def handle_mentions(event, say, logger):
if filetype not in filetype_extension_allowed:
say(f'<@{user}>, this filetype is not supported, please upload a file with extension [{", ".join(filetype_extension_allowed)}]', thread_ts=thread_ts)
return
if file["size"] > max_file_size:
say(f'<@{user}>, this file size is beyond max file size limit ({max_file_size / 1024 /1024}MB)', thread_ts=thread_ts)
return
url_private = file["url_private"]
temp_file_path = index_cache_file_dir + user
if not os.path.exists(temp_file_path):
Expand Down
34 changes: 34 additions & 0 deletions app/ttl_set.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from threading import Thread, Lock
import time

class TtlSet:
def __init__(self):
self.ttl = {}
self.lock = Lock()
# Start a background thread to periodically remove expired items
t = Thread(target=self._cleaner)
t.daemon = True
t.start()

def __len__(self):
return len(self.ttl)

def __contains__(self, value):
return value in self.ttl

def add(self, value, ttl_seconds):
with self.lock:
self.ttl[value] = time.time() + ttl_seconds

def discard(self, value):
with self.lock:
self.ttl.pop(value, None)

def _cleaner(self):
while True:
with self.lock:
now = time.time()
for k, v in list(self.ttl.items()):
if v < now:
self.ttl.pop(k, None)
time.sleep(1)
117 changes: 117 additions & 0 deletions docs/chatGPT/CDDR.md
Original file line number Diff line number Diff line change
Expand Up @@ -3854,3 +3854,120 @@ generate_audio_file(text, 'output.wav')
```

This function splits the input text into separate Chinese and English sentences and generates the appropriate SSML markup to specify the voice for each language. It then sends a request to the Azure Text-to-Speech API with the SSML data and saves the resulting audio file to the specified output path. Note that to use this code you need to replace `YOUR_RESOURCE_NAME`, `YOUR_ENDPOINT_NAME` and `YOUR_API_KEY` with your own values.

---

## 2023-03-23

---

You
---



does python have a set with ttl? if the time is over, it will auto remove the item

ChatGPT
-------

Python provides a built-in `set` data structure, but it does not have a time-to-live (TTL) feature that automatically removes elements after a specified time period. However, you can use third-party libraries like `ttl` or `expiringdict` to achieve this functionality.

`ttl` is a simple Python library that provides a dictionary-like object with TTL functionality. Here's an example of how you can use it:

```python
from ttl import TTLDict

# create a TTL dictionary with a TTL of 5 seconds
my_dict = TTLDict(ttl=5)

# add an item to the dictionary
my_dict['key'] = 'value'

# wait for 5 seconds
time.sleep(5)

# the item will be automatically removed from the dictionary
print(my_dict.get('key')) # prints None
```

`expiringdict` is another Python library that provides an efficient implementation of a dictionary that automatically deletes entries after a certain amount of time has passed. Here's an example of how you can use it:

```python
from expiringdict import ExpiringDict

# create an ExpiringDict with an expiration time of 5 seconds
my_dict = ExpiringDict(max_len=100, max_age_seconds=5)

# add an item to the dictionary
my_dict['key'] = 'value'

# wait for 5 seconds
time.sleep(5)

# the item will be automatically removed from the dictionary
print(my_dict.get('key')) # prints None
```

Note that both `ttl` and `expiringdict` are third-party libraries that need to be installed separately using `pip`.

I hope this helps! Let me know if you have any further questions.

You
---



dose it have a set?

ChatGPT
-------

Yes, Python has a built-in `set` data type that stores unique elements. However, it does not have a Time to Live (TTL) feature built-in.

To achieve the behavior you described, you may need to build your own custom implementation of a `set` with TTL using the following:

1. Use a dictionary to store the value along with its expiry time as key-value pairs
2. Use a separate thread to continually check for expired elements and remove them from the dictionary.
3. Whenever new element is added update it's TTL if already present.

Here is an example base code to get you started:

```python
from threading import Thread, Lock
import time

class TtlSet:
def __init__(self):
self.ttl = {}
self.lock = Lock()
# Start a background thread to periodically remove expired items
t = Thread(target=self._cleaner)
t.setDaemon(True)
t.start()

def __len__(self):
return len(self.ttl)

def __contains__(self, value):
return value in self.ttl

def add(self, value, ttl_seconds):
with self.lock:
self.ttl[value] = time.time() + ttl_seconds

def discard(self, value):
with self.lock:
self.ttl.pop(value, None)

def _cleaner(self):
while True:
with self.lock:
now = time.time()
for k, v in list(self.ttl.items()):
if v < now:
self.ttl.pop(k, None)
time.sleep(1)
```

You can use this `TtlSet` class by calling the `add()` method (with a TTL value in seconds), and the element will automatically be removed from the set after the specified duration.

0 comments on commit 268ae35

Please sign in to comment.