def generateBot(outputBotFile): config = configManager.getConfig() TOKEN = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) if deployUrl is None or deployUrl == sad._INIT_CONFIG_DEPLOY_URL: gaeBotErrorHandler.addError("Deploy Url is emprty", sad._CRITICAL_ERROR_) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) outputBotFile.write("from telegram import Update\n") outputBotFile.write("from flask import Flask, request\n\n") outputBotFile.write("app = Flask(__name__)\n") outputBotFile.write("global updater\n") outputBotFile.write("global TOKEN\n") outputBotFile.write("TOKEN = '" + TOKEN + "'\n") outputBotFile.write("updater = Updater(TOKEN)\n\n") if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: outputBotFile.write("@app.route('/' + TOKEN, methods=['POST'])\n") else: outputBotFile.write("@app.route('/" + webhookPath + "', methods=['POST'])\n") outputBotFile.write("def webhook_handler():\n") outputBotFile.write("\tif request.method == 'POST':\n") outputBotFile.write("\t\tlogging.info(request.get_json(force=True))\n") outputBotFile.write( "\t\tupdate = Update.de_json(request.get_json(force=True), updater.bot)\n" ) outputBotFile.write("\t\tupdater.dispatcher.process_update(update)\n") outputBotFile.write("\treturn 'ok'\n\n\n") outputBotFile.write( "@app.route('/set_webhook', methods=['GET', 'POST'])\n") outputBotFile.write("def set_webhook():\n") outputBotFile.write("\tdispatcher = updater.dispatcher\n") outputBotFile.write( "\tfunctionManager.functionManager.generateHandlers(dispatcher)\n") if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: outputBotFile.write("\ts = updater.bot.setWebhook('" + deployUrl + "' + TOKEN)\n") else: outputBotFile.write("\ts = updater.bot.setWebhook('" + deployUrl + webhookPath + "')\n") outputBotFile.write("\tif s:\n") outputBotFile.write("\t\treturn 'webhook setup ok'\n") outputBotFile.write("\telse:\n") outputBotFile.write("\t\treturn 'webhook setup failed'\n\n\n") outputBotFile.write("@app.route('/')\n") outputBotFile.write("def index():\n") outputBotFile.write("\treturn 'Hello World'\n") outputBotFile.close() gaeBotErrorHandler.handle()
def _initConfiguration(projectNameFlag=True, initProjectFlag=True, gitInitFlag=True, gitHerokuFlag=-1): config = configManager.getConfig() if gitInitFlag: logManager.printVerbose("Creating git...") commandManager.runGitInitCommand() gitHerokuFlag = -1 if projectNameFlag: logManager.printVerbose("Project name doesn't found") _getNewHerokuName(config) initProjectFlag = True if initProjectFlag: logManager.printVerbose("Project hasn't been created in heroku") erroFlag = False while True: if erroFlag: logManager.printVerbose( "The project can't created in heroku. Read the errors above and chose a new heroku project name" ) _getNewHerokuName(config) projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._HEROKU_URL gitUrl = sad._HTTPS_ + sad._HEORKU_GIT_URL + projectName + sad._GIT_EXTENTION commandManager.runHerokuCreateCommand(projectName) if _verifyProject(projectName): configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) configManager.set(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_, gitUrl) gitHerokuFlag = _verifyRemoteHeroku(gitUrl) break else: erroFlag = True if gitHerokuFlag == -1: logManager.printVerbose("Adding git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitAddRemoteCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl) if gitHerokuFlag == 0: logManager.printVerbose("Setting git remote heroku...") gitUrl = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_) commandManager.runGitSetRemoteUrlCommand(sad._DEPLOY_HEROKU_OPTION, gitUrl)
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Google Cloud installation...") _verifyGAEInstallation() logManager.printVerbose("Verifying Curl installation...") _verifyCurlInstallation() logManager.printVerbose("Verifying Google Cloud login...") while True: if not _verifyGAELogin(): logManager.printVerbose("Can't find google account") logManager.printVerbose("Google login...") _GAELogin() else: break config = configManager.getConfig() projectName = configManager.get(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) else: projectName = _getNewGAEName(config) if _verifyGAEProject(projectName): logManager.printVerbose( "The project has already been created in Google Cloud") token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) else: logManager.printVerbose("Project hasn't been created in Google Cloud") while True: _GAECreate(projectName) if _verifyGAEProject(projectName): token = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) deployUrl = sad._HTTPS_ + projectName + sad._GAE_URL_ configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, deployUrl) configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, token) break logManager.printVerbose( "The project can't created in Google Cloud. Read the errors above and chose a new Google Cloud project name" ) projectName = _getNewGAEName(config) _GAEsetProject(projectName) logManager.printVerbose("All Configurations... OK")
def destroy(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) logManager.printVerbose("WARNING: This action delete the bot in the cloud") token = inputManager.getInput("For security, paste here the bot token: ") if token == configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_): logManager.printVerbose("Deleting bot in the cloud...") if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.deleteCloudApp() elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.deleteCloudApp() else: logManager.printVerbose("Ufff, the tokens don't match")
def deploy(): config = configManager.getConfig() deployUrl = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION) if deployUrl is None or deployUrl == sad._INIT_CONFIG_DEPLOY_URL: gaeErrorHandler.addError("Deploy Url is emprty", sad._CRITICAL_ERROR_) webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION) if webhookPath is None or webhookPath == sad._INIT_CONFIG_WEBHOOK_PATH: webhookPath = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_TOKEN_OPTION_) gaeErrorHandler.handle() _createSkeleton() commandManager.runGAEDeploy() _deleteSkeleton() commandManager.runCurlCommand(deployUrl + "set_webhook") logManager.printVerbose("\nDeploying bot in Google Cloud...OK")
def deploy(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.deploy() elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.deploy()
def configure(): config = configManager.getConfig() hosting = configManager.get(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_HOSTING_OPTION_) if hosting == sad._DEPLOY_HEROKU_OPTION: herokuManager.initConfiguration() signal.signal(signal.SIGINT, herokuManager.sigintHandler) elif hosting == sad._DEPLOY_GAE_OPTION: gaeManager.initConfiguration() signal.signal(signal.SIGINT, gaeManager.sigintHandler)
def deleteCloudApp(): config = configManager.getConfig() projectName = configManager.get(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: commandManager.runGAEDeleteProject(projectName) configManager.set(config, sad._DEPLOY_GAE_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_, "") configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_DEPLOY_URL_OPTION, "") configManager.set(config, sad._CONFIG_RAVEGEN_SECTION_, sad._CONFIG_WEBHOOK_PATH_OPTION, "")
def initConfiguration(): logManager.printVerbose("Verifying configuration...") logManager.printVerbose("Verifying Heroku installation...") _verifyHerokuInstallation() logManager.printVerbose("Verifying Heroku login...") while True: if not _verifyHerokuLogIn(): logManager.printVerbose("Can't find heroku token") logManager.printVerbose("Heorku login...") _herokuLogIn() else: break config = configManager.getConfig() projectNameFlag = True initProjectFlag = True gitInitFlag = True gitHerokuFlag = True projectName = configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_PROJECT_NAME_OPTION_) logManager.printVerbose("Verifiying project name...") if projectName != None and projectName != sad._INIT_CONFIG_PROJECT_NAME: logManager.printVerbose("Project name found: " + projectName) projectNameFlag = False logManager.printVerbose("Verifiying project in heroku...") if _verifyProject(projectName): logManager.printVerbose( "The project has already been created in heroku") initProjectFlag = False if utils.file_Or_Directory_Exists(sad._ACTUAL_PATH, sad._GIT_DIR_): logManager.printVerbose("Git has already been created") gitInitFlag = False gitHerokuFlag = _verifyRemoteHeroku( configManager.get(config, sad._DEPLOY_HEROKU_OPTION, sad._CONFIG_GIT_OPTION_)) if gitHerokuFlag > 0: logManager.printVerbose("Git has already been configured") _initConfiguration(projectNameFlag, initProjectFlag, gitInitFlag, gitHerokuFlag) logManager.printVerbose("All Configurations... OK")
def test_get(section, option, expected): config = configManager.getConfig() assert configManager.get(config, section, option) == expected
def test_set(section, option, value): config = configManager.getConfig() configManager.set(config, section, option, value) assert configManager.get(config, section, option) == value