def run(event, context): channel = event.get('channel') png_data = event.get('png_data') s3_bucket = event.get('s3_bucket') s3_key = event.get('s3_key') title = event.get('title') team_id = event.get('team_id') aws_secrets_id = event.get('aws_secrets_id') if team_id == 'T7F3AUXGV': aws_secrets_id = 'slack-gs-bot' # hard coded values if team_id == 'T0SDK1RA8': aws_secrets_id = 'slack-gsbot-for-pbx' # need to move to special function bot_token = Secrets(aws_secrets_id).value() if png_data: #(fd, tmp_file) = tempfile.mkstemp('png') tmp_file = Files.temp_file('.png') with open(tmp_file, "wb") as fh: fh.write(base64.decodebytes(png_data.encode())) else: if s3_bucket and s3_key: tmp_file = S3().file_download_and_delete(s3_bucket, s3_key) else: return None return send_file_to_slack(tmp_file, title, bot_token, channel)
def jira(self): if self._jira is None: try: data = Secrets(self.secrets_id).value_from_json_string() server = data.get('server') #''https://jira.photobox.com' username = data.get('username') #'gsbot' password = data.get( 'password') # Secrets('GS_BOT_GS_JIRA').value() self._jira = JIRA({'server': server}, basic_auth=(username, password)) except Exception as error: print( '[API_JIRA] [error creating jira connection]: {0}'.format( error)) return self._jira
def setup(self): login_details = json.loads(Secrets(self.aws_secrets_id).value()) self.username = login_details.get('username') self.password = login_details.get('password') self.server_url = login_details.get('server_url') if self._browser is None: self._browser = API_Browser(headless=self.headless).sync__setup_browser() return self._browser
def _setup_Elastic_on_cloud_via_AWS_Secret(self, index, secret_id): credentials = json.loads(Secrets(secret_id).value()) self.host = credentials['host'] self.username = credentials['username'] self.password = credentials['password'] self.port = credentials['port'] self.index = index self._setup_Elastic_on_cloud(self.host, self.port, self.username, self.password) return self
def get_oauth_token(self, desired_scope): secret_data = json.loads(Secrets(self.gsuite_secret_id).value() ) # load secret from AWS Secrets store token_file = '/tmp/gmail_credential_{0}.json'.format( desired_scope ) # this is the tmp file with the token value for the desired scope if not Files.exists(token_file): # if the file does not exist if os.getenv('AWS_REGION') is not None or os.getenv( 'SYNC_SERVER' ): # check if we are running in AWS or in the sync server Files.write( token_file, secret_data['token.json'] ) # if we are, use the token.json value from the AWS secret_data else: secret_data = json.loads(Secrets( 'gsuite_token').value()) # BUG, need to refactor this credentials_file = '/tmp/gsuite_credentials.json' # file to hold the credentials.json value Files.write(credentials_file, secret_data['credentials.json'] ) # save value received from AWS into file store = file.Storage( token_file) # create a gsuite Storage object scopes = 'https://www.googleapis.com/auth/{0}'.format( desired_scope ) # full qualified name for the desired scopes flow = client.flow_from_clientsecrets( credentials_file, scopes) # create a gsuite flow object flags = argparser.parse_args( '--auth_host_name localhost --logging_level INFO'.split() ) # configure the use of a localhost server to received the oauth response run_flow( flow, store, flags ) # open browser and prompt user to follow the OAuth flow Files.delete( credentials_file ) # delete main gsuite credentials file (since we don't want it hanging around) return token_file # return file with token credentials
def upload_png_file(channel_id, file): bot_token = Secrets('slack-gs-bot').value() my_file = {'file': ('/tmp/myfile.png', open(file, 'rb'), 'png')} payload = { "filename": 'image.png', "token": bot_token, "channels": [channel_id], } requests.post("https://slack.com/api/files.upload", params=payload, files=my_file) return 'image sent .... '
def run(event, context): channel = event.get('channel') pdf_data = event.get('pdf_data') title = event.get('title') aws_secrets_id = event.get('aws_secrets_id') bot_token = Secrets(aws_secrets_id).value() (fd, tmp_file) = tempfile.mkstemp('pdf)') with open(tmp_file, "wb") as fh: fh.write(base64.decodebytes(pdf_data.encode())) return send_file_to_slack(tmp_file, title, bot_token, channel)
def setup(self): secret_data = json.loads(Secrets(self.secret_id).value()) storage_file = '/tmp/gmail_storage_token.json' Files.write(storage_file, secret_data['storage']) store = file.Storage(storage_file) creds = store.get() self.service = build('gmail', 'v1', http=creds.authorize(Http())) # note: 'storage.json file created using storage # SCOPES = 'https://www.googleapis.com/auth/gmail.readonly' # if not creds or creds.invalid: # flow = client.flow_from_clientsecrets(self.credentials_file, SCOPES) # flags = argparser.parse_args('--auth_host_name localhost --logging_level INFO'.split()) # creds = run_flow(flow, store, flags) return self
def send_message(channel_id, text, attachments=[]): bot_token = Secrets('slack-gs-bot').value() slack_url = "https://slack.com/api/chat.postMessage" data = urllib.parse.urlencode((("token", bot_token), # oauth token ("channel", channel_id), # channel to send message to ("text", text), # message's text ("attachments", attachments))) # message's attachments data = data.encode("ascii") request = urllib.request.Request(slack_url, data=data, method="POST") # send data back to Slack request.add_header("Content-Type", "application/x-www-form-urlencoded") context = ssl.SSLContext() response = urllib.request.urlopen(request, context=context).read() return json.loads(response.decode())
def resolve_bot_token(self, team_id): if team_id == 'T7F3AUXGV': return Secrets('slack-gs-bot').value() if team_id == 'T0SDK1RA8': return Secrets('slack-gsbot-for-pbx').value()
def setup(self, index = None): if index is None: index = self.index_id username = '******' password = Secrets(self.secret_id).value() return Elastic_Search(index)._setup_Elastic_on_cloud(self.host, self.port, username, password)
def config(self): if self._config is None: data = Secrets(self.secrets_id).value_from_json_string() self._config = (data.get('server'), data.get('username'), data.get('password')) return self._config
def resolve_bot_token(self, team_id): # to refactor if team_id == 'T7F3AUXGV': return Secrets('slack-gs-bot').value() if team_id == 'T0SDK1RA8': return Secrets('slack-gsbot-for-pbx').value() return Secrets('slack-gs-bot').value() # default to GS CST Slack org