def broadcast_POST(message): if (message.chat.id == feedBack_target_chat): with sqlite3.connect("../users.sqlite") as conn: cur = conn.cursor() cur.execute("SELECT * FROM users") DB_table = cur.fetchall() print DB_table success = 0 crashes = 0 for elem in DB_table: try: print bot.forward_message(elem[0], message.chat.id, message.json[u'message_id']) success = success + 1 except: print '--------' crashes = crashes + 1 # traceback.print_exc() try: # Added this to make sure every thing is running as smooth as possible bot.send_message( feedBack_target_chat, "This guy blocked the bot:\n" + str(elem[2]) + "\n" + str(elem[3]) + "\n" + str(elem[0]) ) #remove this markup after the first announcement except: pass bot.send_message( feedBack_target_chat, "Finished the announcement\nPassed: " + str(success) + "\nFailed: " + str(crashes))
def COMM_ordermeal(message): check = trafficController.check_spam(message.chat.id, 'COMM_ordermeal') if check == "OK": response = users.STARTorder_meal(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'COMM_ordermeal')
def COMM_thisweek(message): check = trafficController.check_spam(message.chat.id, 'COMM_thisweek') if check == "OK": response = users.this_week_data(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'COMM_thisweek')
def output_FUNC(input_userID, input_2nd): try: FUNC(input_userID, input_2nd) except: bot.send_message(input_userID, MSGs.we_cant_do_it_now) log_error("ERROR: " + FUNC.__name__) return
def COMM_fcode(message): check = trafficController.check_spam(message.chat.id, 'COMM_fcode') if check == "OK": response = users.forgotten_code(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'COMM_fcode')
def test_FUNC(message): check = trafficController.check_spam(message.chat.id, 'set_places') if check == "OK": response = users.extract_DINING_places(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'set_places')
def test_FUNC(message): check = trafficController.check_spam(message.chat.id, 'inc_credit') if check == "OK": response = users.STARTincreasing_credit(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'inc_credit')
def COMM_set_auto_res(message): check = trafficController.check_spam(message.chat.id, 'COMM_get_pri') if check == "OK": response = users.STARTset_auto_res(message.chat.id) if response is not None: bot.send_message(message.chat.id, response) trafficController.finished_process(message.chat.id, 'COMM_get_pri')
def show_markup(call): if (is_in_duel(call.from_user.id)): markup = Duelmarkup else: markup = Defaultmarkup bot.send_message(call.from_user.id, "Here is the menu", reply_markup=markup)
def output_FUNC(input_userID,input_2nd): try: FUNC(input_userID,input_2nd) except: bot.send_message(input_userID,MSGs.we_cant_do_it_now) trafficController.drop_check(input_userID) log_error("ERROR: " + FUNC.__name__) return
def output_FUNC(input_message): try: FUNC(input_message) except: bot.send_message(input_message.chat.id,MSGs.we_cant_do_it_now) trafficController.drop_check(input_message.chat.id) log_error("ERROR: " + FUNC.__name__) return
def process_user_call(call): user_ID = call.from_user.id call_TXT = call.data if (call_TXT == "UserPass"): user_book[user_ID]["state"] = "get_USER" bot.send_message(user_ID, MSGs.give_user) else: bot.send_message(user_ID, "خطا در دریافت دستور...")
def send_welcome(message): check = trafficController.check_spam(message.chat.id) if check == "OK": bot.send_message(message.chat.id, MSGs.greetings, reply_markup=MSGs.enter_userpass_markup, parse_mode='HTML') users.add_user(message) trafficController.drop_check(message.chat.id)
def text_MSG(message): if users.users_book[ message.from_user.id]["state"] == "broadcast_waiting_message": print "ITS A simple text message to send" print message.json[u'message_id'] commercial.broadcast_POST(message) return response = users.process_user_MSG(message.chat.id, message.text, message) if response is not None: bot.send_message(message.chat.id, response)
def send_welcome(message): check = trafficController.check_spam(message.chat.id, 'start') if check == "OK": bot.send_message(message.chat.id, MSGs.greetings, reply_markup=MSGs.enter_userpass_markup, parse_mode='HTML') users.add_user(message.chat.id) print dataBase.check_the_user_in_DB(message) users.clear_PLCs(message.chat.id) trafficController.finished_process(message.chat.id, 'start')
def check_spam(userID): if userID in last_user_msg_time.keys(): print "traffic_controller:", last_user_msg_time[userID] if (datetime.now() - last_user_msg_time[userID]).total_seconds() < 130: bot.send_message(userID, MSGs.in_the_middle_of_a_process) return "IN_PROC" else: print "traffic_controller:", "IGNORING" return "IGNORE" last_user_msg_time[userID] = datetime.now() return "OK"
def test_callback(call): try: bot.answer_callback_query(call.id) except: pass if users.know_user(call.from_user.id) == False: return # print "We have a call..." response = users.process_user_call(call.from_user.id, call.data, call) # print "response: " + str(response) if response is not None: bot.send_message(call.from_user.id, response)
def add_likes(userId, Like_Amnt): cn = sqlite3.connect("zthb.sqlite") cur = cn.cursor() cur.execute("SELECT * FROM cookie_giver WHERE u_id={0}".format(userId)) cn.execute("PRAGMA ENCODING = 'utf8';") # cur.row_factory = lite.Row cn.text_factory = str row = cur.fetchone() if row != None: userLikes = row[6] + Like_Amnt cur.execute("UPDATE cookie_giver SET u_likes=? WHERE u_id=?", (userLikes, userId)) cn.commit() bot.send_message(userId, "Now me {0}❤ you".format(userLikes)) cn.close()
def abort_duel(message): UsrId = message.from_user.id for Duel_ele in Duel_list: if (Duel_ele.F_Id == UsrId or Duel_ele.S_Id == UsrId): bot.send_message(Duel_ele.F_Id, "The Duel has been aborted by " + find_disp_name(UsrId), reply_markup=Defaultmarkup) bot.send_message(Duel_ele.S_Id, "The Duel has been aborted by " + find_disp_name(UsrId), reply_markup=Defaultmarkup) Duel_list.remove(Duel_ele) return True
def check_spam(userID,MSG_type): if userID in last_user_msg.keys(): print last_user_msg[userID] if last_user_msg[userID][0] in long_process: if (datetime.now() - last_user_msg[userID][1]).total_seconds() < 130: bot.send_message(userID, MSGs.in_the_middle_of_a_process) return "IN_PROC" elif last_user_msg[userID][0] in short_process: if (datetime.now() - last_user_msg[userID][1]).total_seconds() < 0.5: bot.send_message(userID, MSGs.Stop_Spamming) return "IGNORE" else: print "--IGNORING--" return "IGNORE" last_user_msg[userID] = (MSG_type, datetime.now()) return "OK"
def renew_all_users(): count_win = 0 count_loose = 0 for user_ID in user_book.keys(): try: if (user_book[user_ID]["user"] and user_book[user_ID]["pass"]): renew_account_books(user_ID, quiet=True) count_win += 1 else: count_loose += 1 except: try: bot.send_message(user_ID, MSGs.cant_auto_renew) except: pass # TODO Implement something to ignore the blockers Error_Handle.log_error("ERROR: " + "renew_all_users()::for") return bot.send_message( inits.feedBack_target_chat, "Passed: " + str(count_win) + '\n' "Failed: " + str(count_loose) + '\n' "Total: " + str(count_win + count_loose))
def send_welcome(message): InvDuelCode = generate_duel_code(str(message.from_user.id), str(message.date)) #find the amount of likes: tempstr = message.text.split() try: StrAmnt = tempstr[1] Amnt = int(StrAmnt) if (Amnt <= 0): raise Exception if (give_user_like(message.from_user.id) < Amnt): raise IOError("User doesn't have the enough likes") except IOError as valerr: print valerr bot.send_message(message.chat.id, "Sorry you don't have enough likes!") return except: bot.send_message(message.chat.id, "Please enter an amount in number") half_inv.append(message.from_user.id) return markup = types.InlineKeyboardMarkup() markup.add( types.InlineKeyboardButton("invite someone", switch_inline_query=InvDuelCode)) bot.send_message(message.chat.id, "Hi,\nI Like to invite you to a CookieDuel\non " + str(Amnt) + emoji.emojize(emoji.demojize(u'❤')), reply_markup=markup) init_inline_func(InvDuelCode) store_duel(message.from_user.id, Amnt, InvDuelCode)
def get_Duel_Amnt_number(message): try: if message.from_user.id not in half_inv: return False half_inv.remove(message.from_user.id) Amnt = int(message.text) if (Amnt <= 0): bot.send_message(message.chat.id, "Sorry, That isn't a valid amount :)") return True if (give_user_like(message.from_user.id) < Amnt): bot.send_message(message.chat.id, "Sorry you don't have enough likes!") return True InvDuelCode = generate_duel_code(str(message.from_user.id), str(message.date)) markup = types.InlineKeyboardMarkup() markup.add( types.InlineKeyboardButton("invite someone", switch_inline_query=InvDuelCode)) bot.send_message(message.chat.id, "Hi,\nI Like to invite you to a CookieDuel\non " + str(Amnt) + emoji.emojize(emoji.demojize(u'❤')), reply_markup=markup) init_inline_func(InvDuelCode) store_duel(message.from_user.id, Amnt, InvDuelCode) return True except: return False
def erase_last_30_MSGs(userID): if (userID == feedBack_target_chat): with sqlite3.connect("../users.sqlite") as conn: cur = conn.cursor() cur.execute("SELECT * FROM users") DB_table = cur.fetchall() print DB_table success = 0 crashes = 0 for elem in DB_table: try: tempMSG = bot.send_message(elem[0], "test", disable_notification=True) MSGnum = tempMSG.json[u'message_id'] i = 0 while (i < 30): try: bot.delete_message(elem[0], MSGnum - i) except: pass i += 1 if (i == 1): success = success + 1 except: print '--------' crashes = crashes + 1 # traceback.print_exc() try: # Added this to make sure every thing is running as smooth as possible bot.send_message( feedBack_target_chat, "This guy blocked the bot:\n" + str(elem[2]) + "\n" + str(elem[3]) + "\n" + str(elem[0]) ) # remove this markup after the first announcement except: pass bot.send_message( feedBack_target_chat, "Finished the announcement\nPassed: " + str(success) + "\nFailed: " + str(crashes))
def TUESDAY_ALARM(): global RUN_THREAD while (RUN_THREAD): try: now = datetime.datetime.utcnow() + datetime.timedelta(hours=3, minutes=30) ALARM_TIME = now + datetime.timedelta(days=( (7 - now.weekday()) % 7) + 1) ALARM_TIME = ALARM_TIME.replace(year=ALARM_TIME.year, month=ALARM_TIME.month, day=ALARM_TIME.day, hour=15, minute=00, second=00) wait_time = (ALARM_TIME - now).total_seconds() % (7 * 24 * 60 * 60) print "wait_time: ", wait_time print(ALARM_TIME - now).total_seconds() time.sleep(wait_time) # time.sleep(2) auto_res_count = 0 counter = 0 for tmpUserID in users.users_book.keys(): print "++=", tmpUserID try: if (tmpUserID not in users.users_auto_res_days.keys() ): # Make sure this guy doesn't have an auto_res if (users.users_book[tmpUserID]["user"] != None and users.users_book[tmpUserID]["pass"] != None): # If there was some password to get in bot.send_message( tmpUserID, "وقت رزرو شده...", reply_markup=MSGs.reserve_time_markup) counter += 1 else: auto_res_count += 1 except: pass bot.send_message( feedBack_target_chat, "Number of users: " + str(counter + auto_res_count)) bot.send_message(feedBack_target_chat, "Number of auto_res: " + str(auto_res_count)) # bot.send_message(feedBack_target_chat, "lunching the AUTO_RESERVE...") # AUTO_RESERVE_TRIG() # bot.send_message(feedBack_target_chat, "finished AUTO_RESERVE :)") except: pass
def respond_to_user_ID(user_ID): user_book[user_ID]["state"] = "admin_respond_get_chat_ID" bot.send_message(user_ID, "leave the destination chat ID:")
def process_user_MSG(message): global dest_Chat_ID user_ID = message.chat.id msg_TXT = message.text if (user_book[user_ID]["state"] == "get_USER"): user_book[user_ID]["user"] = msg_TXT user_book[user_ID]["state"] = "get_PASS" bot.send_message(user_ID, MSGs.give_pass) elif (user_book[user_ID]["state"] == "get_PASS"): user_book[user_ID]["pass"] = msg_TXT user_book[user_ID]["state"] = None check_account_status(user_ID) if (user_book[user_ID]["state"] == "DONE" ): #Check if the user pass is OK and then bring in the database dataBase._update_UserPass(user_ID, user_book[user_ID]["user"], user_book[user_ID]["pass"]) bot.send_message(user_ID, MSGs.your_good_to_go, reply_markup=MSGs.simple_MAIN_markup) else: user_book[user_ID]["user"] = None user_book[user_ID]["pass"] = None elif (user_book[user_ID]["state"] == "get_feedback"): bot.forward_message(feedBack_target_chat, user_ID, message.message_id) bot.send_message(feedBack_target_chat, "The users ID is:") bot.send_message(feedBack_target_chat, str(user_ID)) bot.send_message(user_ID, MSGs.feedBack_sent, reply_markup=MSGs.simple_MAIN_markup) user_book[user_ID]["state"] = None elif (user_book[user_ID]["state"] == "admin_respond_get_chat_ID"): dest_Chat_ID = int(message.text) bot.send_message(feedBack_target_chat, "Leave response:") user_book[user_ID]["state"] = "admin_respond_entering_MSG" elif (user_book[user_ID]["state"] == "admin_respond_entering_MSG"): bot.send_message(dest_Chat_ID, message.text) bot.send_message(feedBack_target_chat, "Sent :)") dest_Chat_ID = -1 else: if (randrange(2) == 0): bot.send_message(user_ID, MSGs.what_question_mark_v1) else: bot.send_message(user_ID, MSGs.what_question_mark_v2)
def cancel_action(user_ID): user_book[user_ID]["state"] = None bot.send_message(user_ID, MSGs.canceled_successfully, reply_markup=MSGs.simple_MAIN_markup)
def check_account_status(user_ID, quiet=False): if (user_book[user_ID]["pass"] == None): bot.send_message(user_ID, MSGs.please_give_user_pass, reply_markup=MSGs.enter_userpass_markup) return if not quiet: bot.send_message(user_ID, MSGs.getting_status) input_data = { "pass": user_book[user_ID]["pass"], "user": user_book[user_ID]["user"], "chat_id": user_ID, "extend": False } input_file_name = 'input_EXT_' + str(user_ID) + '.json' with open('tmp/' + input_file_name, 'w') as outfile: json.dump(input_data, outfile) try: p = subprocess.Popen(['casperjs', 'crawlers/EXT.js', input_file_name]) print p.poll() for i in range(120): if (p.poll() is None): time.sleep(1) except: p.send_signal(signal.SIGINT) Error_Handle.log_error("SCRIPT ERROR: check_account_status") print "Script KILLED" return if (p.poll() is None): p.send_signal(signal.SIGINT) print "CTRL+C The script didn't get completely finished" bot.send_message(user_ID, MSGs.error_in_getting_data) return print "--DONE--" data = None data_output_file_name = 'output_EXT_' + str(user_ID) + '.json' # --Reading the results-- with open('tmp/' + data_output_file_name) as f: data = json.load(f) # os.remove('tmp/' + data_output_file_name) # os.remove('tmp/' + input_file_name) if "ENTRY_STATE" not in data: bot.send_message(user_ID, MSGs.error_in_getting_data) return if data["ENTRY_STATE"] != "GOOD": bot.send_message(user_ID, MSGs.error_in_getting_data) return if data["PASSWORD_STATE"] == "WRONG": bot.send_message(user_ID, MSGs.your_password_is_wrong, reply_markup=MSGs.enter_userpass_markup) return main_MSG = "" for elem in data["table"]: temp_MSG = str(elem["rowNum"] + 1) + ".\n" temp_MSG += "عنوان کتاب:\n" + str(elem["title"]) + "\n" temp_MSG += "موعد بازگشت:\n" + str(elem["returnDate"]) + "\n" temp_MSG += "وضعیت:\n" + str(elem["status"]) + "\n" main_MSG += temp_MSG main_MSG += "-------------\n" bot.send_message(user_ID, main_MSG, reply_markup=MSGs.simple_MAIN_markup) user_book[user_ID]["state"] = "DONE"
def COMM_feedback(message): bot.send_message(message.chat.id, MSGs.give_your_feedback) users.wait_for_feedback(message.chat.id)