def answer_question(teacher_chat_id, parent_chat_id, answer): with connection.cursor() as cursor: query = "SELECT * FROM parentsQuestions" cursor.execute(query) result = cursor.fetchone() if result is not None: ques = "'" + result['question'] + "'" lques = [result['question']] keywords = get_keywords(lques) query2 = f"INSERT INTO QA VALUES({result['chat_id']}, '{answer}', {ques}, '{keywords}')" cursor.execute(query2) query3 = f"DELETE FROM parentsQuestions WHERE question = {ques}" cursor.execute(query3) connection.commit() requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, parent_chat_id, "The teacher says:\n" + answer)) requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, teacher_chat_id, "Is this a general or a private question?" "(general/ private)")) else: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, teacher_chat_id, "There are no questions to answer! " "Thank you and have a nice day! :) ")) return ques
def replay(self): if self.incoming_message_text == 'hello': self.outgoing_message_text = "Hello {}!".format(self._name) elif self.incoming_message_text == 'how are you?': self.outgoing_message_text = 'fine thank you' elif self.incoming_message_text == 'are you a telegram bot?': self.outgoing_message_text = "yes I'm" elif self.incoming_message_text == 'what is my chat id?': self.outgoing_message_text = "you'r chat id is {}".format( self._chat_id) else: self.outgoing_message_text = "I can't answer that, try:\nhello | how are you? | are you a telegram bot? | what is my chat id?" res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self._chat_id, self.outgoing_message_text)) if_answerd = (res.status_code == 200) if if_answerd: self._history["answer"] = self.outgoing_message_text with open("history.json", "a") as f: json.dump(self._history, f) return if_answerd
def send_message(self): res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text)) return True if res.status_code == 200 else False
def add_question(question, teacher_chat_id): global the_question_to_answer with connection.cursor() as cursor: query = f"SELECT * FROM QA WHERE question='{question}' and chat_id={teacher_chat_id}" cursor.execute(query) result = cursor.fetchone() if result is None: the_question_to_answer = question requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, teacher_chat_id, "write @answer to answer the question you added!")) else: requests.get( TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, teacher_chat_id, "question already exists!"))
def what_can_i_do(chat_id): requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You are the boss 😎 you can do one of the following things:\n" "/announce <Announcement> to send an announcement to every parent in the class you " "are in\n" "/add_question <Question> to add a general question"))
def send_message(self, html_text): res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat, html_text, 'HTML')) if res.status_code == 200: return True else: return False
def send_message(self, message): """ Sends message to Telegram servers. """ res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, message)) return True if res.status_code == 200 else False
def send_message_to_user(idChat, msg, disable_notification): msg = urllib.parse.quote(msg, safe='') res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(idChat, msg, 'HTML', disable_notification)) if res.status_code == 200: return True else: return False
def start(chat_id): with connection.cursor() as cursor: sql = "SELECT * FROM `users` where `chat_id`=%s" cursor.execute(sql, chat_id) result = cursor.fetchone() if result is not None: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Hi, what would you like to do today?\n" "It seems you are already registered.\n" "Contact technical support if you want to change your registration\n" "Basil Sgier, Phone: 0533013218\n" "Abeer Dow, Phone: 0547570104\n" "Aseel Nassar, Phone: 0509091207\n" "❤ Tam Team ❤")) else: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Are you a parent or a teacher?"))
def send_message(self): """ Sends message to Telegram servers. """ res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text)) return res.status_code == 200
def send_message(self): """ Sends message to Telegram servers. """ res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text, self.inline_keyboard)) return True if res.status_code == 200 else False
def ask_question(question, chat_id, class_): first_question = question similar_questions = [] with connection.cursor() as cursor: question = "'" + question + "'" lquestion = [question] keywords = get_keywords(lquestion) query = f"SELECT * FROM QA" cursor.execute(query) res = cursor.fetchall() split_keywords = keywords.split() for result in res: db_keywords = result['keywords'].split() for key in split_keywords: if key in db_keywords: similar_questions.append(result['question']) break if len(similar_questions) != 0: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "It seems we have similar questions:\n" "Write %<number>" " to choose the question you want\n")) for i, question in enumerate(similar_questions): requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, str(i) + ")" + " " + question + "\n")) requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "If you can't find your question, write 'none'")) else: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "No similar questions were asked," " we will send your question to the teacher. Please wait for the answer.\n" )) query = f"INSERT INTO parentsQuestions VALUES({chat_id},'{first_question}')" cursor.execute(query) ask_question2(first_question, class_) connection.commit() return chat_id, first_question, similar_questions
def ask_question2(question, class_): with connection.cursor() as cursor: query = f"SELECT * FROM users WHERE job = 'main teacher' and class = {class_}" cursor.execute(query) res = cursor.fetchone() if res is not None: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, res['chat_id'], "You have a new question:\n" + question + "\nStart your answer with @ans")) else: query = f"SELECT * FROM users WHERE job = 'teacher' and class = {class_}" cursor.execute(query) res = cursor.fetchone() if res is not None: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, res['chat_id'], "You have a new question:\n" + question + "\nStart your answer with @ans"))
def auto_send(self): print(self._chatids) while (1): for chatid in self._chatids: res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(chatid, "this is the auto msg")) print(res.status_code) time.sleep(30)
def add_announcement(announcement, class_): with connection.cursor() as cursor: query = f"SELECT chat_id FROM users WHERE role = 'parent' and class = {class_}" cursor.execute(query) result = cursor.fetchall() if result: for parent_chat_id in result: requests.get( TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, parent_chat_id['chat_id'], announcement))
def post_callbackQuery(self): success = None #Shoud be dynamic #self.chat_id = 924179441 #temp = self.callback_id success = requests.post( TELEGRAM_ANSWER_CALLBACK_URL.format(self.callback_id, "Test")) if self.callback_data == self.ikb_opt_1_callback_data: res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.ikb_opt_1_callback_data)) elif self.callback_data == self.ikb_opt_2_callback_data: res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.ikb_opt_2_callback_data)) return "200"
def remove_question(teacher_chat_id, ques): with connection.cursor() as cursor: query = f"SELECT * FROM QA where question = {ques}" cursor.execute(query) result = cursor.fetchone() if result is not None: query3 = f"DELETE FROM QA WHERE question = {ques}" cursor.execute(query3) connection.commit() requests.get( TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, teacher_chat_id, "okay 🤗"))
def handle_message(): if request.get_json().get('edited_message'): msg = request.get_json()['edited_message']['text'] chat_id = request.get_json()['edited_message']['chat']['id'] chat_name = request.get_json()['edited_message']['from']['first_name'] else: msg = request.get_json()['message']['text'] chat_id = request.get_json()['message']['chat']['id'] chat_name = request.get_json()['message']['from']['first_name'] res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, parse_command(msg, chat_id, chat_name))) return Response("success")
def post_message(self): success = None # should be handled in parse_webhook_data_message #self.chat_id = 924179441 self.outgoing_message_text = self.data #self.outgoing_message_text = "Something else" res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text)) return True if res.status_code == 200 else False
def answer_the_last_question(answer, teacher_chat_id): with connection.cursor() as cursor: query = "SELECT * FROM parentsQuestionsQueue LIMIT 1" cursor.execute(query) result = cursor.fetchone() if result is not None: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, result['chat_id'], "The answer for your last question is :\n" + answer)) answer = "'" + answer + "'" ques = "'" + result['question'] + "'" lques = [result['question']] keywords = get_keywords(lques) query2 = f"INSERT INTO QA VALUES({teacher_chat_id}, {answer}, {ques}, '{keywords}')" cursor.execute(query2) query3 = f"DELETE FROM parentsQuestionsQueue WHERE question = {ques}" cursor.execute(query3) connection.commit() else: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, teacher_chat_id, "THERE IS NO QUESTION TO ANSWER!!"))
def send_message(self,reply_markup=None,command_list=None): if reply_markup is None: reply_markup = '' str="" if command_list is not None : for command,desc in command_list: str=str+"\n" + command + " : " + desc self.outgoing_message_text=self.outgoing_message_text+str res = requests.get(TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, self.outgoing_message_text,reply_markup)) print(res) return True if res.status_code == 200 else False
def answer_add_question(answer, chat_id): global the_question_to_answer with connection.cursor() as cursor: query = f"SELECT * FROM QA WHERE question='{the_question_to_answer}' and chat_id={chat_id}" cursor.execute(query) result = cursor.fetchone() if result is None: lquest = [the_question_to_answer] keywords = get_keywords(lquest) query = f"INSERT INTO QA VALUES({chat_id}, '{answer}', '{the_question_to_answer}' , '{keywords}')" cursor.execute(query) connection.commit() requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "question added successfully!")) the_question_to_answer = ''
def send_msg(self, id, msg): res = requests.get(TELEGRAM_SEND_MESSAGE_URL.format(id, msg)) return True if res.status_code == 200 else False
def forward_message(self, chat_id): res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format( chat_id, "Stranger : " + self.incoming_message_text)) return True if res.status_code == 200 else False
def messageAdmin(message): res = requests.get(url_send_message.format(id_admin, f"New photo from {message}")) return res.status_code == 200
def checkSurf(): start = arrow.utcnow() end = arrow.utcnow().shift(hours=+0) #''' response = requests.get( "https://api.stormglass.io/v2/weather/point", params={ 'lat': 39, # change for 'lng': -74, # Change for your location 'params': ','.join( ['waveHeight', 'windDirection', 'windSpeed', 'wavePeriod']), 'start': start, 'end': end }, headers={ "Authorization": "" #stormglass authorization }) oceanInfo = response.json() message = 'Bomb Squad Alarm: ' message = message + arrow.now().format('MM/DD/YYYY HH:mm:ss') + '\n' aveDict = dict() for ele in oceanInfo.get('hours')[0]: if ele != 'time': lis = oceanInfo.get('hours')[0].get(ele).values() total = sum(lis) times = len(lis) average = total / times aveDict[ele] = average #oceanInfo.get('hours')[0].get(ele).values() alarm = True for ele in aveDict: if ele == 'waveHeight': if aveDict.get(ele) <= .5: #this is a check for height alarm = False break message = message + "Wave Height : " + str( aveDict.get(ele) * 3.28084) + "ft\n" if ele == 'windDirection': if aveDict.get(ele) < 190 and aveDict.get( ele) > 10: #this is a check for off shore wind alarm = False break message = message + "Wind Direction : " + str( aveDict.get(ele)) + "degrees\n" if ele == 'windSpeed': message = message + "Wind Speed : " + str( aveDict.get(ele) * 2.23694) + "mph\n" if ele == 'windPeriod': message = message + "Wave Period : " + str( aveDict.get(ele)) + "seconds\n" alarm = True # this is only for test if alarm: requests.get(TELEGRAM_SEND_MESSAGE_URL.format(-423921262, message))
def send_message(self, msg): response = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(self.chat_id, msg)) return True if response.status_code == 200 else False
def send_message(chat_id, content): url = TELEGRAM_SEND_MESSAGE_URL.format(chat_id, content) req = requests.get(url) return True if req.status_code == 200 else False
def send_ticket_options(self, tickets): res = requests.get( TELEGRAM_SEND_MESSAGE_URL.format(os.environ['CHAT_ID'], tickets)) return True if res.status_code == 200 else False
def parse_command(com, chat_id, name): global role_, asker_id_, ques global class_2 global last_similar_questions, parent_last_question class_ = None role = None parsed = com.split(" ", 1) # maxsplit = 1 first_command = parsed[0] with connection.cursor() as cursor: sql = "SELECT * FROM `users` where `chat_id`=%s" cursor.execute(sql, chat_id) result = cursor.fetchone() if result is not None: role = result['role'] class_ = result['class'] else: role = role_ class_ = class_2 if first_command == "/start": start(chat_id) elif first_command.lower() == "teacher" or first_command.lower( ) == "parent": role_ = first_command.lower() requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "What class are you in?\nWrite class <number>, " "so I can know to which class I should add you 😉")) elif first_command.lower() == "class": class_2 = parsed[1] if role_ == "teacher": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Okay good!\nOne more thing, are you the main teacher or not ? (yes/no)" )) else: add_user(chat_id, role_, class_2, None) requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Welcome!! " + name + " you have been registered as" " " + role_ + " in class " + class_2)) elif first_command.lower() == "yes": if role_ == "teacher": add_user(chat_id, role_, class_2, "main teacher") requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Welcome!! " + name + " you have been registered as" " the main teacher" + " in class " + class_2)) what_can_i_do(chat_id) elif first_command.lower() == "no": if role_ == "teacher": add_user(chat_id, role_, class_2, "teacher") requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Welcome!! " + name + " you have been registered as" " " + role_ + " In class " + class_2)) what_can_i_do(chat_id) # elif first_command == "/answer": # if role == "parent": # requests.get( # TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, chat_id, "You are a parent you can not answer a question 😉")) # else: # if len(parsed) <= 1: # requests.get(TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, chat_id, "You should write /answer " # "and after it your answer")) # else: # answer_the_last_question(parsed[1], chat_id) # elif first_command == "/ask": # if len(parsed) <= 1: # requests.get(TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, chat_id, "You should write /ask " # "and after it your question")) # else: # ask_question(parsed[1], chat_id) # elif first_command == "/ask_privately": # if len(parsed) <= 1: # requests.get(TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, chat_id, "You should write /ask_privately " # "and after it your question")) # else: # asker_id_ = ask_question2(parsed[1], chat_id, class_) elif first_command == "@ans": if role == "parent": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You are a parent you can not answer a question 😉")) else: if len(parsed) <= 1: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You should write @ans " "and after it your answer")) else: ques = answer_question(chat_id, asker_id_, parsed[1]) elif first_command == "/add_question": if role == "parent": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You are a parent you can not add a question 😔")) else: if len(parsed) <= 1: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You should write /add_question " "and after it your question")) else: add_question(parsed[1], chat_id) elif first_command == "@answer": if role == "parent": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You are a parent you can not answer a question 😉")) else: if len(parsed) <= 1: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You should write @answer " "and after it your answer")) else: answer_add_question(parsed[1], chat_id) elif first_command == "/announce": if role == "parent": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You are a parent you can not send announcement 🙃")) else: if len(parsed) <= 1: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "You should write /announce " "and after it your announcement")) else: add_announcement(parsed[1], class_) elif first_command.lower() == "general": requests.get( TELEGRAM_SEND_MESSAGE_URL.format(TOKEN, chat_id, "Saved in FAQ")) elif first_command.lower() == "private": remove_question(chat_id, ques) elif first_command.lower() == "hello" or first_command.lower( ) == "hey" or first_command.lower() == "hi" or first_command.lower( ) == "sup": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, f"Hey!! {name},\n" "My name is Tam how can I help you? \n")) if role == "teacher": what_can_i_do(chat_id) elif first_command[0] == '%': if 0 <= int(first_command[1:]) < len(last_similar_questions): with connection.cursor() as cursor: query = f"SELECT * FROM QA WHERE question = '{last_similar_questions[int(first_command[1:])]}'" cursor.execute(query) res = cursor.fetchone() if res is not None: requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "The answer is:\n" + res['answer'])) elif first_command.lower() == 'none': with connection.cursor() as cursor: query = f"INSERT INTO parentsQuestions VALUES({chat_id},'{parent_last_question}')" cursor.execute(query) ask_question2(parent_last_question, class_) requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "There is no answer yet, I will check with the " "teacher and get back to you")) connection.commit() elif first_command.lower() == "help": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Contact technical support:\n" "Abeer Dow, Phone: 0547570104\n" "Basil Sgier, Phone: 0533013218\n" "Aseel Nassar, Phone: 0509091207\n" "❤ Tam Team ❤")) else: if role == "teacher": requests.get( TELEGRAM_SEND_MESSAGE_URL.format( TOKEN, chat_id, "Unavailable command you can do the following:\n")) what_can_i_do(chat_id) else: asker_id_, parent_last_question, last_similar_questions = ask_question( com, chat_id, class_) return ""