mirror of
https://github.com/house-of-vanity/conf_bot.git
synced 2025-07-06 22:34:07 +00:00
Add alert feature.
This commit is contained in:
36
database.py
36
database.py
@ -35,6 +35,14 @@ class DataBase:
|
|||||||
sql = "SELECT id FROM word WHERE word = '%s'" % word
|
sql = "SELECT id FROM word WHERE word = '%s'" % word
|
||||||
return(self.execute(sql)[0][0])
|
return(self.execute(sql)[0][0])
|
||||||
|
|
||||||
|
def get_alert(self):
|
||||||
|
now = dt.datetime.now().strftime("%H%M")
|
||||||
|
sql = "SELECT * FROM alert WHERE time = '%s'" % now
|
||||||
|
alerts = self.execute(sql)
|
||||||
|
sql = "DELETE FROM alert WHERE time = '%s'" % now
|
||||||
|
self.execute(sql)
|
||||||
|
return alerts
|
||||||
|
|
||||||
def add_user(self,
|
def add_user(self,
|
||||||
username,
|
username,
|
||||||
user_id,
|
user_id,
|
||||||
@ -94,6 +102,20 @@ class DataBase:
|
|||||||
)
|
)
|
||||||
self.execute(sql)
|
self.execute(sql)
|
||||||
|
|
||||||
|
def add_alert(self, user_id, conf_id, alert_time, message):
|
||||||
|
date = int(dt.datetime.now().strftime("%s"))
|
||||||
|
sql = """INSERT OR IGNORE INTO
|
||||||
|
alert('conf_id', 'user_id', 'created', 'time', 'message')
|
||||||
|
VALUES ('%s','%s','%s','%s','%s')""" % (
|
||||||
|
conf_id,
|
||||||
|
user_id,
|
||||||
|
date,
|
||||||
|
alert_time,
|
||||||
|
message
|
||||||
|
)
|
||||||
|
print('ALERT: ', sql)
|
||||||
|
self.execute(sql)
|
||||||
|
|
||||||
def get_top(self, user_id, conf_id, limit=10):
|
def get_top(self, user_id, conf_id, limit=10):
|
||||||
sql = """
|
sql = """
|
||||||
SELECT w.word, COUNT(*) as count FROM relations r
|
SELECT w.word, COUNT(*) as count FROM relations r
|
||||||
@ -117,6 +139,20 @@ class DataBase:
|
|||||||
result = self.execute(sql)
|
result = self.execute(sql)
|
||||||
return(result)
|
return(result)
|
||||||
|
|
||||||
|
def all_conf_users(self, 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'
|
||||||
|
""" % (
|
||||||
|
conf_id
|
||||||
|
)
|
||||||
|
result = self.execute(sql)
|
||||||
|
return(result)
|
||||||
|
|
||||||
def here(self, user_id, conf_id):
|
def here(self, user_id, conf_id):
|
||||||
sql = """
|
sql = """
|
||||||
SELECT DISTINCT(u.username) FROM relations r
|
SELECT DISTINCT(u.username) FROM relations r
|
||||||
|
60
worker.py
60
worker.py
@ -1,16 +1,19 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from string import punctuation
|
|
||||||
import subprocess
|
import subprocess
|
||||||
from database import DataBase
|
|
||||||
import os
|
import os
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from urllib.parse import urlencode
|
|
||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
import settings
|
import settings
|
||||||
import re
|
import re
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
|
|
||||||
|
from string import punctuation
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
from database import DataBase
|
||||||
|
|
||||||
from pygments import highlight
|
from pygments import highlight
|
||||||
from pygments.lexers import PythonLexer
|
from pygments.lexers import PythonLexer
|
||||||
@ -26,6 +29,7 @@ class MessageWorker:
|
|||||||
self.telegram_key = settings.parser.get('bot', 'telegram_key')
|
self.telegram_key = settings.parser.get('bot', 'telegram_key')
|
||||||
self.telegram_api = settings.parser.get('bot', 'telegram_api')
|
self.telegram_api = settings.parser.get('bot', 'telegram_api')
|
||||||
self.me = self.getMe()
|
self.me = self.getMe()
|
||||||
|
self.cron_timer()
|
||||||
print("My name is %s" % self.me['result']['username'])
|
print("My name is %s" % self.me['result']['username'])
|
||||||
|
|
||||||
def getMe(self):
|
def getMe(self):
|
||||||
@ -36,6 +40,13 @@ class MessageWorker:
|
|||||||
raw = urllib.request.urlopen(request).read().decode()
|
raw = urllib.request.urlopen(request).read().decode()
|
||||||
return json.loads(raw)
|
return json.loads(raw)
|
||||||
|
|
||||||
|
def isTime(self, string):
|
||||||
|
try:
|
||||||
|
time.strptime(string, '%H%M')
|
||||||
|
return True
|
||||||
|
except ValueError:
|
||||||
|
return False
|
||||||
|
|
||||||
def handleUpdate(self, msg):
|
def handleUpdate(self, msg):
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
@ -122,24 +133,21 @@ class MessageWorker:
|
|||||||
self.send(id=conf_id, msg=msg + ' ```')
|
self.send(id=conf_id, msg=msg + ' ```')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# if '@here' in input_message:
|
if input_message[:6] == '/alert':
|
||||||
# conf_id = msg['message']['chat']['id']
|
conf_id = msg['message']['chat']['id']
|
||||||
# user_id = msg['message']['from']['id']
|
user_id = msg['message']['from']['id']
|
||||||
# chat_title = msg['message']['chat']['title']
|
chat_title = msg['message']['chat']['title']
|
||||||
# self.db.add_conf(conf_id, chat_title)
|
self.db.add_conf(conf_id, chat_title)
|
||||||
# if msg['message']['text'] != '@here':
|
msg = msg['message']['text'].split()[1:]
|
||||||
# message = msg['message']['text'].replace('@here', '\n')
|
alert_time = msg[-1].replace(':', '').replace('.', '').replace(' ', '')
|
||||||
# else:
|
print("Check if it's correct time ", alert_time)
|
||||||
# message = """I summon you!\n"""
|
if self.isTime(alert_time):
|
||||||
|
print("Its correct time")
|
||||||
|
message = " ".join(msg[0:-1])
|
||||||
|
self.db.add_alert(user_id, conf_id, alert_time, message)
|
||||||
|
self.send(id=conf_id, msg='Alert created.')
|
||||||
|
return True
|
||||||
|
|
||||||
# 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
|
|
||||||
if input_message[:5] == '/code':
|
if input_message[:5] == '/code':
|
||||||
conf_id = msg['message']['chat']['id']
|
conf_id = msg['message']['chat']['id']
|
||||||
user_id = msg['message']['from']['id']
|
user_id = msg['message']['from']['id']
|
||||||
@ -249,3 +257,15 @@ class MessageWorker:
|
|||||||
data = {'chat_id': id}
|
data = {'chat_id': id}
|
||||||
files = {'photo': open('code.png', 'rb')}
|
files = {'photo': open('code.png', 'rb')}
|
||||||
r = requests.post(url, files=files, data=data)
|
r = requests.post(url, files=files, data=data)
|
||||||
|
|
||||||
|
def cron_timer(self):
|
||||||
|
alerts = self.db.get_alert()
|
||||||
|
print("Check alerts")
|
||||||
|
for alert in alerts:
|
||||||
|
users = self.db.all_conf_users(conf_id=alert[0])
|
||||||
|
msg = ""
|
||||||
|
for user in users:
|
||||||
|
msg += ' @%s ' % (user[0])
|
||||||
|
msg += "Hey all!\n %s" % alert[4]
|
||||||
|
self.send(id=alert[0], msg=msg)
|
||||||
|
threading.Timer(30, self.cron_timer).start()
|
||||||
|
Reference in New Issue
Block a user