본문 바로가기
coding/log

[개발일지] Class함수 이용한 슬랙 개발 플로우

by 코딩희송 2022. 5. 12.
  • class slack service 제작 :
import axios from 'axios';
import ProfileSchema from '';

class SlackService {
	private url: string = 'slack hooks services url';
    
    constructor(private profileSchema: ProfileSchema) {}
    
    private async send(payload: any) {
    	await axios.post(this.url, payload);
    }
    
    public async message() {
    	const payload = {
        	attachments: [{color: '#82D8EB'}]
        }
    }
    
    public async error({ solanaTag, customerPublicKey, code, desc, ... }) {
    	const env = process.env.NODE_ENV === 'development' ? 'Dev' : 'Prod';
        const ts = Date.now();
        const fields = [ ... ];
        
        if (network) fields.push({title: '', value: network.toUpperCase(), short: false});
        if (token) fields.push({title: '', value: token.toUpperCase(), short: false});
        fields.push({title: '', value: costomerPublicKey, short: false});
        
        ...
        
        const payload = { text: `*[${env}] ${desc}*`, attachments: [{ ... }] };
        await this.send(payload);
    }
}

export default SlackService;
  • use:
import ProfileSchema from './app/schema/profile';
import SlackService from './app/services/slack';

...

const slackService = new SlackService(profileSchema);
const apiService = new APIService(profileSchema, slackService); // 어디든 재사용 가능

...
class BroadcastService {
	...
    
    error(type: ErrorMessageType, code: string, desc?: string, data?: any) {
    	...
    	const message = { solanaTag: profile?.solanaTag, customerPublicKey: profile?.publicKey, code, desc, ...data};
        this.slackService.error(message);
    }
}

댓글