22 Commits
0.9 ... master

Author SHA1 Message Date
AB
9b853f6809 Improve markov chain text generator. 2020-02-28 10:00:56 +00:00
AB
40364735e0 Improve markov chain text generator. 2020-02-28 09:53:00 +00:00
AB
cded7272d2 Markov chain works with /markov command 2020-02-07 15:31:43 +00:00
AB
9a6bb39440 Markov chain works with /markov command 2020-02-07 14:51:19 +00:00
92686ed13d Add markov chain text generator. 2020-02-07 17:24:10 +03:00
AB
7f1f542be2 Fix logging. Update cert gen script. 2019-10-29 15:12:53 +00:00
AB
e09ffd6fa6 It works again. 2019-10-24 13:17:20 +00:00
a80b915d10 Improve logging. 2019-05-17 14:21:38 +03:00
a6d7903c57 Add +min feature to alert 2019-01-26 14:31:01 +03:00
b990f3eec8 Prettify alert message. 2019-01-10 23:05:54 +03:00
62ad832396 Let's collect more data. 2019-01-09 23:18:19 +03:00
7e7a4e3cc6 Allow speaking in only one chat 2019-01-09 14:55:49 +03:00
7f4d4ced56 Fix @here func. Now uses id instead of username. 2019-01-09 14:39:36 +03:00
e1e6ee1e9d Improve simple responses. 2018-12-28 18:52:37 +03:00
d406ec14b4 Add simple responses. 2018-12-26 16:59:07 +03:00
a86b2c3b8e Restrict @here command. prettify logs. 2018-12-14 21:09:27 +03:00
c04a5a9933 Add alert feature. 2018-11-30 17:15:45 +03:00
e707292465 Add alert feature. 2018-11-30 17:04:25 +03:00
AB
1b9a37c267 adjust stopwords 2018-09-14 07:55:00 +03:00
AB
9806a379f4 Update userinfo with every new message. 2018-09-14 00:08:07 +03:00
AB
2b0b563a16 Got rid with strict username existance check.
Now users w/o username should handled OK.
2018-09-14 00:00:31 +03:00
d71da08eff Update README.md 2018-08-23 12:52:13 +03:00
15 changed files with 1621 additions and 169 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ code.png
*.swp
*.swo
*.allah
.vscode/settings.json

View File

@ -1,4 +1,4 @@
# Full burst ultimate storm revolution conf_bot
# Full burst ultimate storm revolution Telegram Conference Bot
# Create your own spy bot in Telegram ¯\_(ツ)_/¯

View File

@ -1 +1,2 @@
openssl req -newkey rsa:2048 -sha256 -nodes -keyout cert.key -x509 -days 365 -out cert.pem -subj /C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=hexor.ru
openssl req -newkey rsa:2048 -sha256 -nodes -keyout cert.key -x509 -days 3650 -out cert.pem -subj "/C=US/ST=New York/L=Brooklyn/O=Example Brooklyn Company/CN=des-rkn.hexor.ru"

View File

@ -1,43 +1,45 @@
BEGIN TRANSACTION;
-- DROP TABLE IF EXISTS `word`;
CREATE TABLE IF NOT EXISTS `word` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`word` TEXT UNIQUE
CREATE TABLE `word` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`word` TEXT UNIQUE
);
-- DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER NOT NULL UNIQUE,
`username` TEXT NOT NULL,
`first_name` INTEGER NOT NULL,
`last_name` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
CREATE TABLE sqlite_sequence(name,seq);
CREATE TABLE `user` (
`id` INTEGER NOT NULL UNIQUE,
`username` TEXT NOT NULL,
`first_name` INTEGER NOT NULL,
`last_name` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
-- DROP TABLE IF EXISTS `reset`;
CREATE TABLE IF NOT EXISTS `reset` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_id` INTEGER,
`conf_id` INTEGER,
`date` INTEGER,
`relation_id` INTEGER,
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`)
CREATE TABLE `conf` (
`id` NUMERIC NOT NULL UNIQUE,
`title` TEXT,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
-- DROP TABLE IF EXISTS `relations`;
CREATE TABLE IF NOT EXISTS `relations` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`word_id` INTEGER NOT NULL,
`user_id` INTEGER NOT NULL,
`conf_id` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
FOREIGN KEY(`conf_id`) REFERENCES `conf`(`id`),
FOREIGN KEY(`word_id`) REFERENCES `word`(`id`),
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`)
CREATE TABLE IF NOT EXISTS "relations" (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`word_id` INTEGER NOT NULL,
`user_id` INTEGER NOT NULL,
`conf_id` INTEGER NOT NULL,
`date` INTEGER NOT NULL, `msg_id` INTEGER NULL,
FOREIGN KEY(`word_id`) REFERENCES `word`(`id`) ON DELETE CASCADE,
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`),
FOREIGN KEY(`conf_id`) REFERENCES `conf`(`id`)
);
-- DROP TABLE IF EXISTS `conf`;
CREATE TABLE IF NOT EXISTS `conf` (
`id` NUMERIC NOT NULL UNIQUE,
`title` TEXT,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
CREATE TABLE `reset` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_id` INTEGER,
`conf_id` INTEGER,
`date` INTEGER,
`relation_id` INTEGER,
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`)
);
COMMIT;
CREATE TABLE `alert` (
`conf_id`TEXT NOT NULL,
`user_id`TEXT NOT NULL,
`created`TEXT NOT NULL,
`time`TEXT NOT NULL,
`message`TEXT
);
CREATE TABLE `xxx_message` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `text`TEXT UNIQUE NULL);

