-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[feature] add smslocal sms notification #3135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f8706aa
[feature] Add SMSLocal notification service
a-little-fool 2fe9c1c
[improve] Add apache license
a-little-fool 5454099
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/ser…
a-little-fool 7e349bf
Update hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/con…
a-little-fool fc6406b
[improve] deal apache license
a-little-fool 0b8c4e7
[improve] format code
a-little-fool 4746951
Update hertzbeat-manager/src/main/resources/application.yml
a-little-fool d1693fb
Merge branch 'master' into smslocal-notice
yunfan24 c65bd91
fix
a-little-fool 5e9358b
fix
a-little-fool c48a46b
[feature] add smslocal sms notification
a-little-fool 7f259f7
[feature] add smslocal sms notification
a-little-fool 68ead56
[bugfix] fix
a-little-fool 5168a9a
[bugfix] fix
a-little-fool 3a00d8a
Update hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/p…
a-little-fool 9d1206d
[doc] add smslocal help-doc
a-little-fool File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/config/SmslocalSmsProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.alert.config; | ||
|
||
|
||
import lombok.Data; | ||
|
||
/** | ||
* Smslocal SMS Properties | ||
*/ | ||
@Data | ||
public class SmslocalSmsProperties { | ||
/** | ||
* SmsLocal account api key | ||
*/ | ||
private String apiKey; | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/SmsLocalSmsClientImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.alert.service.impl; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hertzbeat.alert.config.SmslocalSmsProperties; | ||
import org.apache.hertzbeat.alert.service.SmsClient; | ||
import org.apache.hertzbeat.common.constants.SmsConstants; | ||
import org.apache.hertzbeat.common.entity.alerter.GroupAlert; | ||
import org.apache.hertzbeat.common.entity.alerter.NoticeReceiver; | ||
import org.apache.hertzbeat.common.entity.alerter.NoticeTemplate; | ||
import org.apache.hertzbeat.common.support.exception.SendMessageException; | ||
import org.apache.hertzbeat.common.util.JsonUtil; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.apache.http.util.EntityUtils; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Smslocal SMS Client Implement | ||
*/ | ||
@Slf4j | ||
public class SmsLocalSmsClientImpl implements SmsClient { | ||
private static final String HOST = "secure.smslocal.com"; | ||
|
||
private static final String PATH = "/api/service/enterprise-service/external/sms"; | ||
|
||
private static final String FROM = "Hertzbeat"; | ||
|
||
private static final String SUCCESS_CODE = "200"; | ||
|
||
private final SmslocalSmsProperties config; | ||
|
||
public SmsLocalSmsClientImpl(SmslocalSmsProperties smslocalSmsProperties) { | ||
this.config = smslocalSmsProperties; | ||
} | ||
|
||
@Override | ||
public void sendMessage(NoticeReceiver receiver, NoticeTemplate noticeTemplate, GroupAlert alert) { | ||
if (Objects.isNull(receiver) || Objects.isNull(alert)) { | ||
log.warn("receiver and alert can not be null! receiver: {}, alert:{}", receiver, alert); | ||
return; | ||
} | ||
|
||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) { | ||
String content = alert.getCommonAnnotations().get("summary"); | ||
if (Objects.isNull(content) || Objects.isNull(alert.getCommonAnnotations().get("description"))) { | ||
content = alert.getAlerts().get(0).getContent(); | ||
} | ||
SmsMessage smsMessage = new SmsMessage(FROM, receiver.getPhone(), content); | ||
|
||
String payload = JsonUtil.toJson(smsMessage); | ||
|
||
HttpPost httpPost = new HttpPost("https://" + HOST + PATH); | ||
httpPost.setHeader("Content-Type", "application/json; charset=utf-8"); | ||
httpPost.setHeader("Token", config.getApiKey()); | ||
httpPost.setEntity(new StringEntity(payload, StandardCharsets.UTF_8)); | ||
|
||
log.debug("Sending SMS request to {}, payload: {}", httpPost.getURI(), payload); | ||
|
||
// send http request and handle response | ||
try (CloseableHttpResponse response = httpClient.execute(httpPost)) { | ||
int statusCode = response.getStatusLine().getStatusCode(); | ||
String responseBody = EntityUtils.toString(response.getEntity()); | ||
|
||
log.debug("SMS response status: {}, body: {}", statusCode, responseBody); | ||
|
||
if (statusCode != 200) { | ||
throw new SendMessageException("HTTP request failed with status code: " + statusCode); | ||
} | ||
|
||
JsonNode jsonResponse = JsonUtil.fromJson(responseBody); | ||
JsonNode jsonNode = jsonResponse.get(0); | ||
if (Objects.isNull(jsonNode)) { | ||
log.warn("jsonResponse parse errorCode failed: {}", jsonResponse); | ||
return; | ||
} | ||
String errorCode = jsonNode.get("errorCode").asText(); | ||
if (!SUCCESS_CODE.equals(errorCode)) { | ||
String msgid = jsonNode.get("id").asText(); | ||
throw new SendMessageException(errorCode + ":" + msgid); | ||
} | ||
|
||
log.info("Successfully sent SMS to phone: {}", receiver.getPhone()); | ||
} | ||
} catch (Exception e) { | ||
log.error("Failed to send SMS: {}", e.getMessage()); | ||
throw new SendMessageException(e.getMessage()); | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public String getType() { | ||
return SmsConstants.SMSLOCAL; | ||
} | ||
|
||
@Override | ||
public boolean checkConfig() { | ||
if (Objects.isNull(config) || Objects.isNull(config.getApiKey()) || config.getApiKey().isBlank()) { | ||
log.warn("smslocal properties can not be null: {}", config); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
private static class SmsMessage { | ||
String from; | ||
String to; | ||
String content; | ||
final int datacoding = 0; | ||
final String direction = "mt"; | ||
|
||
public SmsMessage(String from, String to, String content) { | ||
this.from = from; | ||
this.to = to; | ||
this.content = content; | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
hertzbeat-manager/src/main/java/org/apache/hertzbeat/manager/pojo/dto/SmslocalConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.apache.hertzbeat.manager.pojo.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
|
||
/** | ||
* Smslocal SMS configuration | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class SmslocalConfig { | ||
/** | ||
* Smslocal api key | ||
*/ | ||
private String apiKey; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.