diff --git a/.gitignore b/.gitignore index e69de29..b0d1d33 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,4 @@ +assets/cert* +**/settings.ini +main.db +settings.ini \ No newline at end of file diff --git a/assets/cert.sh b/assets/cert.sh old mode 100644 new mode 100755 diff --git a/assets/main.db.sql b/assets/main.db.sql old mode 100644 new mode 100755 index c2c75da..1e2fcf1 --- a/assets/main.db.sql +++ b/assets/main.db.sql @@ -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`; diff --git a/assets/mystem b/assets/mystem new file mode 100755 index 0000000..75b2040 Binary files /dev/null and b/assets/mystem differ diff --git a/assets/settings.ini.example b/assets/settings.ini.example new file mode 100755 index 0000000..98e9ff4 --- /dev/null +++ b/assets/settings.ini.example @@ -0,0 +1,3 @@ +[bot] +telegram_key = 53004316d:dv0YexIXGHvmNojcD2mUaslKs0 +telegram_api = https://api.telegram.org/ \ No newline at end of file diff --git a/assets/stop-word.ru b/assets/stop-word.ru old mode 100644 new mode 100755 diff --git a/conf-bot.service b/conf-bot.service new file mode 100644 index 0000000..ea967e9 --- /dev/null +++ b/conf-bot.service @@ -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 + + diff --git a/database.py b/database.py old mode 100644 new mode 100755 index 087a298..fb453f3 --- a/database.py +++ b/database.py @@ -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() \ No newline at end of file diff --git a/main.py b/main.py old mode 100644 new mode 100755 index f7fc761..7f655e1 --- a/main.py +++ b/main.py @@ -1,20 +1,20 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -from webhook import WebHook -import settings -import signal -import sys - -# catch ctrl+c -def signal_handler(signal, frame): - print('Exiting...') - settings.db.close() - sys.exit(0) -signal.signal(signal.SIGINT, signal_handler) - -wh = WebHook( - certfile = 'assets/cert.pem', - keyfile='assets/cert.key', - port=8080) - -wh.serve() \ No newline at end of file +#!/usr/bin/python3 +# -*- coding: utf-8 -*- +from webhook import WebHook +import settings +import signal +import sys + +# catch ctrl+c +def signal_handler(signal, frame): + print('Exiting...') + settings.db.close() + sys.exit(0) +signal.signal(signal.SIGINT, signal_handler) + +wh = WebHook( + certfile = 'assets/cert.pem', + keyfile='assets/cert.key', + port=8443) + +wh.serve() diff --git a/settings.py b/settings.py old mode 100644 new mode 100755 diff --git a/webhook.py b/webhook.py old mode 100644 new mode 100755 diff --git a/worker.py b/worker.py old mode 100644 new mode 100755 index 51f192c..99e175f --- a/worker.py +++ b/worker.py @@ -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)