def update_program(): folder_update = '../.hasobot_update' UpdateService.download_and_extract_update(folder_update) folder_path = os.path.dirname(os.path.abspath(sys.argv[0])) Util.copytree(folder_update + '/hasobot-master', folder_path) shutil.rmtree(folder_update) os.chdir(folder_path)
def __init__(self): super(UdpServer, self).__init__() self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) self.port = Util.get_setting('udp_port', cls=int) self.port_bc = Util.get_setting('udp_broadcast_port', cls=int)
def handle_getvideo(update: Update): telegram_id = BotUtil.get_telegram_id(update) video_len = int(BotUtil.get_message_arguments(update)) Util.capture_video(video_len) func_result, bot_result = get_file_stream(telegram_id=telegram_id, file_name='out.avi') TelegramBotManager().bot.send_document(chat_id=update.message.chat_id, document=func_result)
def check_and_update(): server_version = UpdateService.get_server_commit_number() current_version = Util.get_setting('current_version', 'update', int) if server_version != current_version: TelegramBotManager().send_broadcast_message('Bot is updating to new version!', UserAccessMode.ADMIN) ServiceManager().stop_services() UpdateService.update_program() Util.set_setting(server_version, 'current_version', 'update') print('Program is updated!') Util.restart_program()
def download_and_extract_update(path): file_url = Util.get_setting('repository_url', 'update') branch = Util.get_setting('branch', 'update', default='master') file_url = f'{file_url}/archive/{branch}.zip' update_zip = requests.get(file_url) if os.path.exists(path) is False: os.mkdir(path) update_file = open(f'{path}/update.zip', 'wb') update_file.write(update_zip.content) update_file.close() update_file = zipfile.ZipFile(f'{path}/update.zip', 'r') update_file.extractall(f'{path}/') os.remove(f'{path}/update.zip')
def __init__(self, name, attributes: []): self.name = name if Util.check_list_type(attributes, Field) is False: print("given attributes type should be TableField") raise RuntimeError() else: self.attributes = attributes
def run(self): udp_enabled = Util.get_setting('udp_enabled', cls=bool) if udp_enabled is False or self.port is None: return self.socket.settimeout(10) self.socket.bind(('', self.port)) while True: self.read_package() self.send_broadcast_message()
def run_service(self): try: GPIO.output(22, GPIO.LOW) global blueled blueled = blueled + 1 if (blueled % 2) == 0: #logging.error('BB0\n') GPIO.output(23, GPIO.HIGH) blueled = 0 else: #logging.error('BB1\n') GPIO.output(23, GPIO.LOW) #logging.error('Alarm State'+ str(MiscMessageListener.alarm_state)) if MiscMessageListener.alarm_state == True: if GPIO.input(25): # if port 25 == 1 GPIO.output(22, GPIO.HIGH) global button button.wait_for_press() message = "Hareket algilandi! Video cekiliyor..." TelegramBotManager().send_broadcast_message( text=message, user_access=None) filter_result = BotDatabase().filter( Tables.USERS, lambda row: row[UsersTableMap.ACCESS] == UsersAccessMode.ADMIN) #logging.error('Capturing video\n') Util.capture_video(5) if filter_result.count > 0: for user in filter_result.rows: if UsersTableMap.CHAT_ID in user: chat_id = user[UsersTableMap.CHAT_ID] #Util.capture_video(5) file = open('out.avi', 'rb') TelegramBotManager().bot.send_document( chat_id=chat_id, document=file, timeout=300) #logging.error('blue led '+ str(blueled)+'\n') except Exception as e: TelegramBotManager().send_broadcast_message(text='Exception' + str(e), user_access=None) logging.error('Gpio run service error' + str(e))
def get_file_id(update: Update): file_id = None folder = Util.get_abs_file_name('./') if update.message.photo: file_id = update.message.photo[-1] folder = Util.get_abs_file_name(Util.get_setting('photo', 'media')) elif update.message.video: file_id = update.message.video.file_id folder = Util.get_abs_file_name(Util.get_setting('video', 'media')) elif update.message.document: file_id = update.message.document[-1] folder = Util.get_abs_file_name( Util.get_setting('document', 'media')) elif update.message.audio: file_id = update.message.audio[-1] folder = Util.get_abs_file_name(Util.get_setting('audio', 'media')) return file_id, folder
def get_server_commit_number(): page_content = requests.get(Util.get_setting('repository_url', 'update')) page_content = BeautifulSoup(page_content.content) commit_element = page_content.select('.commits a .num') if len(commit_element) <= 0: return None else: try: return int(commit_element[0].text.strip()) except ValueError as e: logging.error("Error : " + str(e)) return None
def initialize(self): token = Util.get_setting('token') updater = Updater(token=token) dispatcher = updater.dispatcher MessageManager.initialize() command_handler = MessageHandler(Filters.all, MessageManager.handle_messages) dispatcher.add_handler(command_handler) updater.start_polling() self.bot: Bot = updater.bot UdpServer().start() ServiceManager.load_services() ServiceManager.start_services() self.send_started_message()
def run_service(self): enable_auto_update = Util.get_setting('enable_auto_update', 'general', bool) if enable_auto_update is True: print("Update Service is running!") UpdateService.check_and_update()
def handle_temperature(update: Update): res, output, error = Util.execute("vcgencmd measure_temp") TelegramBotManager().bot.send_message(chat_id=update.message.chat_id, text=output)
def shutdown_bot(*, telegram_id): Util.execute("shutdown now -h")
def reboot_bot(*, telegram_id): Util.execute("reboot")
def exec_cmd_bot(*, telegram_id, command): res, output, error = Util.execute(command) if len(output) == 0: output = "Empty" return output
def __init__(self): update_interval = Util.get_setting('update_interval', cls=int) if update_interval is None: update_interval = 60 * 60 * 24 # check updates daily super(UpdateService, self).__init__(update_interval)
import os import logging from Core.Util.Util import Util from Core.TelegramBotManager import TelegramBotManager import atexit from Files.Database import DatabaseManager if __name__ == '__main__': try: program_path = os.path.dirname(__file__) if program_path is not '': os.chdir(program_path) logging.basicConfig( filename=Util.get_abs_file_name("hasobot.log"), format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) TelegramBotManager().initialize() # initialize bot and start except Exception as e: logging.error(str(e)) @atexit.register def on_exit(): DatabaseManager.DatabaseManager().close()
def __init__(self, file_name: str = "BotDB.db"): super().__init__(Util.get_abs_file_name(file_name)) self.create_table(Tables.USERS) self.create_table(LastIdsTableMap.TABLE_NAME)
import os import logging from Core.Util.Util import Util from Core.TelegramBotManager import TelegramBotManager if __name__ == '__main__': try: program_path = os.path.dirname(__file__) if program_path is not '': os.chdir(program_path) logging.basicConfig(filename=Util.get_abs_file_name("hasobot.log"), format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) TelegramBotManager().initialize() except Exception as e: logging.error(str(e))