def createGroup(username,group_name,additional_username): print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Running background event: Creating Telegram Group') client = tele_util.getClient(username) results = tele_util.initialize_Group( username=additional_username, client=client, group_name=group_name, ) telegram_chat = Telegram_Chats.objects.get(name=group_name) telegram_chat.link = results['group_link'] telegram_chat.save() print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Telegram Group: ' + group_name + ' create') tele_util.disconnectClient(client) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Ending background event: Creating Telegram Group') getAllChatMembers( username=username, chat_type=telegram_chat.type, chat_name=group_name, schedule=0, )
def configureDB_telegram(requests): response = {"configure_telegram" : "active"} utilities.populateRelevantCourses(requests,instructorEmail=requests.user.email) response['courses'] = requests.session['courseList_updated'] if requests.method == "GET": return render(requests, "Module_TeamManagement/Instructor/instructorTools.html", response) try: username = requests.user.email.split('@')[0] phone_number = requests.POST.get('phone_number') login_code = requests.POST.get('login_code') toolType = 'Telegram' if requests.POST.get('type') == None else requests.POST.get('type') facultyObj = Faculty.objects.get(username=username) registered_course = facultyObj.course_section.all() if len(phone_number) == 8: phone_number = str('+65') + phone_number client = tele_util.getClient(username) if not client.is_user_authorized(): if phone_number != None and login_code == None: client.send_code_request(phone_number) encrypt_phone_number = utilities.encode(phone_number) facultyObj.phone_number = encrypt_phone_number facultyObj.save() return HttpResponse('') elif phone_number != None and login_code != None: try: client.sign_in(phone=phone_number, code=login_code) except PhoneNumberUnoccupiedError: client.sign_up(phone=phone_number, code=login_code) print(toolType) for course_section in registered_course: bootstrap.configureCourseToolsList(course_section.course_section_id,toolType) # Add faculty telegram username into DB # myself = client.get_me() # facultyObj.telegram_username = '******' + str(myself.username) # facultyObj.save() tele_util.disconnectClient(client) except Exception as e: traceback.print_exc() response['error_message'] = e.args[0] return render(requests, "Module_TeamManagement/Instructor/instructorTools.html", response) response['message'] = 'Telegram Account Configured' # return render(requests, "Module_TeamManagement/Instructor/instructorTools.html", response) return faculty_HomePage(requests)
def faculty_telegram_UpdateChatMembers(requests): response = {"faculty_telegram_UpdateChatMembers": "active"} tele_chat_type = { 'Channel': Channel, 'Group': Chat, } # Redirect user to login page if not authorized and faculty try: processLogin.InstructorVerification(requests) except: logout(requests) return render(requests, 'Module_Account/login.html', response) course_section = requests.POST.get('course_section') telegram_chat_name = requests.POST.get('chat_name').replace('_', ' ') print('Telegram Chat Name: ' + telegram_chat_name) print('Course Section: ' + course_section) try: telegram_chat = Telegram_Chats.objects.get(name=telegram_chat_name) client = tele_util.getClient(requests.user.email.split('@')[0]) try: members, count = tele_util.getMembers( client, telegram_chat.name, tele_chat_type[telegram_chat.type]) except OperationalError: raise Exception( 'Please wait a few minutes for the chat to be initialize, before updating chat members. If the problem persist, please check with system administrator.' ) if count > 0: telegram_chat.members = '_'.join(members) telegram_chat.save() else: # telegram_chat.delete() raise Exception( 'Chat does not exists within Telegram client. Please remove chat from the system.' ) tele_util.disconnectClient(client) except Exception as e: # traceback.print_exc() requests.POST = requests.POST.copy() requests.POST['chat_name'] = None response['courses'] = requests.session['courseList_updated'] response['error_message'] = str(e.args[0]) return faculty_telegram_Base(requests, response) requests.POST = requests.POST.copy() requests.POST['chat_name'] = telegram_chat_name response['message'] = 'Members successfully updated' return faculty_telegram_Base(requests, response)
def sendMessage(username,chat_type,chat_name,message): print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Running background event: Sending Telegram Message') client = tele_util.getClient(username) if chat_type == 'Group': tele_util.sendGroupMessage(client,chat_name,message) elif chat_type == 'Channel': tele_util.sendChannelMessage(client,chat_name,message) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Sent message to ('+ chat_type +'): ' + chat_name) tele_util.disconnectClient(client) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Ending background event: Sending Telegram Message')
def deleteChat(username,telegram_username,chat_name,chat_type): print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Running background event: Deleting Telegram Chat') client = tele_util.getClient(username) if chat_type == 'Group': tele_util.deleteGroup(client,chat_name,telegram_username) elif chat_type == 'Channel': tele_util.deleteChannel(client,chat_name) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Telegram Group: ' + chat_name + ' deleted') tele_util.disconnectClient(client) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Ending background event: Deleting Telegram Chat')
def getAllChatMembers(username,chat_type,chat_name): print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Running background event: Get All Members') tele_chat_type = { 'Channel':Channel, 'Group':Chat, } telegram_chat = Telegram_Chats.objects.get(name=chat_name) client = tele_util.getClient(username) members, count = tele_util.getMembers(client,chat_name,tele_chat_type[chat_type]) telegram_chat.members = '_'.join(members) telegram_chat.save() print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Retreive all members for chat: ' + chat_name) tele_util.disconnectClient(client) print('[' + datetime.now().strftime("%Y-%m-%d %H:%M:%S") + '] Ending background event: Get All Members')
def faculty_telegram_SendMessage(requests): response = {"faculty_telegram_SendMessage": "active"} tele_chat_type = { 'Channel': Channel, 'Group': Chat, } # Redirect user to login page if not authorized and faculty try: processLogin.InstructorVerification(requests) except: logout(requests) return render(requests, 'Module_Account/login.html', response) message = requests.POST.get('message') telegram_chat_type = requests.POST.get('telegram_chat_type') telegram_chat_name = requests.POST.get('telegram_chat_name').replace( '_', ' ') print('Telegram Chat Type: ' + telegram_chat_type) print('Telegram Chat Name: ' + telegram_chat_name) print('Message: ' + message) requests.POST = requests.POST.copy() requests.POST['chat_name'] = telegram_chat_name try: client = tele_util.getClient(requests.user.email.split('@')[0]) if message == None or message == '': raise Exception('Please specify a message for broadcasting.') try: telegram_chat = Telegram_Chats.objects.get(name=telegram_chat_name) if not tele_util.dialogExists(client, telegram_chat_name, tele_chat_type[telegram_chat_type]): raise Exception( 'Chat does not exists within Telegram client therefore, message cannot be sent. Please remove chat from the system.' ) except OperationalError: raise Exception( 'Please wait a few minutes for the chat to be initialize, before sending a message. If the problem persist, please check with system administrator.' ) except ObjectDoesNotExist: raise Exception('Chat does not exists within database.') if requests.POST.get('datetime') == 'now' or requests.POST.get( 'setDate') == None: scheduled_datetime = datetime.now() else: scheduled_datetime = (datetime.strptime( requests.POST.get('setDate'), '%Y-%m-%dT%H:%M')) period = scheduled_datetime - datetime.now() tasks.sendMessage( username=requests.user.email.split('@')[0], chat_type=telegram_chat_type, chat_name=telegram_chat_name, message=message, schedule=period, ) except Exception as e: # traceback.print_exc() response['courses'] = requests.session['courseList_updated'] response['error_message'] = str(e.args[0]) return faculty_telegram_Base(requests, response) finally: tele_util.disconnectClient(client) response['message'] = 'Message successfully sent' return faculty_telegram_Base(requests, response)