-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
73 lines (67 loc) · 2.59 KB
/
utils.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
import logging
from subprocess import (PIPE, Popen)
import telebot
from ConstMessages import *
from Consts import *
from States import *
def add_new_user(db, username, chatId):
""" add a new user to database for broadcasting."""
new_user = {
'id': chatId, 'username': username, 'state': 0,
'report': {'finglish_msg': "", 'farsi_msg': ""}
}
if not db.users.find_one({"id" : new_user["id"]}):
logging.critical("db: " + str(new_user) + " added.")
db.users.insert_one(new_user)
else:
logging.critical("db: " + str(new_user) + " : " + "user exists!")
def transliterate_to_farsi(message):
""" transliterate finglish messages to farsi, returns farsi text """
text = message.text
user_id = message.from_user.id
logging.critical(str(user_id) + " : " + text)
if text:
if text[0] == '/':
text = text[1:]
text = text.replace("@TransliterateBot", "")
text = text.split()
# defallahi(text)
# irregularHandle(text)
shcommand = ['php', './behnevis.php']
shcommand.extend(text)
pipe = Popen(shcommand, stdout=PIPE, stderr=PIPE)
text, err = pipe.communicate()
if err:
logging.critical("PHP ERR: " + err)
logging.critical("res : " + str(user_id) + " : " + text)
return text
def add_report_request(db, message):
"""Add a report request to the database"""
uid = message.from_user.id
farsi_msg = db.users.find_one({'id': uid})['report']['farsi_msg']
finglish_msg = db.users.find_one({'id': uid})['report']['finglish_msg']
corrected_msg = message.text
new_report_instance = {'finglish': finglish_msg,
'farsi': farsi_msg,
'corrected': corrected_msg}
db.reports.insert_one(new_report_instance)
logging.critical("A new report record added: " + str(new_report_instance))
db.users.update({'id': message.from_user.id}, {'$set': {'state': 0}})
def create_message_markup(buttons, row_width=1):
""" buttons is an array of dictionaries containing text and callback data:
buttons = [{
"text": "buttonText1",
"data": "callBackData1"
},
{
"text": "buttonText2",
"data": "callBackData2"
}]
"""
markup = telebot.types.InlineKeyboardMarkup(row_width=row_width)
buttons_list = []
for button in buttons:
new_button = telebot.types.InlineKeyboardButton(button["text"], callback_data=button["data"])
buttons_list.append(new_button)
markup.add(buttons_list)
return markup