告警通知增加短信接口
This commit is contained in:
		
							parent
							
								
									9173b256fa
								
							
						
					
					
						commit
						f9fa6f0c91
					
				@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					package com.aizuda.snailjob.common.core.alarm.attribute;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					public class SmsAttribute {
 | 
				
			||||||
 | 
					    private List<String> phoneNumbers; // 接收短信的手机号列表
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -0,0 +1,17 @@
 | 
				
			|||||||
 | 
					package com.aizuda.snailjob.common.core.alarm.sms;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import lombok.Data;
 | 
				
			||||||
 | 
					import org.springframework.boot.context.properties.ConfigurationProperties;
 | 
				
			||||||
 | 
					import org.springframework.context.annotation.Configuration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Configuration
 | 
				
			||||||
 | 
					@ConfigurationProperties(prefix = "snailjob.sms")
 | 
				
			||||||
 | 
					@Data
 | 
				
			||||||
 | 
					public class SnailJobSmsProperties {
 | 
				
			||||||
 | 
					    private boolean enabled;
 | 
				
			||||||
 | 
					    private String accountSid;
 | 
				
			||||||
 | 
					    private String authToken;
 | 
				
			||||||
 | 
					    private String appId;
 | 
				
			||||||
 | 
					    private String smsServerUrl;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -0,0 +1,63 @@
 | 
				
			|||||||
 | 
					package com.aizuda.snailjob.common.core.alarm.strategy;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.core.alarm.AlarmContext;
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.core.alarm.attribute.SmsAttribute;
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.core.alarm.sms.SnailJobSmsProperties;
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.core.enums.AlarmTypeEnum;
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.core.util.JsonUtil;
 | 
				
			||||||
 | 
					import com.aizuda.snailjob.common.log.SnailJobLog;
 | 
				
			||||||
 | 
					import lombok.RequiredArgsConstructor;
 | 
				
			||||||
 | 
					import org.springframework.stereotype.Component;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@Component
 | 
				
			||||||
 | 
					@RequiredArgsConstructor
 | 
				
			||||||
 | 
					public class SmsAlarm extends AbstractAlarm<AlarmContext> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private final SnailJobSmsProperties snailJobSmsProperties;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public Integer getAlarmType() {
 | 
				
			||||||
 | 
					        return AlarmTypeEnum.SMS.getValue(); // 返回短信类型的值
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public boolean syncSendMessage(AlarmContext alarmContext) {
 | 
				
			||||||
 | 
					        if (!snailJobSmsProperties.isEnabled()) {
 | 
				
			||||||
 | 
					            SnailJobLog.LOCAL.warn("短信功能未启用");
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try {
 | 
				
			||||||
 | 
					            String notifyAttribute = alarmContext.getNotifyAttribute();
 | 
				
			||||||
 | 
					            SmsAttribute smsAttribute = JsonUtil.parseObject(notifyAttribute, SmsAttribute.class);
 | 
				
			||||||
 | 
					            String message = alarmContext.getText();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // 示例:调用第三方短信发送API
 | 
				
			||||||
 | 
					            sendSms(smsAttribute.getPhoneNumbers(), message);
 | 
				
			||||||
 | 
					        } catch (Exception e) {
 | 
				
			||||||
 | 
					            SnailJobLog.LOCAL.error("发送短信失败:", e);
 | 
				
			||||||
 | 
					            return false;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Override
 | 
				
			||||||
 | 
					    public boolean asyncSendMessage(List<AlarmContext> alarmContexts) {
 | 
				
			||||||
 | 
					        for (AlarmContext context : alarmContexts) {
 | 
				
			||||||
 | 
					            asyncSendMessage(context);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return Boolean.TRUE;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private void sendSms(List<String> phoneNumbers, String message) {
 | 
				
			||||||
 | 
					        // 这里应使用具体的短信服务SDK或HTTP请求来发送短信
 | 
				
			||||||
 | 
					        // 示例伪代码:
 | 
				
			||||||
 | 
					        for (String phoneNumber : phoneNumbers) {
 | 
				
			||||||
 | 
					            // 实际发送逻辑
 | 
				
			||||||
 | 
					            System.out.println("发送短信至 " + phoneNumber + ": " + message);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -37,6 +37,12 @@ public enum AlarmTypeEnum {
 | 
				
			|||||||
     * Webhook
 | 
					     * Webhook
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    WEBHOOK(5),
 | 
					    WEBHOOK(5),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 短信
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    SMS(6),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ;
 | 
					    ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private final int value;
 | 
					    private final int value;
 | 
				
			||||||
 | 
				
			|||||||
@ -95,6 +95,13 @@ snail-job:
 | 
				
			|||||||
  log-storage: 7 # 日志保存时间(单位: day)
 | 
					  log-storage: 7 # 日志保存时间(单位: day)
 | 
				
			||||||
  rpc-type: grpc
 | 
					  rpc-type: grpc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sms:
 | 
				
			||||||
 | 
					    enabled: true
 | 
				
			||||||
 | 
					    account-sid: your_account_sid
 | 
				
			||||||
 | 
					    auth-token: your_auth_token
 | 
				
			||||||
 | 
					    app-id: your_app_id
 | 
				
			||||||
 | 
					    sms-server-url: https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Messages.json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  mail:
 | 
					  mail:
 | 
				
			||||||
    enabled: true
 | 
					    enabled: true
 | 
				
			||||||
    host: smtp.qq.com
 | 
					    host: smtp.qq.com
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user