2018-02-04 07:13:09 +03:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
|
import os
|
|
|
|
|
import urllib.request
|
2018-07-27 15:20:40 +02:00
|
|
|
|
import requests
|
2018-03-26 16:09:06 +02:00
|
|
|
|
import json
|
2018-02-04 07:13:09 +03:00
|
|
|
|
import settings
|
2018-02-19 17:22:55 +03:00
|
|
|
|
import re
|
2018-11-30 17:04:25 +03:00
|
|
|
|
import time
|
|
|
|
|
import threading
|
2018-12-26 16:59:07 +03:00
|
|
|
|
import random
|
2018-11-30 17:04:25 +03:00
|
|
|
|
|
|
|
|
|
from string import punctuation
|
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
|
from database import DataBase
|
2020-02-07 14:51:19 +00:00
|
|
|
|
from markov.simple import get
|
2018-02-04 07:13:09 +03:00
|
|
|
|
|
2018-07-27 15:20:40 +02:00
|
|
|
|
from pygments import highlight
|
|
|
|
|
from pygments.lexers import PythonLexer
|
2018-07-30 15:28:06 +02:00
|
|
|
|
from pygments.lexers import guess_lexer, get_lexer_by_name
|
2018-07-30 19:02:59 +02:00
|
|
|
|
from pygments.styles import get_style_by_name
|
2018-07-27 15:20:40 +02:00
|
|
|
|
from pygments.formatters import ImageFormatter
|
|
|
|
|
|
2018-08-03 15:15:35 +03:00
|
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
|
class MessageWorker:
|
2018-08-03 15:15:35 +03:00
|
|
|
|
def __init__(self, db, stop_words='assets/stop-word.ru', settings=settings):
|
2018-02-04 07:13:09 +03:00
|
|
|
|
self.stop_words = stop_words
|
|
|
|
|
self.db = db
|
2018-03-26 16:09:06 +02:00
|
|
|
|
self.telegram_key = settings.parser.get('bot', 'telegram_key')
|
|
|
|
|
self.telegram_api = settings.parser.get('bot', 'telegram_api')
|
|
|
|
|
self.me = self.getMe()
|
2018-11-30 17:04:25 +03:00
|
|
|
|
self.cron_timer()
|
2018-03-26 16:09:06 +02:00
|
|
|
|
print("My name is %s" % self.me['result']['username'])
|
|
|
|
|
|
|
|
|
|
def getMe(self):
|
2018-08-03 15:15:35 +03:00
|
|
|
|
url = self.telegram_api + 'bot' + self.telegram_key + '/getMe'
|
2018-03-26 16:09:06 +02:00
|
|
|
|
print(url)
|
|
|
|
|
urllib.request.Request(url)
|
|
|
|
|
request = urllib.request.Request(url)
|
|
|
|
|
raw = urllib.request.urlopen(request).read().decode()
|
|
|
|
|
return json.loads(raw)
|
2018-02-04 07:13:09 +03:00
|
|
|
|
|
2018-11-30 17:04:25 +03:00
|
|
|
|
def isTime(self, string):
|
|
|
|
|
try:
|
|
|
|
|
time.strptime(string, '%H%M')
|
2019-01-26 14:31:01 +03:00
|
|
|
|
return string
|
2018-11-30 17:04:25 +03:00
|
|
|
|
except ValueError:
|
2019-01-26 14:31:01 +03:00
|
|
|
|
pass
|
|
|
|
|
try:
|
2019-05-17 14:21:38 +03:00
|
|
|
|
if (int(string[1:]) > 0) and (string[0] == '+') and (len(string[1:]) < 4):
|
2019-01-26 14:31:01 +03:00
|
|
|
|
return string
|
|
|
|
|
except:
|
|
|
|
|
pass
|
|
|
|
|
return False
|
|
|
|
|
|
2018-11-30 17:04:25 +03:00
|
|
|
|
|
2019-01-09 14:39:36 +03:00
|
|
|
|
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')
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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"))
|
|
|
|
|
|
2019-01-09 14:39:36 +03:00
|
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
|
def handleUpdate(self, msg):
|
|
|
|
|
try:
|
2019-10-29 15:12:53 +00:00
|
|
|
|
try:
|
|
|
|
|
input_message = msg['message']['text']
|
|
|
|
|
except KeyError as excp:
|
|
|
|
|
print(f"KeyError: {excp}")
|
|
|
|
|
return True
|
2019-10-24 13:17:20 +00:00
|
|
|
|
if input_message == '/help':
|
2018-02-26 19:13:04 +01:00
|
|
|
|
conf_id = msg['message']['chat']['id']
|
|
|
|
|
user_id = msg['message']['from']['id']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
if msg['message']['chat']['type'] == 'private':
|
|
|
|
|
chat_title = conf_id
|
|
|
|
|
else:
|
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2018-02-26 19:13:04 +01:00
|
|
|
|
self.db.add_conf(conf_id, chat_title)
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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):
|
2019-10-29 15:12:53 +00:00
|
|
|
|
# if str(msg['message']['chat']['id']) != "-1001233797421":
|
|
|
|
|
# print("@here isn't available for '%s' (%s)" % (msg['message']['chat']['title'], msg['message']['chat']['id']))
|
|
|
|
|
# return
|
2018-02-04 07:13:09 +03:00
|
|
|
|
conf_id = msg['message']['chat']['id']
|
|
|
|
|
user_id = msg['message']['from']['id']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
if msg['message']['chat']['type'] == 'private':
|
|
|
|
|
chat_title = conf_id
|
|
|
|
|
else:
|
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2018-02-04 07:13:09 +03:00
|
|
|
|
self.db.add_conf(conf_id, chat_title)
|
2019-10-24 13:17:20 +00:00
|
|
|
|
if msg['message']['text'] != '@here':
|
|
|
|
|
message = msg['message']['text'].replace('@here', '\n').replace(' @'+self.me['result']['username'], '\n')
|
|
|
|
|
else:
|
|
|
|
|
message = """I summon you!\n"""
|
2018-02-04 07:13:09 +03:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
users = self.db.here(
|
2018-02-04 07:13:09 +03:00
|
|
|
|
user_id=user_id,
|
|
|
|
|
conf_id=conf_id
|
|
|
|
|
)
|
2019-10-24 13:17:20 +00:00
|
|
|
|
for user in users:
|
|
|
|
|
message += ' [%s](tg://user?id=%s)' % (user[2], user[1])
|
2018-02-04 07:13:09 +03:00
|
|
|
|
self.send(id=conf_id, msg=message)
|
|
|
|
|
return True
|
2019-10-24 13:17:20 +00:00
|
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
# 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)
|
|
|
|
|
# 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)
|
|
|
|
|
self.send(id=conf_id, msg='```\n' + self.db.scheme + '\n```')
|
|
|
|
|
return True
|
2018-02-19 18:24:38 +03:00
|
|
|
|
|
2020-02-07 14:51:19 +00:00
|
|
|
|
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:
|
2020-02-07 15:31:43 +00:00
|
|
|
|
count = 5
|
2020-02-28 09:53:00 +00:00
|
|
|
|
try:
|
|
|
|
|
use_all = bool(msg['message']['text'][8:])
|
|
|
|
|
except:
|
|
|
|
|
use_all = False
|
|
|
|
|
if use_all:
|
2020-02-28 10:00:56 +00:00
|
|
|
|
rand_messages = self.db.get_random_message(count=count)
|
2020-02-28 09:53:00 +00:00
|
|
|
|
else:
|
2020-02-28 10:00:56 +00:00
|
|
|
|
rand_messages = self.db.get_random_message(conf_id, count=count)
|
|
|
|
|
print(count, rand_messages)
|
2020-02-07 14:51:19 +00:00
|
|
|
|
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
|
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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:
|
2018-02-19 18:24:38 +03:00
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
self.db.add_conf(conf_id, chat_title)
|
2018-02-19 18:24:38 +03:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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
|
2018-02-19 18:24:38 +03:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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:
|
2018-02-26 15:50:32 +01:00
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
self.db.add_conf(conf_id, chat_title)
|
2018-02-26 15:50:32 +01:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
message = """Your stat has been resetted."""
|
|
|
|
|
self.db.reset(
|
|
|
|
|
conf_id=conf_id,
|
|
|
|
|
user_id=user_id)
|
|
|
|
|
return True
|
2018-02-26 15:50:32 +01:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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:
|
2018-11-30 17:04:25 +03:00
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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
|
2018-11-30 17:04:25 +03:00
|
|
|
|
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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':
|
2019-01-09 14:39:36 +03:00
|
|
|
|
# codefunc
|
2019-10-24 13:17:20 +00:00
|
|
|
|
conf_id = msg['message']['chat']['id']
|
|
|
|
|
user_id = msg['message']['from']['id']
|
|
|
|
|
if msg['message']['chat']['type'] == 'private':
|
|
|
|
|
chat_title = conf_id
|
|
|
|
|
else:
|
2018-07-27 15:20:40 +02:00
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
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
|
2018-02-04 07:13:09 +03:00
|
|
|
|
try:
|
|
|
|
|
text = msg['message']['text']
|
2018-09-14 00:00:31 +03:00
|
|
|
|
try:
|
|
|
|
|
username = msg['message']['from']['username']
|
|
|
|
|
except:
|
|
|
|
|
username = "_null"
|
2018-02-04 07:13:09 +03:00
|
|
|
|
try:
|
|
|
|
|
last_name = msg['message']['from']['last_name']
|
|
|
|
|
except:
|
|
|
|
|
last_name = '_null'
|
|
|
|
|
try:
|
|
|
|
|
first_name = msg['message']['from']['first_name']
|
|
|
|
|
except:
|
|
|
|
|
first_name = '_null'
|
|
|
|
|
user_id = msg['message']['from']['id']
|
|
|
|
|
chat_id = msg['message']['chat']['id']
|
2019-10-24 13:17:20 +00:00
|
|
|
|
if msg['message']['chat']['type'] == 'private':
|
|
|
|
|
chat_title = chat_id
|
|
|
|
|
else:
|
|
|
|
|
chat_title = msg['message']['chat']['title']
|
2018-02-04 07:13:09 +03:00
|
|
|
|
except:
|
|
|
|
|
return False
|
2018-12-14 21:09:27 +03:00
|
|
|
|
print("[%s] (%s) %s %s %s: %s" % (chat_title, user_id, username, first_name, last_name, text))
|
2018-02-04 07:13:09 +03:00
|
|
|
|
collection = self.clean_text(text)
|
2018-08-03 15:15:35 +03:00
|
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
|
self.db.add_user(username,
|
2018-08-03 15:15:35 +03:00
|
|
|
|
user_id,
|
|
|
|
|
first_name,
|
|
|
|
|
last_name)
|
2018-02-04 07:13:09 +03:00
|
|
|
|
|
|
|
|
|
self.db.add_conf(chat_id, chat_title)
|
|
|
|
|
|
|
|
|
|
for word in collection:
|
2019-01-09 23:18:19 +03:00
|
|
|
|
self.db.add_relation(word=word, user_id=user_id, conf_id=chat_id, text=text)
|
2018-08-03 15:15:35 +03:00
|
|
|
|
|
2018-02-04 07:13:09 +03:00
|
|
|
|
def clean_text(self, s):
|
|
|
|
|
file = open(self.stop_words, 'rt')
|
|
|
|
|
sw = file.read().split('\n')
|
|
|
|
|
file.close()
|
2018-08-03 15:15:35 +03:00
|
|
|
|
s = re.sub(
|
|
|
|
|
r'(https?:\/\/)?([\da-z\.-]+)\.([\/\w\.-]*)*\/?\S', '', s, flags=re.MULTILINE)
|
2018-02-04 07:13:09 +03:00
|
|
|
|
# dirty hack with dat fucking file
|
2018-08-03 15:15:35 +03:00
|
|
|
|
fh = open("tmp.txt", "w")
|
2018-02-04 07:13:09 +03:00
|
|
|
|
fh.write(s)
|
|
|
|
|
fh.close()
|
|
|
|
|
cmd = "./assets/mystem -nlwd < tmp.txt"
|
2018-08-03 15:15:35 +03:00
|
|
|
|
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
|
2018-02-04 07:13:09 +03:00
|
|
|
|
output = ps.communicate()[0]
|
|
|
|
|
os.remove("tmp.txt")
|
|
|
|
|
# end of the fuckin' dirty hack
|
|
|
|
|
s = output.decode('utf8')
|
|
|
|
|
s = s.replace('?', '')
|
|
|
|
|
s = s.split('\n')
|
|
|
|
|
collection = []
|
|
|
|
|
for word in s:
|
2018-08-03 15:15:35 +03:00
|
|
|
|
if len(word) > 2:
|
2018-02-04 07:13:09 +03:00
|
|
|
|
if word not in sw:
|
|
|
|
|
collection.append(word)
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
pass
|
|
|
|
|
return collection
|
|
|
|
|
|
2018-12-28 18:52:37 +03:00
|
|
|
|
def send(self, id, msg, parse_mode='Markdown'):
|
2018-08-03 15:15:35 +03:00
|
|
|
|
# print(msg)
|
|
|
|
|
url = self.telegram_api + 'bot' + self.telegram_key + '/sendMessage'
|
2018-02-04 07:13:09 +03:00
|
|
|
|
post_fields = {
|
|
|
|
|
'text': msg,
|
|
|
|
|
'chat_id': id,
|
2018-12-28 18:52:37 +03:00
|
|
|
|
'parse_mode': parse_mode,
|
2018-02-04 07:13:09 +03:00
|
|
|
|
'disable_web_page_preview': 1
|
2018-08-03 15:15:35 +03:00
|
|
|
|
}
|
2018-02-04 07:13:09 +03:00
|
|
|
|
urllib.request.Request(url, urlencode(post_fields).encode())
|
|
|
|
|
request = urllib.request.Request(url, urlencode(post_fields).encode())
|
|
|
|
|
json = urllib.request.urlopen(request).read().decode()
|
|
|
|
|
return json
|
2018-07-27 15:20:40 +02:00
|
|
|
|
|
|
|
|
|
def send_img(self, id):
|
2019-01-09 14:39:36 +03:00
|
|
|
|
print('Going to send code.png to %s' % id)
|
|
|
|
|
try:
|
2018-08-03 15:15:35 +03:00
|
|
|
|
url = self.telegram_api + 'bot' + self.telegram_key + '/sendPhoto'
|
2018-07-27 15:20:40 +02:00
|
|
|
|
data = {'chat_id': id}
|
|
|
|
|
files = {'photo': open('code.png', 'rb')}
|
|
|
|
|
r = requests.post(url, files=files, data=data)
|
2019-10-29 15:12:53 +00:00
|
|
|
|
#print(r)
|
2019-01-09 14:39:36 +03:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
2018-11-30 17:04:25 +03:00
|
|
|
|
|
|
|
|
|
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:
|
2019-01-10 23:05:54 +03:00
|
|
|
|
msg += ' [%s](tg://user?id=%s) ' % (user[1], user[2])
|
|
|
|
|
msg += "\nHey all!\n%s" % alert[4]
|
|
|
|
|
self.send(id=alert[0], msg=msg)
|
2018-11-30 17:04:25 +03:00
|
|
|
|
threading.Timer(30, self.cron_timer).start()
|