-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
169 lines (144 loc) · 5.77 KB
/
bot.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Main bot logic
"""
##### IMPORTS ######
import logging
import telebot
from Token import TOKEN
from mongo_auth import dbuser, dbpass
from bot_url import BOT_URL
import pymongo
from utils import *
from init import *
####################
##### HANDLERS #####
@bot.message_handler(commands=['start'])
def initialize(message):
""" function for start command """
if not db.users.find_one({'id': message.from_user.id}):
add_new_user(db, message.from_user.username, message.from_user.id)
else: logging.info("User exists.")
txt = str(message.text)
if len(txt) > len('/start'):
bot.send_message(message.from_user.id, txt[len('/start'):]+REPORT_HOWTO_MESSAGE)
else:
bot.reply_to(message,
(START_MESSAGE))
@bot.message_handler(commands=['help'])
def help_provider(message):
""" function for help command """
bot.reply_to(message,
(HELP_MESSAGE))
@bot.message_handler(commands=['contact'])
def contact_creator(message):
""" function for contact command command """
bot.reply_to(message,
(CONTACT_MESSAGE))
@bot.message_handler(commands=['about'])
def about_me(message):
""" function for 'about creator of this bot' command """
bot.reply_to(message,
(ABOUT_MESSAGE))
@bot.callback_query_handler(func=lambda callback: True )
def handle_all_callbacks(callback):
"""
this runs a function named callback['data'], with callback as the only argument
"""
globals()[callback.data](callback)
@bot.message_handler(func=lambda message: True, content_types=['text'])
def handle_group_or_user(message):
""" handle messages -> transliterate messages or save reports. """
# if not db.users.find_one({'id': message.from_user.id}):
# add_new_user(db, message.from_user.username, message.from_user.id)
# else:
user_reported = db.users.find_one({"id": message.from_user.id})
if user_reported:
if user_reported['state'] == REPORT:
logging.critical("New incoming report:")
add_report_request(db, message)
bot.send_message(message.from_user.id, REPORT_SUCCESS_MESSAGE)
accept_message_buttons = [
{
"text": "✅",
"data": "accept"
},
{
"text": "❌",
"data": "reject"
}
]
accept_markup = create_message_markup(accept_message_buttons, row_width=2)
accept_report_text = "New Report:" + \
"\nFinglish: " + user_reported['report']['finglish_msg'] + \
"\nFarsi: " + user_reported['report']['farsi_msg'] + \
"\nCorrected: " + message.text
bot.send_message(MY_ID, accept_report_text, reply_markup=accept_markup)
return
else:
logging.critical("ERR: User not found. adding user to database...")
db.users.insert_one({'id': message.from_user.id, 'username': message.from_user.username,
'state': IDLE,
'report':{'finglish_msg': "", 'farsi_msg': ""}})
# Adding glass keyboard markup to the result message
report_message_buttons = [{
"text": "اشتباهه؟🗣",
"data": "wrong"
}]
if message.chat.type == "private": # Private message to bot
text = transliterate_to_farsi(message)
markup = create_message_markup(report_message_buttons)
bot.reply_to(message, text, reply_markup=markup)
else: # Group message
if message.text == 'fa' or message.text == 'Fa' or \
message.text == 'FA' or message.text == 'فا'.decode('utf-8'):
msg = message.reply_to_message
if msg is not None:
text = transliterate_to_farsi(msg)
markup = create_message_markup(report_message_buttons)
bot.reply_to(msg, text, reply_markup=markup)
else:
logging.critical("Err : message is empty")
####################
## CALLBACK FUNCS ##
def wrong(callback):
"""handle incoming callback for reporting wrong transliterations"""
if not db.users.find_one({'id': callback.from_user.id}):
add_new_user(db, callback.from_user.username, callback.from_user.id)
else: logging.info("In Callback func, User exists.")
finglish_msg = callback.message.reply_to_message.text
farsi_msg = callback.message.text
updated_user = {'id': callback.from_user.id, 'username': callback.from_user.username,
'state': REPORT,
'report':{'finglish_msg': finglish_msg, 'farsi_msg': farsi_msg}
}
db.users.update({'id': callback.from_user.id}, updated_user)
logging.info("user reported: " + str(updated_user))
if callback.message.reply_to_message.chat.type == 'private':
bot.send_message(callback.from_user.id, str(finglish_msg)+"\nلطفا شکل درست این پیام را به فارسی بنویسید.")
bot.answer_callback_query(callback.id)
else:
bot.answer_callback_query(callback.id, url=BOT_URL+str(finglish_msg))
def accept(callback):
"""
handle incoming callbacks from reports accepted by admin
"""
# add words to database and search from them.
bot.answer_callback_query(callback.id, text="accepted")
def reject(callback):
"""
handle incoming callbacks from reports rejected by admin
"""
# delete ? record from database
bot.answer_callback_query(callback.id)
def like(callback):
"""
handle incoming callbacks from liking a transliteration
"""
pass
####################
###### RUNNER ######
bot.skip_pending = True
bot.polling()
####################