View File

@ -422,3 +422,6 @@ ThreadTopBot
многочисленные
многочисленный
ага
делать
писать
бот

View File

@ -35,12 +35,38 @@ class DataBase:
sql = "SELECT id FROM word WHERE word = '%s'" % word
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,
username,
user_id,
first_name,
last_name):
date = int(dt.datetime.now().strftime("%s"))
try:
sql = """
UPDATE user
SET
username = '%s',
first_name = '%s',
last_name = '%s'
WHERE
id = '%s'
""" % (
username,
first_name,
last_name,
user_id
)
self.execute(sql)
except:
pass
sql = """INSERT OR IGNORE INTO
user('id', 'username', 'first_name', 'last_name', 'date')
VALUES ('%s','%s','%s','%s','%s')""" % (
@ -63,19 +89,40 @@ class DataBase:
)
self.execute(sql)
def add_relation(self, word, user_id, conf_id):
def add_relation(self, word, user_id, conf_id, text):
word_id = self.save_word(word)
date = int(dt.datetime.now().strftime("%s"))
sql2 = "INSERT OR IGNORE INTO xxx_message('text') VALUES ('%s')" % text
self.execute(sql2)
sql3 = "SELECT id FROM `xxx_message` WHERE text = '%s'" % text
msg_id = self.execute(sql3)[0][0]
sql = """INSERT OR IGNORE INTO
relations('word_id', 'user_id', 'conf_id', 'date')
VALUES ('%s','%s','%s','%s')""" % (
relations('word_id', 'user_id', 'conf_id', 'msg_id', 'date')
VALUES ('%s','%s','%s','%s', '%s')""" % (
word_id,
user_id,
conf_id,
msg_id,
date
)
self.execute(sql)
def add_alert(self, user_id, conf_id, alert_time, message):
date = int(dt.datetime.now().strftime("%s"))
print(alert_time)
if alert_time[0] == '+':
alert_time = (dt.datetime.now() + dt.timedelta(minutes=int(alert_time[1:]))).strftime("%H%M")
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
)
self.execute(sql)
def get_top(self, user_id, conf_id, limit=10):
sql = """
SELECT w.word, COUNT(*) as count FROM relations r
@ -99,9 +146,42 @@ class DataBase:
result = self.execute(sql)
return(result)
def all_conf_users(self, conf_id):
sql = """
SELECT DISTINCT(u.username), u.first_name, u.id 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 get_random_word(self, count=1, like="%"):
sql = "SELECT word FROM word WHERE word LIKE '%s' ORDER BY random() LIMIT %s" % (like, count)
result = self.execute(sql)
return(result)
def get_random_message(self, conf_id=None, count=1):
if not conf_id:
print('get random message from all DB, count %s' % count)
sql = "SELECT text FROM xxx_message ORDER BY RANDOM() LIMIT %s" % count
else:
print('get random message from %s, count: %s' % (conf_id, count))
sql = """SELECT x.text FROM xxx_message x LEFT JOIN relations r ON r.msg_id == x.id
WHERE r.conf_id = '%s' ORDER BY RANDOM() DESC LIMIT %s""" % (conf_id, count)
result = self.execute(sql)
messages = list()
for msg in result:
messages.append(msg[0])
return(messages)
def here(self, user_id, conf_id):
sql = """
SELECT DISTINCT(u.username) FROM relations r
SELECT DISTINCT(u.username), u.id, u.first_name FROM relations r
LEFT JOIN user u
ON u.id = r.user_id
LEFT JOIN conf c

7
markov/__init__.py Normal file
View File

@ -0,0 +1,7 @@
"""Markov chain text generator"""
from .gen import MarkovChain
from .simple import get
__all__ = ["MarkovChain", "get"]

74
markov/gen.py Normal file
View File

@ -0,0 +1,74 @@
from .histograms import Dictogram
import random
from collections import deque
import re
class MarkovChain:
def __init__(self, text=str()):
self.length = 25
self.text = text
self.text_list = None
self._prepare_list()
self.model = self._gen_model()
def _prepare_list(self):
if isinstance(self.text, str):
self._clean_text()
self.text_list = self.text.split()
else:
raise TypeError
def _clean_text(self):
text = self.text.replace('','')
text = self.text.replace('«','')
text = self.text.replace('»','')
text = self.text.replace('(','')
text = self.text.replace(')','')
text = self.text.replace('.','')
text = self.text.replace(',','')
self.text = text
print(self.text)
def _gen_model(self):
data = self.text_list
if data is None:
raise TypeError
markov_model = dict()
for i in range(0, len(data)-1):
if data[i] in markov_model:
markov_model[data[i]].update([data[i+1]])
else:
markov_model[data[i]] = Dictogram([data[i+1]])
return markov_model
def _generate_random_start(self):
model = self.model
# Чтобы сгенерировать любое начальное слово, раскомментируйте строку:
return random.choice(list(model.keys()))
# Чтобы сгенерировать "правильное" начальное слово, используйте код ниже:
# Правильные начальные слова - это те, что являлись началом предложений в корпусе
if 'END' in model:
seed_word = 'END'
while seed_word == 'END':
seed_word = model['END'].return_weighted_random_word()
return seed_word
return random.choice(list(model.keys()))
def generate_random_sentence(self):
markov_model = self.model
length = self.length if len(self.text_list) > self.length else len(self.text_list) - 1
current_word = self._generate_random_start()
sentence = [current_word]
for i in range(0, length):
try:
current_dictogram = markov_model[current_word]
random_weighted_word = current_dictogram.return_weighted_random_word()
current_word = random_weighted_word
sentence.append(current_word)
except KeyError:
pass
sentence[0] = sentence[0].capitalize()
return ' '.join(sentence) + '.'

49
markov/histograms.py Normal file
View File

@ -0,0 +1,49 @@
import random
class Dictogram(dict):
def __init__(self, iterable=None):
# Инициализируем наше распределение как новый объект класса,
# добавляем имеющиеся элементы
super(Dictogram, self).__init__()
self.types = 0 # число уникальных ключей в распределении
self.tokens = 0 # общее количество всех слов в распределении
if iterable:
self.update(iterable)
def update(self, iterable):
# Обновляем распределение элементами из имеющегося
# итерируемого набора данных
for item in iterable:
if item in self:
self[item] += 1
self.tokens += 1
else:
self[item] = 1
self.types += 1
self.tokens += 1
def count(self, item):
# Возвращаем значение счетчика элемента, или 0
if item in self:
return self[item]
return 0
def return_random_word(self):
random_key = random.sample(self, 1)
# Другой способ:
# random.choice(histogram.keys())
return random_key[0]
def return_weighted_random_word(self):
# Сгенерировать псевдослучайное число между 0 и (n-1),
# где n - общее число слов
random_int = random.randint(0, self.tokens-1)
index = 0
list_of_keys = list(self.keys())
# вывести 'случайный индекс:', random_int
for i in range(0, self.types):
index += self[list_of_keys[i]]
# вывести индекс
if(index > random_int):
# вывести list_of_keys[i]
return list_of_keys[i]

90
markov/simple.py Normal file
View File

@ -0,0 +1,90 @@
import random
from collections import deque
import re
class Dictogram(dict):
def __init__(self, iterable=None):
super(Dictogram, self).__init__()
self.types = 0
self.tokens = 0
if iterable:
self.update(iterable)
def update(self, iterable):
for item in iterable:
if item in self:
self[item] += 1
self.tokens += 1
else:
self[item] = 1
self.types += 1
self.tokens += 1
def count(self, item):
if item in self:
return self[item]
return 0
def return_random_word(self):
random_key = random.sample(self, 1)
return random_key[0]
def return_weighted_random_word(self):
random_int = random.randint(0, self.tokens-1)
index = 0
list_of_keys = list(self.keys())
for i in range(0, self.types):
index += self[list_of_keys[i]]
if(index > random_int):
return list_of_keys[i]
def get(text):
def generate_random_start(model):
if 'END' in model:
seed_word = 'END'
while seed_word == 'END':
seed_word = model['END'].return_weighted_random_word()
return seed_word
return random.choice(list(model.keys()))
def generate_random_sentence(length, markov_model):
current_word = generate_random_start(markov_model)
sentence = [current_word]
for i in range(0, length):
try:
current_dictogram = markov_model[current_word]
random_weighted_word = current_dictogram.return_weighted_random_word()
current_word = random_weighted_word
sentence.append(current_word)
except:
break
sentence[0] = sentence[0].capitalize()
return ' '.join(sentence) + '.'
return sentence
def make_markov_model(data):
markov_model = dict()
for i in range(0, len(data)-1):
if data[i] in markov_model:
markov_model[data[i]].update([data[i+1]])
else:
markov_model[data[i]] = Dictogram([data[i+1]])
return markov_model
# simple cleanup
text = text.replace('','')
text = text.replace('«','')
text = text.replace('»','')
text = text.replace('(','')
text = text.replace(')','')
text = "START " + text
text = text.replace('.', ' END')
text_list = text.split()
model = make_markov_model(text_list)
generated = generate_random_sentence(random.randint(5,30), model)
generated = generated.replace(' END', '.')
return generated

76
puller.py Normal file
View File

@ -0,0 +1,76 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import settings
import signal
import sys
#from webhook import WebHook
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# catch ctrl+c
def signal_handler(signal, frame):
print('Exiting...')
settings.db.close()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# Define a few command handlers. These usually take the two arguments bot and
# update. Error handlers also receive the raised TelegramError object in error.
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Hi!')
def help(update, context):
"""Send a message when the command /help is issued."""
update.message.reply_text('Help!')
def echo(update, context):
"""Echo the user message."""
update.message.reply_text(update.message.text)
def error(update, context):
"""Log Errors caused by Updates."""
logger.warning('Update "%s" caused error "%s"', update, context.error)
def main():
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
token = settings.parser.get('bot', 'telegram_key')
updater = Updater(token, use_context=True)
# Get the dispatcher to register handlers
dp = updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
# on noncommand i.e message - echo the message on Telegram
dp.add_handler(MessageHandler(Filters.text, echo))
# log all errors
dp.add_error_handler(error)
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()

87
simple.py Normal file
View File

@ -0,0 +1,87 @@
import random
from collections import deque
import re
class Dictogram(dict):
def __init__(self, iterable=None):
super(Dictogram, self).__init__()
self.types = 0
self.tokens = 0
if iterable:
self.update(iterable)
def update(self, iterable):
for item in iterable:
if item in self:
self[item] += 1
self.tokens += 1
else:
self[item] = 1
self.types += 1
self.tokens += 1
def count(self, item):
if item in self:
return self[item]
return 0
def return_random_word(self):
random_key = random.sample(self, 1)
return random_key[0]
def return_weighted_random_word(self):
random_int = random.randint(0, self.tokens-1)
index = 0
list_of_keys = list(self.keys())
for i in range(0, self.types):
index += self[list_of_keys[i]]
if(index > random_int):
return list_of_keys[i]
def get(text):
def generate_random_start(model):
if 'END' in model:
seed_word = 'END'
while seed_word == 'END':
seed_word = model['END'].return_weighted_random_word()
return seed_word
return random.choice(list(model.keys()))
def generate_random_sentence(length, markov_model):
current_word = generate_random_start(markov_model)
sentence = [current_word]
for i in range(0, length):
current_dictogram = markov_model[current_word]
random_weighted_word = current_dictogram.return_weighted_random_word()
current_word = random_weighted_word
sentence.append(current_word)
sentence[0] = sentence[0].capitalize()
return ' '.join(sentence) + '.'
return sentence
def make_markov_model(data):
markov_model = dict()
for i in range(0, len(data)-1):
if data[i] in markov_model:
markov_model[data[i]].update([data[i+1]])
else:
markov_model[data[i]] = Dictogram([data[i+1]])
return markov_model
# simple cleanup
text = text.replace('','')
text = text.replace('«','')
text = text.replace('»','')
text = text.replace('(','')
text = text.replace(')','')
text = "START " + text
text = text.replace('.', ' END')
text_list = text.split()
model = make_markov_model(text_list)
generated = generate_random_sentence(50, model)
generated = generated.replace(' END', '.')
print(generated)

822
vim-session Normal file
View File

@ -0,0 +1,822 @@
let SessionLoad = 1
if &cp | set nocp | endif
map Q gq
let s:cpo_save=&cpo
set cpo&vim
vmap gx <Plug>NetrwBrowseXVis
nmap gx <Plug>NetrwBrowseX
vnoremap <silent> <Plug>NetrwBrowseXVis :call netrw#BrowseXVis()
nnoremap <silent> <Plug>NetrwBrowseX :call netrw#BrowseX(expand((exists("g:netrw_gx")? g:netrw_gx : '<cfile>')),netrw#CheckIfRemote())
inoremap  u
let &cpo=s:cpo_save
unlet s:cpo_save
set background=dark
set backspace=indent,eol,start
set display=truncate
set fileencodings=ucs-bom,utf-8,default,latin1
set helplang=en
set history=200
set incsearch
set langnoremap
set nolangremap
set nomodeline
set nrformats=bin,hex
set ruler
set runtimepath=~/.vim,/var/lib/vim/addons,/usr/share/vim/vimfiles,/usr/share/vim/vim80,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/after
set scrolloff=5
set showcmd
set suffixes=.bak,~,.swp,.o,.info,.aux,.log,.dvi,.bbl,.blg,.brf,.cb,.ind,.idx,.ilg,.inx,.out,.toc
set ttimeout
set ttimeoutlen=100
set wildignore=*.pyc
set wildmenu
let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0
let v:this_session=expand("<sfile>:p")
silent only
cd ~/repos/conf_bot
if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''
let s:wipebuf = bufnr('%')
endif
set shortmess=aoO
badd +0 puller.py
badd +0 main.py
badd +0 settings.py
badd +0 webhook.py
badd +0 assets/settings.ini
badd +0 worker.py
argglobal
silent! argdel *
$argadd puller.py
set stal=2
edit puller.py
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal autoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=b:#,fb:-
setlocal commentstring=#\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal expandtab
if &filetype != 'python'
setlocal filetype=python
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=tcq
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=^\\s*\\(from\\|import\\)
setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal indentexpr=GetPythonIndent(v:lnum)
setlocal indentkeys=0{,0},:,!^F,o,O,e,<:>,=elif,=except
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=python3complete#Complete
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal noreadonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=4
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=4
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=.py
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'python'
setlocal syntax=python
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 51 - ((50 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
51
normal! 027|
tabedit settings.py
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal autoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=b:#,fb:-
setlocal commentstring=#\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal expandtab
if &filetype != 'python'
setlocal filetype=python
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=tcq
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=^\\s*\\(from\\|import\\)
setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal indentexpr=GetPythonIndent(v:lnum)
setlocal indentkeys=0{,0},:,!^F,o,O,e,<:>,=elif,=except
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=python3complete#Complete
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal noreadonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=4
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=4
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=.py
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'python'
setlocal syntax=python
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 5 - ((4 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
5
normal! 0
tabedit webhook.py
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal autoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=b:#,fb:-
setlocal commentstring=#\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal expandtab
if &filetype != 'python'
setlocal filetype=python
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=tcq
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=^\\s*\\(from\\|import\\)
setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal indentexpr=GetPythonIndent(v:lnum)
setlocal indentkeys=0{,0},:,!^F,o,O,e,<:>,=elif,=except
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=python3complete#Complete
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal noreadonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=4
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=4
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=.py
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'python'
setlocal syntax=python
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 1 - ((0 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
1
normal! 0
tabedit worker.py
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal autoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=b:#,fb:-
setlocal commentstring=#\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal expandtab
if &filetype != 'python'
setlocal filetype=python
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=tcq
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=^\\s*\\(from\\|import\\)
setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal indentexpr=GetPythonIndent(v:lnum)
setlocal indentkeys=0{,0},:,!^F,o,O,e,<:>,=elif,=except
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=python3complete#Complete
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal noreadonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=4
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=4
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=.py
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'python'
setlocal syntax=python
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 31 - ((30 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
31
normal! 09|
tabedit assets/settings.ini
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal noautoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,0#,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=:;
setlocal commentstring=;\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal noexpandtab
if &filetype != 'dosini'
setlocal filetype=dosini
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=croql
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=
setlocal includeexpr=
setlocal indentexpr=
setlocal indentkeys=0{,0},:,0#,!^F,o,O,e
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal noreadonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=8
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=0
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'dosini'
setlocal syntax=dosini
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 3 - ((2 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
3
normal! 040|
tabedit main.py
set splitbelow splitright
set nosplitbelow
set nosplitright
wincmd t
set winminheight=1 winheight=1 winminwidth=1 winwidth=1
argglobal
setlocal keymap=
setlocal noarabic
setlocal autoindent
setlocal backupcopy=
setlocal balloonexpr=
setlocal nobinary
setlocal nobreakindent
setlocal breakindentopt=
setlocal bufhidden=
setlocal buflisted
setlocal buftype=
setlocal nocindent
setlocal cinkeys=0{,0},0),:,!^F,o,O,e
setlocal cinoptions=
setlocal cinwords=if,else,while,do,for,switch
setlocal colorcolumn=
setlocal comments=b:#,fb:-
setlocal commentstring=#\ %s
setlocal complete=.,w,b,u,t,i
setlocal concealcursor=
setlocal conceallevel=0
setlocal completefunc=
setlocal nocopyindent
setlocal cryptmethod=
setlocal nocursorbind
setlocal nocursorcolumn
setlocal nocursorline
setlocal define=
setlocal dictionary=
setlocal nodiff
setlocal equalprg=
setlocal errorformat=
setlocal expandtab
if &filetype != 'python'
setlocal filetype=python
endif
setlocal fixendofline
setlocal foldcolumn=0
setlocal foldenable
setlocal foldexpr=0
setlocal foldignore=#
setlocal foldlevel=0
setlocal foldmarker={{{,}}}
setlocal foldmethod=manual
setlocal foldminlines=1
setlocal foldnestmax=20
setlocal foldtext=foldtext()
setlocal formatexpr=
setlocal formatoptions=tcq
setlocal formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*
setlocal formatprg=
setlocal grepprg=
setlocal iminsert=0
setlocal imsearch=-1
setlocal include=^\\s*\\(from\\|import\\)
setlocal includeexpr=substitute(v:fname,'\\.','/','g')
setlocal indentexpr=GetPythonIndent(v:lnum)
setlocal indentkeys=0{,0},:,!^F,o,O,e,<:>,=elif,=except
setlocal noinfercase
setlocal iskeyword=@,48-57,_,192-255
setlocal keywordprg=
setlocal nolinebreak
setlocal nolisp
setlocal lispwords=
setlocal nolist
setlocal makeencoding=
setlocal makeprg=
setlocal matchpairs=(:),{:},[:]
setlocal nomodeline
setlocal modifiable
setlocal nrformats=bin,hex
setlocal nonumber
setlocal numberwidth=4
setlocal omnifunc=python3complete#Complete
setlocal path=
setlocal nopreserveindent
setlocal nopreviewwindow
setlocal quoteescape=\\
setlocal readonly
setlocal norelativenumber
setlocal norightleft
setlocal rightleftcmd=search
setlocal noscrollbind
setlocal shiftwidth=4
setlocal noshortname
setlocal signcolumn=auto
setlocal nosmartindent
setlocal softtabstop=4
setlocal nospell
setlocal spellcapcheck=[.?!]\\_[\\])'\"\ \ ]\\+
setlocal spellfile=
setlocal spelllang=en
setlocal statusline=
setlocal suffixesadd=.py
setlocal swapfile
setlocal synmaxcol=3000
if &syntax != 'python'
setlocal syntax=python
endif
setlocal tabstop=8
setlocal tagcase=
setlocal tags=
setlocal termkey=
setlocal termsize=
setlocal textwidth=0
setlocal thesaurus=
setlocal noundofile
setlocal undolevels=-123456
setlocal nowinfixheight
setlocal nowinfixwidth
setlocal wrap
setlocal wrapmargin=0
silent! normal! zE
let s:l = 1 - ((0 * winheight(0) + 51) / 102)
if s:l < 1 | let s:l = 1 | endif
exe s:l
normal! zt
1
normal! 0
tabnext 2
set stal=1
if exists('s:wipebuf')
silent exe 'bwipe ' . s:wipebuf
endif
unlet! s:wipebuf
set winheight=1 winwidth=20 shortmess=filnxtToO
set winminheight=1 winminwidth=1
let s:sx = expand("<sfile>:p:r")."x.vim"
if file_readable(s:sx)
exe "source " . fnameescape(s:sx)
endif
let &so = s:so_save | let &siso = s:siso_save
doautoall SessionLoadPost
unlet SessionLoad
" vim: set ft=vim :

View File

@ -41,7 +41,7 @@ class WebHook:
def __init__(self,
certfile,
keyfile,
address='0.0.0.0',
address=settings.parser.get('bot', 'bind_address'),
port=8443,
RequestHandler=RequestHandler):
@ -56,6 +56,8 @@ class WebHook:
self.httpd.serve_forever()
except KeyboardInterrupt:
pass
except Exception as e:
print("ERROR: %s" % e)
finally:
# Clean-up server (close socket, etc.)
self.httpd.server_close()

406
worker.py
View File

@ -1,16 +1,21 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from string import punctuation
import subprocess
from database import DataBase
import os
import urllib.request
from urllib.parse import urlencode
import requests
import json
import settings
import re
import time
import threading
import random
from string import punctuation
from urllib.parse import urlencode
from database import DataBase
from markov.simple import get
from pygments import highlight
from pygments.lexers import PythonLexer
@ -26,6 +31,7 @@ class MessageWorker:
self.telegram_key = settings.parser.get('bot', 'telegram_key')
self.telegram_api = settings.parser.get('bot', 'telegram_api')
self.me = self.getMe()
self.cron_timer()
print("My name is %s" % self.me['result']['username'])
def getMe(self):
@ -36,141 +42,275 @@ class MessageWorker:
raw = urllib.request.urlopen(request).read().decode()
return json.loads(raw)
def isTime(self, string):
try:
time.strptime(string, '%H%M')
return string
except ValueError:
pass
try:
if (int(string[1:]) > 0) and (string[0] == '+') and (len(string[1:]) < 4):
return string
except:
pass
return False
def colorize(self, code):
code_tag = code[code.rfind('#') + 1:]
try:
lexer = get_lexer_by_name(code_tag)
code = code[:code.rfind('#')]
except:
lexer = guess_lexer(code)
print("Lexer is defined as %s" % lexer)
if lexer.name == 'Text only':
lexer = get_lexer_by_name('python')
print(highlight(code, lexer, ImageFormatter(
font_size=16,
line_number_bg="#242e0c",
line_number_fg="#faddf2",
line_number_bold=True,
font_name='DejaVuSansMono',
style=get_style_by_name('monokai')), outfile="code.png"))
def handleUpdate(self, msg):
try:
try:
input_message = msg['message']['text']
if ('@here' in input_message) or (' @'+self.me['result']['username'] in input_message):
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)
if msg['message']['text'] != '@here':
message = msg['message']['text'].replace('@here', '\n').replace(' @'+self.me['result']['username'], '\n')
else:
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
input_message = msg['message']['text'].replace(
'@' + self.me['result']['username'], '')
except:
input_message = msg['message']['text']
if input_message == '/scheme':
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)
self.send(id=conf_id, msg='```\n' + self.db.scheme + '\n```')
except KeyError as excp:
print(f"KeyError: {excp}")
return True
if input_message == '/stat':
if input_message == '/help':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
chat_title = msg['message']['chat']['title']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
msg = """
Commands:
@here - call all conf users
/stat - show user stat
/scheme - print sql schema
/reset - reset user stat
/sql - execute sql
/alert - set alert
/code - highlight code snippet
"""
self.send(id=conf_id, msg=msg)
if ('@here' in input_message) or (' @'+self.me['result']['username'] in input_message):
# if str(msg['message']['chat']['id']) != "-1001233797421":
# print("@here isn't available for '%s' (%s)" % (msg['message']['chat']['title'], msg['message']['chat']['id']))
# return
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
if msg['message']['text'] != '@here':
message = msg['message']['text'].replace('@here', '\n').replace(' @'+self.me['result']['username'], '\n')
else:
message = """I summon you!\n"""
message = """Here is your top:\n"""
top = self.db.get_top(
users = self.db.here(
user_id=user_id,
conf_id=conf_id
)
for word in top:
message += '*%s*: %s\n' % (word[1], word[0])
for user in users:
message += ' [%s](tg://user?id=%s)' % (user[2], user[1])
self.send(id=conf_id, msg=message)
return True
if input_message == '/reset':
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 = """Your stat has been resetted."""
self.db.reset(
conf_id=conf_id,
user_id=user_id)
return True
if input_message[:4] == '/sql':
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)
sql = msg['message']['text'][5:]
res = self.db.command(sql)
if 'syntax' in str(res) \
or 'ambiguous' in str(res):
self.send(id=conf_id, msg=str(res))
return False
try:
msg = '```\n'
for z in res:
for i in z:
msg = msg + str(i) + '\t'
msg = msg + '\n'
except:
msg = res
self.send(id=conf_id, msg=msg + ' ```')
return True
# if '@here' in input_message:
input_message = msg['message']['text'].replace(
'@' + self.me['result']['username'], '')
except:
input_message = msg['message']['text']
# if str(msg['message']['chat']['id']) == "-1001233797421":
# if random.randint(0,300) == 1:
# 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)
# if msg['message']['text'] != '@here':
# message = msg['message']['text'].replace('@here', '\n')
# if msg['message']['chat']['type'] == 'private':
# chat_title = conf_id
# else:
# 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
if input_message[:5] == '/code':
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)
# word_aya = self.db.get_random_word(count=1, like="%ая")
# word_da = self.db.get_random_word(count=1, like="%да")
# msg = "Ты %s %s." % (word_aya[0][0], word_da[0][0])
# self.send(id=conf_id, msg=msg)
# if (input_message[0] == 'я') or (input_message[0] == 'Я'):
# if len(input_message) > 3:
# conf_id = msg['message']['chat']['id']
# user_id = msg['message']['from']['id']
# if msg['message']['chat']['type'] == 'private':
# chat_title = conf_id
# else:
# chat_title = msg['message']['chat']['title']
# self.db.add_conf(conf_id, chat_title)
# answers = ['И чо бля?','Да и похуй.','Ну и хуй с тобой.','Нет я.']
# if random.randint(0,100) > 80:
# msg = answers[random.randint(0,len(answers)-1)]
# self.send(id=conf_id, msg=msg)
# if (input_message[0:1] == 'Ты') or (input_message[0:1] == 'ты'):
# if len(input_message) > 5:
# conf_id = msg['message']['chat']['id']
# user_id = msg['message']['from']['id']
# if msg['message']['chat']['type'] == 'private':
# chat_title = conf_id
# else:
# chat_title = msg['message']['chat']['title']
# self.db.add_conf(conf_id, chat_title)
# answers = ['Двачую.','Да.', 'А я покакал.', "Винда лучше."]
# if random.randint(0,100) > 70:
# msg = answers[random.randint(0,len(answers)-1)]
# self.send(id=conf_id, msg=msg)
if input_message == '/scheme':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
if len(msg['message']['text'][6:]) < 10000:
code = msg['message']['text'][6:]
code_tag = code[code.rfind('#') + 1:]
try:
lexer = get_lexer_by_name(code_tag)
code = code[:code.rfind('#')]
print("Lexer is defined as %s" % lexer)
except:
lexer = guess_lexer(code)
print("lexer is %s" % lexer)
if lexer.name == 'Text only':
lexer = get_lexer_by_name('python')
highlight(code, lexer, ImageFormatter(
font_size=16,
line_number_bg="#242e0c",
line_number_fg="#faddf2",
line_number_bold=True,
font_name='DejaVuSansMono',
style=get_style_by_name('monokai')), outfile="code.png")
self.send_img(conf_id)
return True
except:
return False
self.db.add_conf(conf_id, chat_title)
self.send(id=conf_id, msg='```\n' + self.db.scheme + '\n```')
return True
if input_message[:7] == '/markov':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
max_sen = 100
try:
count = int(msg['message']['text'][8:])
if count > max_sen:
count = max_sen
except:
count = 5
try:
use_all = bool(msg['message']['text'][8:])
except:
use_all = False
if use_all:
rand_messages = self.db.get_random_message(count=count)
else:
rand_messages = self.db.get_random_message(conf_id, count=count)
print(count, rand_messages)
rand_text = " ".join(rand_messages)
gen_text = get(rand_text)
try:
gen_text = gen_text.lower().capitalize()
except:
pass
self.send(id=conf_id, msg=f'{gen_text}')
return True
if input_message == '/stat':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
message = """Here is your top:\n"""
top = self.db.get_top(
user_id=user_id,
conf_id=conf_id
)
for word in top:
message += '*%s*: %s\n' % (word[1], word[0])
self.send(id=conf_id, msg=message)
return True
if input_message == '/reset':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
message = """Your stat has been resetted."""
self.db.reset(
conf_id=conf_id,
user_id=user_id)
return True
if input_message[:4] == '/sql':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
sql = msg['message']['text'][5:]
res = self.db.command(sql)
if 'syntax' in str(res) \
or 'ambiguous' in str(res):
self.send(id=conf_id, msg=str(res))
return False
try:
msg = '```\n'
for z in res:
for i in z:
msg = msg + str(i) + '\t'
msg = msg + '\n'
except:
msg = res
self.send(id=conf_id, msg=msg + ' ```')
return True
if input_message[:6] == '/alert':
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
msg = msg['message']['text'].split()[1:]
alert_time = msg[-1].replace(':', '').replace('.', '').replace(' ', '')
if self.isTime(alert_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
if input_message[:5] == '/code':
# codefunc
conf_id = msg['message']['chat']['id']
user_id = msg['message']['from']['id']
if msg['message']['chat']['type'] == 'private':
chat_title = conf_id
else:
chat_title = msg['message']['chat']['title']
self.db.add_conf(conf_id, chat_title)
if len(msg['message']['text'][6:]) < 10000:
try:
self.colorize(msg['message']['text'][6:])
except Exception as e:
print(e)
self.send_img(conf_id)
return True
try:
text = msg['message']['text']
username = msg['message']['from']['username']
try:
username = msg['message']['from']['username']
except:
username = "_null"
try:
last_name = msg['message']['from']['last_name']
except:
@ -181,10 +321,13 @@ class MessageWorker:
first_name = '_null'
user_id = msg['message']['from']['id']
chat_id = msg['message']['chat']['id']
chat_title = msg['message']['chat']['title']
if msg['message']['chat']['type'] == 'private':
chat_title = chat_id
else:
chat_title = msg['message']['chat']['title']
except:
return False
print("[%s] (%s) %s %s %s: %s" % (chat_title, user_id, username, first_name, last_name, text))
collection = self.clean_text(text)
self.db.add_user(username,
@ -195,7 +338,7 @@ class MessageWorker:
self.db.add_conf(chat_id, chat_title)
for word in collection:
self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id)
self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id, text=text)
def clean_text(self, s):
file = open(self.stop_words, 'rt')
@ -203,7 +346,6 @@ class MessageWorker:
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)
@ -227,13 +369,13 @@ class MessageWorker:
pass
return collection
def send(self, id, msg):
def send(self, id, msg, parse_mode='Markdown'):
# print(msg)
url = self.telegram_api + 'bot' + self.telegram_key + '/sendMessage'
post_fields = {
'text': msg,
'chat_id': id,
'parse_mode': 'Markdown',
'parse_mode': parse_mode,
'disable_web_page_preview': 1
}
urllib.request.Request(url, urlencode(post_fields).encode())
@ -242,7 +384,23 @@ class MessageWorker:
return json
def send_img(self, id):
print('Going to send code.png to %s' % id)
try:
url = self.telegram_api + 'bot' + self.telegram_key + '/sendPhoto'
data = {'chat_id': id}
files = {'photo': open('code.png', 'rb')}
r = requests.post(url, files=files, data=data)
#print(r)
except Exception as e:
print(e)
def cron_timer(self):
alerts = self.db.get_alert()
for alert in alerts:
users = self.db.all_conf_users(conf_id=alert[0])
msg = ""
for user in users:
msg += ' [%s](tg://user?id=%s) ' % (user[1], user[2])
msg += "\nHey all!\n%s" % alert[4]
self.send(id=alert[0], msg=msg)
threading.Timer(30, self.cron_timer).start()