forked from m-messiah/xye-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
88 lines (79 loc) · 2.98 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# coding=utf-8
from os import environ
import urllib
from flask import Flask, request, jsonify
import re
from random import randint
app = Flask(__name__)
app.config['DEBUG'] = False
NON_LETTERS = re.compile(ur'[^а-яё \-]+', flags=re.UNICODE)
PREFIX = re.compile(u"^[бвгджзйклмнпрстфхцчшщ]+", flags=re.UNICODE)
DELAY = {}
def error():
return 'Hello World! I am XyE_bot (https://telegram.me/xye_bot)'
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'GET':
return error()
else:
if 'Content-Type' not in request.headers:
return error()
if request.headers['Content-Type'] != 'application/json':
return error()
try:
update = request.json
message = update['message']
chat = message['chat']
text = message.get('text')
if text:
app.logger.debug("MESSAGE FROM\t%s",
chat['username'] if 'username' in chat
else chat['id'])
if text == "/start" or text == "/help":
return jsonify(
method="sendMessage",
chat_id=chat['id'],
text=u"Привет! Я бот-хуебот.\n"
u"Я буду хуифицировать "
u"некоторые из твоих фраз"
)
else:
if chat['id'] not in DELAY:
DELAY[chat['id']] = randint(0, 4)
else:
DELAY[chat['id']] -= 1
if DELAY[chat['id']] == 0:
response = huify(text)
del DELAY[chat['id']]
if response:
return jsonify(
method="sendMessage",
chat_id=chat['id'],
text=response
)
return jsonify(result="SKIP", text="SKIP")
except Exception as e:
app.logger.warning(str(e))
return jsonify(result="Fail", text=str(e))
def huify(text):
vowels = {u'о', u'е', u'а', u'я', u'у', u'ю'}
rules = {u'о': u'е', u'а': u'я', u'у': u'ю'}
words = text.split()
if len(words) > 3:
return None
word = NON_LETTERS.sub(u"", words[-1].lower())
postfix = PREFIX.sub(u"", word)
if word == u"бот":
return u'хуебот'
if len(postfix) < 3:
return None
if postfix[0] in rules:
if postfix[1] not in vowels:
return u"ху%s%s" % (rules[postfix[0]], postfix[1:])
else:
if postfix[1] in rules:
return u"ху%s%s" % (rules[postfix[1]], postfix[2:])
else:
return u'ху%s' % postfix[1:]
else:
return u"ху%s" % postfix