빨리 슬랙의 슬래시 명령을 만들어보십시오.
소개
Serverless Framework 사용하면 비교적 간단하게 슬래시 커맨드 만들 수 있는 것은?라고 생각 얼마나 작성할 수 있는지 시험해 보았습니다.
별로 좋은 아이디어가 나오지 않았기 때문에 삼각 함수의 값을 돌려주는 슬래시 커멘드를 만들려고 합니다.
/tri-func sin 30
0.5
/tri-func cos 60
0.5
/tri-func tan 45
1.0
이런 느낌으로( sinh 라든지 conh 가 없는 것은 용감)
 API 만들기
 프로젝트 만들기
$ mkdir <project> && cd <project>
$ sls create -t aws-python3 -n tri-func  
$ tree
.
├── handler.py
└── serverless.yml
 pipenv 도입
표준 math 라이브러리를 사용해도 좋지만 최근 serverless-python-requirements 의 dockerizePip . 를 사용하여 삼각 함수의 값을 반환합니다.
이번에는 numpy와 pipenv를 사용하여 라이브러리를 사용합니다.
pipenv로 가상 환경 만들기 serverless-python-requirements 설치
$ pipenv install --python 3.6.5
$ pipenv install numpy
serverless-python-requirements 설치
$ sls plugin install -n serverless-python-requirements
 serverless.yml 편집
numpy를 지정하지 않으면 profile가 사용됩니다.
serverless.ymlservice: tri-func
provider:
  name: aws
  runtime: python3.6
  stage: stg
  region: ap-northeast-1
  profile: <hogehoge> #profileが複数あるので指定する
functions:
  get:
    handler: handler.get_tri
    events: #APIGatewayの設定
      - http: GET /
plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    usePipenv: true # pipenvを使用する
    dockerizePip: non-linux # amazonlinuxでビルドする
 handler.py 수정
handler.py
import json
import numpy as np
def get_tri(event, context):
    # event['multiValueQueryStringParameters']['text']にコマンドの引数が入ってくる
    text = event['multiValueQueryStringParameters']['text'][0]
    text = text.split()
    try:
        tri = text[0]
        theta = int(text[1])
        if tri == 'sin':
            result = np.sin(np.pi*theta/180)
        elif tri == 'cos':
            result = np.cos(np.pi*theta/180)
        elif tri == 'tan':
            result = np.tan(np.pi*theta/180)
        else :
            result = 'エラー'
    except Exception as e:
        result = 'エラー'
    body = {
        'text': '結果: {}'.format(result),
    }
    response = {
        'statusCode': 200,
        'isBase64Encoded': False,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(body)
    }
    return response
 배포
$ sls deploy -v
Serverless: Stack update finished...
Service Information
service: tri-func
stage: stg
region: ap-northeast-1
stack: tri-func-stg
resources: 9
api keys:
  None
endpoints: # URLをメモっておく
  GET - https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/stg
functions:
  get: tri-func-stg-get
layers:
  None
배포하면 엔드 포인트가 표시되므로 메모하십시오.
 슬랙에서 슬래시 명령 등록
Slack > 관리 > 사용자 정의 통합 > Slash > 설정 추가에서 Commands에서 슬래시 명령 작성
배포 시 표시되는 엔드포인트를 URL에 붙여넣기
 
그리고 창조!
 슬래시 명령을 사용해보기
우선 default , sin30 , cos60 에서 테스트
 
 
 
성공하네요 (자릿수는 용서해)
 결국 얼마나 걸렸습니까?
