-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
252 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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,72 @@ | ||
name: Build Docker slack bot | ||
on: | ||
push: | ||
paths: | ||
- '.github/workflows/merge-docker-slack-bot.yaml' | ||
- 'analog-gmp/**' | ||
- 'config/subxt/**' | ||
- 'primitives/**' | ||
- 'tc-subxt/**' | ||
- 'tc-cli/**' | ||
- 'Cargo.toml' | ||
- 'Cargo.lock' | ||
- 'rust-toolchain.toml' | ||
branches: | ||
- 'development' | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.ref }} | ||
cancel-in-progress: true | ||
env: | ||
DOCKER_REPO: analoglabs/slack-bot | ||
jobs: | ||
set-tags: | ||
name: Get & set tags | ||
runs-on: ubuntu-latest | ||
outputs: | ||
commit_hash: ${{ steps.get-sha.outputs.sha }} | ||
commit_hash8: ${{ steps.get-sha.outputs.sha8 }} | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
- name: Get SHA | ||
id: get-sha | ||
run: | | ||
sha=$(git log -1 --format='%H') | ||
echo "sha=$sha" >> $GITHUB_OUTPUT | ||
echo "sha8=$(git log -1 --format='%H' | cut -c1-8)" >> $GITHUB_OUTPUT | ||
echo "SHA commit:" $sha | ||
build-binary: | ||
name: Build Docker image | ||
needs: ["set-tags"] | ||
runs-on: ["self-hosted", "container"] | ||
steps: | ||
- name: Fetch latest code | ||
uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
- name: Setup Cargo | ||
uses: actions-rust-lang/setup-rust-toolchain@v1 | ||
with: | ||
toolchain: stable | ||
target: x86_64-unknown-linux-musl,wasm32-unknown-unknown | ||
components: rust-src | ||
- name: Install musl deps | ||
run: sudo apt-get update && sudo apt-get install -y musl-tools | ||
- name: Rust cache | ||
uses: Swatinem/rust-cache@v2 | ||
- name: Build slack-bot | ||
run: cargo build --profile testnet -p tc-cli --target x86_64-unknown-linux-musl --features testnet,develop | ||
- name: Copy bin | ||
run: cp -r target/x86_64-unknown-linux-musl/testnet/slack_bot slack-bot-bin | ||
- name: Build and push | ||
uses: ./.github/actions/buildah-action | ||
with: | ||
image_name: ${{ env.DOCKER_REPO }} | ||
containerfile: ./config/docker/Dockerfile.slack-bot-release | ||
context: . | ||
volume: ${{ github.workspace }}/target:/build/target | ||
username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
registry: docker.io | ||
push: 'true' | ||
tags: ${{ needs.set-tags.outputs.commit_hash8 }},latest |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,6 @@ | ||
FROM ubuntu:22.04 | ||
WORKDIR /app | ||
|
||
COPY slack-bot-bin slack-bot | ||
|
||
ENTRYPOINT ["/app/slack-bot"] |
This file contains 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 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 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,89 @@ | ||
use anyhow::Result; | ||
use axum::Extension; | ||
use slack_morphism::prelude::*; | ||
use std::sync::Arc; | ||
use tokio::net::TcpListener; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let subscriber = tracing_subscriber::fmt() | ||
.with_env_filter("axum_events_api_server=debug,slack_morphism=debug") | ||
.finish(); | ||
tracing::subscriber::set_global_default(subscriber)?; | ||
rustls::crypto::ring::default_provider() | ||
.install_default() | ||
.expect("Failed to install rustls crypto provider"); | ||
server().await?; | ||
Ok(()) | ||
} | ||
|
||
fn error_handler( | ||
err: Box<dyn std::error::Error + Send + Sync>, | ||
_client: Arc<SlackHyperClient>, | ||
_states: SlackClientEventsUserState, | ||
) -> HttpStatusCode { | ||
tracing::error!("{:#?}", err); | ||
|
||
// Defines what we return Slack server | ||
HttpStatusCode::BAD_REQUEST | ||
} | ||
|
||
async fn oauth_install_function( | ||
resp: SlackOAuthV2AccessTokenResponse, | ||
_client: Arc<SlackHyperClient>, | ||
_states: SlackClientEventsUserState, | ||
) { | ||
tracing::info!("{:#?}", resp); | ||
} | ||
|
||
async fn command_event( | ||
Extension(_environment): Extension<Arc<SlackHyperListenerEnvironment>>, | ||
Extension(event): Extension<SlackCommandEvent>, | ||
) -> axum::Json<SlackCommandEventResponse> { | ||
tracing::info!("Received command event: {:?}", event); | ||
axum::Json(SlackCommandEventResponse::new( | ||
SlackMessageContent::new().with_text("Working on it".into()), | ||
)) | ||
} | ||
|
||
async fn server() -> Result<()> { | ||
let client: Arc<SlackHyperClient> = | ||
Arc::new(SlackClient::new(SlackClientHyperConnector::new()?)); | ||
|
||
let addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8080)); | ||
tracing::info!("Loading server: {}", addr); | ||
|
||
let oauth_listener_config = SlackOAuthListenerConfig::new( | ||
std::env::var("SLACK_CLIENT_ID")?.into(), | ||
std::env::var("SLACK_CLIENT_SECRET")?.into(), | ||
std::env::var("SLACK_BOT_SCOPE")?, | ||
std::env::var("SLACK_REDIRECT_HOST")?, | ||
); | ||
|
||
let listener_environment: Arc<SlackHyperListenerEnvironment> = Arc::new( | ||
SlackClientEventsListenerEnvironment::new(client.clone()).with_error_handler(error_handler), | ||
); | ||
let signing_secret: SlackSigningSecret = std::env::var("SLACK_SIGNING_SECRET")?.into(); | ||
|
||
let listener: SlackEventsAxumListener<SlackHyperHttpsConnector> = | ||
SlackEventsAxumListener::new(listener_environment.clone()); | ||
|
||
// build our application route with OAuth nested router and Push/Command/Interaction events | ||
let app = axum::routing::Router::new() | ||
.nest( | ||
"/auth", | ||
listener.oauth_router("/auth", &oauth_listener_config, oauth_install_function), | ||
) | ||
.route( | ||
"/command", | ||
axum::routing::post(command_event).layer( | ||
listener | ||
.events_layer(&signing_secret) | ||
.with_event_extractor(SlackEventsExtractors::command_event()), | ||
), | ||
); | ||
|
||
axum::serve(TcpListener::bind(&addr).await.unwrap(), app).await.unwrap(); | ||
|
||
Ok(()) | ||
} |