a script to populate databases, and a command to see chat id

This commit is contained in:
Von Random 2023-11-01 02:01:19 +00:00
parent 7d48ffa35c
commit 54be2516fe
2 changed files with 39 additions and 0 deletions

View file

@ -84,6 +84,8 @@ class Commander:
response = 'failure' response = 'failure'
elif command.startswith('/adduser '): elif command.startswith('/adduser '):
self.__add_user(sender.id, command[9:]) self.__add_user(sender.id, command[9:])
elif command == '/chat':
response = str(event.message.peer_id)
elif command == '/list': elif command == '/list':
response = ', '.join(self.available_tokens) response = ', '.join(self.available_tokens)
elif command == '/users': elif command == '/users':

37
populate Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env python3
import psycopg
import yaml
import sys
with open('config.yml', 'r', encoding='UTF-8') as data:
config = yaml.safe_load(data.read())
with open(config['response_tokens'], 'r', encoding='UTF-8') as data:
valid_tokens = {item for item in yaml.safe_load(data.read())}
with open(sys.argv[1], 'r', encoding='UTF-8') as data:
phrases = yaml.safe_load(data.read())
with psycopg.connect(config['db_spec']) as conn:
query_phrases = 'INSERT INTO responses (tokens, response) VALUES (%s, %s)'
with conn.cursor() as cur:
for regexref, responses in phrases.items():
tokens = set(regexref.split(','))
if tokens != tokens & valid_tokens:
print(f'{str(tokens)} failed to add!')
continue
token_string = ','.join(sorted(tokens))
for response in responses:
cur.execute('SAVEPOINT sp1')
try:
cur.execute( query_phrases, (token_string, response))
except psycopg.errors.UniqueViolation as err:
cur.execute('ROLLBACK TO SAVEPOINT sp1')
# print(err)
continue
cur.execute('RELEASE SAVEPOINT sp1')
conn.commit()
#for item in names:
# usernames = names[item]
# for username in usernames:
# cur.execute('INSERT INTO names (tg_id, name) VALUES (%s, %s)',
# (item, username))
#conn.commit()