tan45 의 환경이 갖추어져 있으면 아마 10분 정도로 슬래시 커멘드를 만들 수 있는 것은 아닐까요.
덧붙여서 이 기사를 쓰면서 작성하고 있었기 때문에 나는 약 1시간 정도였습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(빨리 슬랙의 슬래시 명령을 만들어보십시오.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/thimi0412/items/b0888d565319f8b75082
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
/tri-func sin 30
0.5
/tri-func cos 60
0.5
/tri-func tan 45
1.0
프로젝트 만들기
$ mkdir <project> && cd <project>
$ sls create -t aws-python3 -n tri-func  
$ tree
.
├── handler.py
└── serverless.yml
pipenv 도입
표준
math 라이브러리를 사용해도 좋지만 최근 serverless-python-requirements 의 dockerizePip . 를 사용하여 삼각 함수의 값을 반환합니다.이번에는
numpy와 pipenv를 사용하여 라이브러리를 사용합니다.pipenv로 가상 환경 만들기
serverless-python-requirements 설치$ pipenv install --python 3.6.5
$ pipenv install numpy
serverless-python-requirements 설치
$ sls plugin install -n serverless-python-requirements
serverless.yml 편집
numpy를 지정하지 않으면 profile가 사용됩니다.serverless.yml
service: tri-func
provider:
  name: aws
  runtime: python3.6
  stage: stg
  region: ap-northeast-1
  profile: <hogehoge> #profileが複数あるので指定する
functions:
  get:
    handler: handler.get_tri
    events: #APIGatewayの設定
      - http: GET /
plugins:
  - serverless-python-requirements
custom:
  pythonRequirements:
    usePipenv: true # pipenvを使用する
    dockerizePip: non-linux # amazonlinuxでビルドする
handler.py 수정
handler.py
import json
import numpy as np
def get_tri(event, context):
    # event['multiValueQueryStringParameters']['text']にコマンドの引数が入ってくる
    text = event['multiValueQueryStringParameters']['text'][0]
    text = text.split()
    try:
        tri = text[0]
        theta = int(text[1])
        if tri == 'sin':
            result = np.sin(np.pi*theta/180)
        elif tri == 'cos':
            result = np.cos(np.pi*theta/180)
        elif tri == 'tan':
            result = np.tan(np.pi*theta/180)
        else :
            result = 'エラー'
    except Exception as e:
        result = 'エラー'
    body = {
        'text': '結果: {}'.format(result),
    }
    response = {
        'statusCode': 200,
        'isBase64Encoded': False,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps(body)
    }
    return response
배포
$ sls deploy -v
Serverless: Stack update finished...
Service Information
service: tri-func
stage: stg
region: ap-northeast-1
stack: tri-func-stg
resources: 9
api keys:
  None
endpoints: # URLをメモっておく
  GET - https://xxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/stg
functions:
  get: tri-func-stg-get
layers:
  None
배포하면 엔드 포인트가 표시되므로 메모하십시오.
슬랙에서 슬래시 명령 등록
Slack > 관리 > 사용자 정의 통합 > Slash > 설정 추가에서 Commands에서 슬래시 명령 작성
배포 시 표시되는 엔드포인트를 URL에 붙여넣기
 
그리고 창조!
 슬래시 명령을 사용해보기
우선 default , sin30 , cos60 에서 테스트
 
 
 
성공하네요 (자릿수는 용서해)
 결국 얼마나 걸렸습니까?
tan45 의 환경이 갖추어져 있으면 아마 10분 정도로 슬래시 커멘드를 만들 수 있는 것은 아닐까요.
덧붙여서 이 기사를 쓰면서 작성하고 있었기 때문에 나는 약 1시간 정도였습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(빨리 슬랙의 슬래시 명령을 만들어보십시오.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/thimi0412/items/b0888d565319f8b75082
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
우선
default , sin30 , cos60 에서 테스트


성공하네요 (자릿수는 용서해)
결국 얼마나 걸렸습니까?
tan45 의 환경이 갖추어져 있으면 아마 10분 정도로 슬래시 커멘드를 만들 수 있는 것은 아닐까요.
덧붙여서 이 기사를 쓰면서 작성하고 있었기 때문에 나는 약 1시간 정도였습니다.
                
                    
        
    
    
    
    
    
                
                
                
                
                    
                        
                            
                            
                            Reference
                            
                            이 문제에 관하여(빨리 슬랙의 슬래시 명령을 만들어보십시오.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
                                
                                https://qiita.com/thimi0412/items/b0888d565319f8b75082
                            
                            
                            
                                텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                            
                            
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)
                            
                            
                        
                    
                
                
                
            
Reference
이 문제에 관하여(빨리 슬랙의 슬래시 명령을 만들어보십시오.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/thimi0412/items/b0888d565319f8b75082텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
                                
                                
                                
                                
                                
                                우수한 개발자 콘텐츠 발견에 전념
                                (Collection and Share based on the CC Protocol.)