Skip to content

Instantly share code, notes, and snippets.

@IMS94
Created April 21, 2022 06:29
Show Gist options
  • Save IMS94/27b6f9b49b6be625587068bfc14895da to your computer and use it in GitHub Desktop.
Save IMS94/27b6f9b49b6be625587068bfc14895da to your computer and use it in GitHub Desktop.
A Ballerina program to get latest Ballerina tagged stackoverflow questions as notifications to the google chat
import ballerina/http;
import ballerina/time;
import ballerina/log;
configurable string chatId = ?;
configurable string chatKey = ?;
configurable string chatToken = ?;
// How often will this program be executed
configurable int intervalSeconds = 60 * 10;
type Queestion record {
string[] tags;
boolean is_answered;
int answer_count;
string link;
string title;
};
type QuestionsResponse record {
Queestion[] items;
};
public function main() returns error? {
time:Utc toDate = time:utcNow();
time:Utc fromDate = [toDate[0] - intervalSeconds, toDate[1]];
log:printInfo("Fetching new SO questions", interval = intervalSeconds, timeUnit = "seconds", fromdate = fromDate[0]);
http:Client stackoverflowClient = check new ("https://api.stackexchange.com/2.3");
QuestionsResponse response = check stackoverflowClient->get(string `/questions?order=desc&sort=creation&tagged=ballerina&site=stackoverflow&fromdate=${fromDate[0]}`);
log:printInfo("Fetched questions", fromdate = fromDate[0], numOfQuestions = response.items.length());
if response.items.length() == 0 {
log:printInfo("No new questions during this period");
return;
}
http:Client googleChatClient = check new ("https://chat.googleapis.com/v1/spaces");
string message = "*New StackOverflow Question(s):*";
foreach Queestion question in response.items {
message = message + "\n - " + string `<${question.link}|${question.title}> (Answered: ${question.is_answered ? "Yes" : "No"})`;
}
json body = {text: message};
json|http:ClientError chatResponse = googleChatClient->post(string `/${chatId}/messages?key=${chatKey}&token=${chatToken}`, body);
if chatResponse is http:ClientError {
log:printError("Failed to send message", 'error = chatResponse);
} else {
log:printInfo("Sent message", response = chatResponse);
}
log:printInfo("Finished execution");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment