Skip to content

Commit

Permalink
Add embeds
Browse files Browse the repository at this point in the history
Made it so the user can choose to add embeds or not
  • Loading branch information
Zephira58 committed Sep 12, 2022
1 parent 846c08a commit bfdcffd
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 31 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webhook_sender"
version = "1.1.0"
version = "1.2.0"
edition = "2021"
authors = ["Xanthus58 <xanthus58@protonmail.com>"]
license = "MIT"
Expand All @@ -15,4 +15,5 @@ eframe = "0.19.0"
reqwest = { version = "0.11", features = ["blocking", "json"] }
tokio = { version = "1.20.1", features = ["full"] }
egui-notify = "0.3.0"
self_update = "0.31.0"
self_update = "0.31.0"
webhook = "2.1.0"
126 changes: 105 additions & 21 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ pub struct MyApp {
webhook: String,
username: String,
avatar_url: String,
update_check: bool,
update_check: i32,
update_available: bool,
update_notifcation: bool,
update_notification: bool,
embed: bool,
embed_title: String,
embed_footer: String,
embed_footer_icon: String,
embed_image: String,
embed_thumbnail: String,
embed_field_title: String,
embed_field_value: String,
}

impl Default for MyApp {
Expand All @@ -34,9 +42,17 @@ impl Default for MyApp {
webhook: "".to_string(),
username: "Xans Webhook Sender".to_string(),
avatar_url: "https://cdn.discordapp.com/avatars/292971545956188160/eab559efa07f0f3dd13d21ac5f26c4ce.png?size=1024".to_string(),
update_check: false,
update_check: 0,
update_available: false,
update_notifcation: false,
update_notification: false,
embed: false,
embed_title: "".to_string(),
embed_footer: "".to_string(),
embed_footer_icon: "".to_string(),
embed_image: "".to_string(),
embed_thumbnail: "".to_string(),
embed_field_title: "".to_string(),
embed_field_value: "".to_string(),
}
}
}
Expand Down Expand Up @@ -74,26 +90,65 @@ impl eframe::App for MyApp {
ui.label("Notice: This application is not affiliated with Discord in any way.\nThe application will say message sent even if the webhook URL is invalid.");

ui.separator();
ui.label("*Required");
ui.horizontal(|ui| {
ui.label("Enter Webhook URL ");
ui.label("*Enter Webhook URL:");
ui.text_edit_singleline(&mut self.webhook);
});
ui.horizontal(|ui| {
ui.label("Enter Username ");
ui.add_space(22.0);
ui.label("Enter Username: ");
ui.add_space(25.0);
ui.text_edit_singleline(&mut self.username);
});
ui.horizontal(|ui| {
ui.label("Enter Avatar URL ");
ui.add_space(18.0);
ui.label("Enter Avatar URL: ");
ui.add_space(20.0);
ui.text_edit_singleline(&mut self.avatar_url);
});
ui.horizontal(|ui| {
ui.label("Enter a message ");
ui.add_space(23.0);
ui.label("*Enter a message:");
ui.add_space(21.0);
ui.text_edit_multiline(&mut self.message);
});

if self.embed {
ui.separator();
ui.horizontal(|ui| {
ui.label("Enter Embed Title: ");
ui.add_space(58.0);
ui.text_edit_singleline(&mut self.embed_title);
});
ui.horizontal(|ui| {
ui.label("Enter Embed Footer: ");
ui.add_space(46.0);
ui.text_edit_singleline(&mut self.embed_footer);
});
ui.horizontal(|ui| {
ui.label("Enter Embed Footer Icon: ");
ui.add_space(20.0);
ui.text_edit_singleline(&mut self.embed_footer_icon);
});
ui.horizontal(|ui| {
ui.label("Enter Embed Image URL: ");
ui.add_space(24.0);
ui.text_edit_singleline(&mut self.embed_image);
});
ui.horizontal(|ui| {
ui.label("Enter Embed Thumbnail URL: ");
ui.text_edit_singleline(&mut self.embed_thumbnail);
});
ui.horizontal(|ui| {
ui.label("*Enter Embed Field Title: ");
ui.add_space(23.0);
ui.text_edit_singleline(&mut self.embed_field_title);
});
ui.horizontal(|ui| {
ui.label("*Enter Embed Field Value: ");
ui.add_space(17.0);
ui.text_edit_singleline(&mut self.embed_field_value);
});
}

ui.separator();

ui.label("-Your message-");
Expand All @@ -115,35 +170,64 @@ impl eframe::App for MyApp {

let send_button = ui.button("Send message");
if send_button.clicked() && self.webhook.is_empty(){
println!("ERROR: Webhook URL not found!\n");
println!("\nERROR: Webhook URL not found!");
cb(self.toasts.error("Please enter a wehbook url"));
}
else if send_button.clicked() && self.message.is_empty() {
println!("ERROR: Message not found!\n");
println!("\nERROR: Message not found!");
cb(self.toasts.error("Please enter a message"));
}
else if send_button.clicked() {
send_message(&self.message, &self.webhook, &self.username, &self.avatar_url);
else if send_button.clicked() && self.embed == false {
send_message(&self.message, &self.webhook, &self.username, &self.avatar_url, );
cb(self.toasts.success("Message Sent!"));
self.message = "".to_string();
}
else if send_button.clicked() && self.embed_field_title.is_empty() || send_button.clicked() && self.embed_field_value.is_empty() {
cb(self.toasts.error("Embed field title and value are required!"));
println!("ERROR: Embed field title and value are required!");
}
else if send_button.clicked() && self.embed == true {
send_embed(&self.message, &self.webhook, &self.username, &self.avatar_url, &self.embed_title, &self.embed_footer, &self.embed_footer_icon, &self.embed_image, &self.embed_thumbnail, &self.embed_field_title, &self.embed_field_value);
cb(self.toasts.success("Message Sent!"));
self.message = "".to_string();
}

let embed = ui.checkbox(&mut self.embed, "Embed?");
if embed.clicked() {
&self.embed != &self.embed;
println!("\nEmbed: {}", &self.embed);
cb(self.toasts.success("Embed toggled!"));
}
});

//Various UI elements and checks for application updates
if !self.update_check {
self.update_check = true;
if self.update_check == 0 {
cb(self.toasts.info("Checking for updates... Please wait"));
println!("\nChecking for updates...");
}
if self.update_check == 100 {
self.update_check = 110;
let x = update();
if x.is_err() {
self.update_available = true
}
else {
cb(self.toasts.info("No update available"));
println!("\nNo update available");
}
}
else if self.update_check < 105 && !self.update_available {
self.update_check += 1;
}

if self.update_available {
ui.separator();
if !self.update_notifcation {
cb(self.toasts.info("Update avalible!"));
self.update_notifcation = true;
if !self.update_notification {
cb(self.toasts.info("Update available!"));
println!("\nUpdate available!");
self.update_notification = true;
}
ui.label("There is an update avalible!");
ui.label("There is an update available!");
ui.horizontal(|ui| {
ui.label("You can download the update from");
ui.hyperlink_to("here", "https://github.com/Xanthus58/webhook_sender/releases/latest");
Expand Down
42 changes: 35 additions & 7 deletions src/app/api_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ use self_update::cargo_crate_version;
use std::collections::HashMap;

pub fn get_insult() -> String {
println!("Fetching insult from https://insult.mattbas.org/api/insult...");
println!("\nFetching insult from https://insult.mattbas.org/api/insult...");
//View more details on how this works here (https://www.reddit.com/r/learnrust/comments/wz9flc/how_to_get_request_to_return_as_a_string/)
let x = reqwest::blocking::get("https://insult.mattbas.org/api/insult")
.expect("Get failed")
.text()
.expect("Couldn't get response body");
println!("Insult fetched!\n");
println!("Insult fetched!");
return x;
}

pub fn get_affirmation() -> String {
println!("Fetching affirmation from https://www.affirmations.dev/...");
println!("\nFetching affirmation from https://www.affirmations.dev/...");
let x = reqwest::blocking::get("https://www.affirmations.dev/")
.expect("Get failed")
.text()
Expand All @@ -25,27 +25,55 @@ pub fn get_affirmation() -> String {
.replace(":", "")
.replace("affirmation", "")
.replace('"', "");
println!("Affirmation fetched!\n");
println!("Affirmation fetched!");
return x;
}


use webhook::client::{WebhookClient, WebhookResult};
use webhook::models::NonLinkButtonStyle;


const IMAGE_URL: &'static str = "https://cdn.discordapp.com/avatars/312157715449249795/a_b8b3b0c35f3dee2b6586a0dd58697e29.png";

#[tokio::main]
pub async fn send_message(msg: &str, webhook: &str, username: &str, avatar_url: &str) {
println!("Sending message...");
println!("\nSending message...");
let message = msg.to_owned();
let message = message.as_str();

let mut request_body = HashMap::new();
request_body.insert("content", message);
request_body.insert("username", username);
request_body.insert("avatar_url", avatar_url);
println!("Message sent!\n");

reqwest::Client::new()
.post(webhook)
.json(&request_body)
.send()
.await;
println!("Message sent!");
}
#[tokio::main]
pub async fn send_embed(msg: &str, webhook: &str, username: &str, avatar_url: &str, embed_title: &str, embed_footer: &str, embed_footer_icon: &str, embed_image: &str, embed_thumbnail: &str, embed_field_title: &str, embed_field_value: &str) -> WebhookResult<()> {
let client = WebhookClient::new(webhook);
let webhook_info = client.get_information().await?;
println!("\nwebhook: {:?}", webhook_info);
println!("\nSending embed...");
client.send(|message| message
.content("")
.username(username)
.avatar_url(avatar_url)
.embed(|embed| embed
.title(embed_title)
.description(msg)
.footer(embed_footer, Some(String::from(embed_footer_icon)))
.image(embed_image)
.thumbnail(embed_thumbnail)
.author(username, Some(String::from(avatar_url)), Some(String::from(avatar_url)))
.field(embed_field_title, embed_field_value, false))).await?;
println!("Embed sent!");
Ok(())
}

pub fn update() -> Result<(), Box<dyn (::std::error::Error)>> {
Expand All @@ -57,6 +85,6 @@ pub fn update() -> Result<(), Box<dyn (::std::error::Error)>> {
.current_version(cargo_crate_version!())
.build()?
.update()?;
println!("Update status: `{}`!\n", status.version());
println!("Update status: `{}`!", status.version());
Ok(())
}

0 comments on commit bfdcffd

Please sign in to comment.