This commit is contained in:
Alexandr Godless
2018-02-19 17:22:55 +03:00
parent 97ed251351
commit 4feaaa4a20
12 changed files with 79 additions and 21 deletions

4
.gitignore vendored
View File

@ -0,0 +1,4 @@
assets/cert*
**/settings.ini
main.db
settings.ini

0
assets/cert.sh Normal file → Executable file
View File

3
assets/main.db.sql Normal file → Executable file
View File

@ -2,6 +2,7 @@ BEGIN TRANSACTION;
-- DROP TABLE IF EXISTS `word`;
CREATE TABLE IF NOT EXISTS `word` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`is_russian` INTEGER NOT NULL,
`word` TEXT UNIQUE
);
-- DROP TABLE IF EXISTS `user`;
@ -21,7 +22,7 @@ CREATE TABLE IF NOT EXISTS `relations` (
`conf_id` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
FOREIGN KEY(`conf_id`) REFERENCES `conf`(`id`),
FOREIGN KEY(`word_id`) REFERENCES `word`(`id`) ON DELETE CASCADE,
FOREIGN KEY(`word_id`) REFERENCES `word`(`id`),
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`)
);
-- DROP TABLE IF EXISTS `conf`;

BIN
assets/mystem Executable file

Binary file not shown.

3
assets/settings.ini.example Executable file
View File

@ -0,0 +1,3 @@
[bot]
telegram_key = 53004316d:dv0YexIXGHvmNojcD2mUaslKs0
telegram_api = https://api.telegram.org/

0
assets/stop-word.ru Normal file → Executable file
View File

17
conf-bot.service Normal file
View File

@ -0,0 +1,17 @@
[Unit]
Description=Telegram spy bot.
[Service]
Type=simple
User=ab
Group=ab
Restart=always
RestartSec=5
StartLimitInterval=60s
StartLimitBurst=3
WorkingDirectory=/var/lib/conf_bot
ExecStart=/usr/bin/python3 /var/lib/conf_bot/main.py
ExecStop=killall -TERM /usr/bin/python3 /var/lib/conf_bot/main.py
[Install]
WantedBy=multi-user.target

15
database.py Normal file → Executable file
View File

@ -91,6 +91,21 @@ class DataBase:
result = self.execute(sql)
return(result)
def here(self, user_id, conf_id):
sql= """
select distinct(u.username) from relations r
left join user u
on u.id = r.user_id
left join conf c on r.conf_id = c.id
where c.id = '%s' and
u.id != '%s'
""" % (
conf_id,
user_id
)
result = self.execute(sql)
return(result)
def close(self):
self.conn.close()

4
main.py Normal file → Executable file
View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from webhook import WebHook
import settings
@ -15,6 +15,6 @@ signal.signal(signal.SIGINT, signal_handler)
wh = WebHook(
certfile = 'assets/cert.pem',
keyfile='assets/cert.key',
port=8080)
port=8443)
wh.serve()

0
settings.py Normal file → Executable file
View File

0
webhook.py Normal file → Executable file
View File

18
worker.py Normal file → Executable file
View File

@ -8,6 +8,7 @@ import os
import urllib.request
from urllib.parse import urlencode
import settings
import re
class MessageWorker:
def __init__(self, db, stop_words = 'assets/stop-word.ru'):
@ -31,6 +32,21 @@ class MessageWorker:
message += '*%s*: %s\n' % (word[1], word[0])
self.send(id=conf_id, msg=message)
return True
if msg['message']['text'] == '@here':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
message = """I summon you!\n"""
users = self.db.here(
user_id=user_id,
conf_id=conf_id
)
for user in users:
message += '@%s ' % (user[0])
self.send(id=conf_id, msg=message)
return True
except:
return False
try:
@ -68,6 +84,8 @@ class MessageWorker:
file = open(self.stop_words, 'rt')
sw = file.read().split('\n')
file.close()
s = re.sub(r'(https?:\/\/)?([\da-z\.-]+)\.([\/\w\.-]*)*\/?\S','',s,flags=re.MULTILINE)
print(s)
# dirty hack with dat fucking file
fh = open("tmp.txt","w")
fh.write(s)