Discord JDA using a local image in an embed? - discord

Is it possible to use a local image file as a thumbnail/image in an embedded message with Discord JDA?
For one of my commands i'm building an image programmatically and uploading it via the Imgur API before displaying it in an embedded message using the Imgur URL.
I know I can send the file to the channel directly but i'd like it to be contained within an embed that displays other relevant info.
Cheers

You can use attachment://filename.ext as described in the documentation for setImage.
For instance, if you have a file called cat-final-copy-final-LAST.png you can send it like this:
// the name locally is not cat.png but we can still call it cat.png when we send it with addFile
File file = new File("cat-final-copy-final-LAST.png");
EmbedBuilder embed = new EmbedBuilder();
// this URI "attachment://cat.png" references the attachment with the name "cat.png" that you pass in `addFile` below
embed.setImage("attachment://cat.png");
Then send it, with 5.X like this:
// this name does not have to be the same name the file has locally, it can be anything as long as the file extension is correct
channel.sendMessage(embed.build())
.addFiles(FileUpload.fromData(file, "cat.png"))
.queue();
Or with JDA 4.X:
// this name does not have to be the same name the file has locally, it can be anything as long as the file extension is correct
channel.sendMessage(embed.build())
.addFile(file, "cat.png")
.queue();

Related

Is there any way to get content of the ZenDesk Attachments instead of redirect URL?

I need to get ZenDesk ticket attachment content like encoded format. ZenDesk API provided only the content url. By using that content url I can only able to get the redirect page of that file. But I need to automate a process that file as Base64 encoded format. Thanks in advance.
Note : I tried to migrate ZenDesk to Salesforce via Dell Boomi.
I found a resolution for my problem and I guess it's the same as yours.
In salesforce apex code I got the url response from zendesk and I used subtring method to get URL of attachment.
After that I used Pagereferece to open the URL, see below:
String exampleMyResponse= '<html><body>You are being <a href="https://xxx.zdusercontent.com/attachment/000001/sdlfkashdf98709udfah?token=eyJhbGciOiJkaX46SgYrFzTEpYqUIzpQeNnl5BMBNoRnUOsgQj389Ei7nNcGOcfGYaavlqLL2qaIiLCJlbmMiOiJBMTI4Q0JDLUhTMjU2In0..U8oX8QnYBM1lZMb6rhQGRA.NC3Z9kHC9ZE6HhygIHHan6xWYvoPqziVx76CZ6vcNYHBuAjV-LmBclVJYumKWKXA_PDhXX27z977XKYrLJZSc85a6lJTEqd-V2mP7U6O6r0_6E9hO8CWaA1dyxYYWw8kUsgMFUaPr0wCupxm3NbDzT03ZwO6EBJj79x4UZdauiXfEUrSwdl1pPahlQE2VfFo8DprgX9GQHzRFm5lwMrhA3crogo8Ox';
**//You need to authorize your domain "https://xxx.zdusercontent.com"** in remote site
Pagereference pg = new Pagereference(exampleMyResponse.substringAfter('href="'));
**Blob b = pg.getContentAsPDF();**//Here you can use getContent() too for another type of file
//Example to save PDF
Attachment att = new Attachment(Name = 'stvm_4', Body = b, ContentType = 'application/pdf', ParentId='Sobject_Id');
insert att;

How can I send an image from an API from a Discord bot?

For context, this Discord bot gets a players displayname from the Hypixel API. It then uses the displayname to get an image from the Plancke API: gen.plancke.io/exp/${player.displayname}.png
I have been looking for ways of sending this image to a channel and I have found this way of doing it.
message.channel.send({files: [`gen.plancke.io/exp/${player.displayname}.png`]});
This method works with local files, but I am trying to send an image from an API, which in this case is gen.plancke.io/exp/${player.displayname}.png. When I run my code, it gives me an error saying it cannot find the file. How can I send the image from an API instead of a local file path?
Passing a link to files without using http:// or https:// will make it assume it's a file path, not a URL, that is why it "cannot find the file".
message.channel.send({files: [`https://gen.plancke.io/exp/${player.displayname}.png`]});
message.channel.send({ file: `http://gen.plancke.io/exp/${player.displayname}.png` });
This should do you justice. Also, errors will be thrown If the user's name is not found on the "API".
message.channel.send({ file: `http://gen.plancke.io/exp/${player.displayname}.png` }).catch(e => {
console.log("Invalid displayname provided.");
});
With this code, the errors will be logged clearly.

Sending attachments as an email from a form - NOT from local files

I have created a HTML email form which allows a user to enter To, subject, message, content and attachments however I cannot get the attachments to send.
I have researched online and came across many variations of this code:
messageBodyPart = new MimeBodyPart();
String filename = "/home/manisha/file.txt";
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
But is there a way of sending attachments input into the form instead of adding the file path to a file in the code?
Thanks
First, the files need to be uploaded from the browser to the server using the html form. Depending on what you're using to manage the uploaded data, you can store the file data in memory or in files on the server. If you store it in memory, you can use a ByteArrayDataSource instead of FileDataSource in your code above.

java mail attachments getting corrupted

This is my code for attaching the files to the mail:
Multipart mp=new MimeMultipart("mixed");
BodyPart mbody=new MimeBodyPart();
mbody.setHeader("Content-Type", "text/html; charset=us-ascii");
mbody.setHeader("Content-Transfer-Encoding","7bit");
mbody.setContent(content2, "text/html");
mp.addBodyPart(mbody);
for(File file:f){
BodyPart mbody2=new MimeBodyPart();
DataSource ds=new FileDataSource(file.getAbsolutePath());
mbody2.setDataHandler(new DataHandler(ds));
mbody2.setFileName(ds.getName());
mbody2.setHeader("Content-Type", "multipart/mixed");
mbody2.setHeader("Content-Transfer-Encoding", "base64");
mp.addBodyPart(mbody2);
}
m.setContent(mp);
content2 is the html content I am embedding in the E-mail, and I am adding files from an arraylist f.
The problem here is that although the files get attached and I receive the E-mail fine, I am unable to open the attachments because the data is corrupt. This happens for all the files I've tried to attach like jpegs, pdfs, spreadsheets, word docs and txt files.
I read here: https://community.oracle.com/thread/1589120 that this could happen because JavaMail uses encoding that messes up the binary data of the file and adding mbody2.setHeader("Content-Transfer-Encoding", "base64"); should fix the problem but that doesn't work for me.
Any ideas on what could be wrong?
Thanks
Time for some debugging...
First, remove all of the setHeader calls; some of them are wrong and none of them should be necessary.
Next, determine if the problem is on the sending end or the receiving end. Try multiple mail readers to see if they all have problems with the attachments.
Try sending plain text attachments. Are they also corrupted?
Post the protocol trace showing what happens when you send a simple message with a simple attachment that fails, so we can see if the message is being constructed correctly.
What version of JavaMail are you using?
What mail reader are you using to view the attachments?

How to get the attached file of a received mail using python and store it in a blob property field

I am trying to get the attached file from the received mail and store it as Blob property in the GAE datastore. I am using Google app engine Python.
The file should be added to datastore only if the file is a excel file. Following code shows the method i have used for this. In that class 'mail_message.attachments' will give a list of attachments of the received mail. From that we can get only the file name and file content . But here i have to get the format of the file for checking whether it's a excel file. so that's why i have used the following method.
class LogSenderHandler(InboundMailHandler):
def receive(self, mail_message):
file_format_supported=['application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet','application/vnd.ms-excel']
for payload_no in range(len(mail_message.original.get_payload())):
one_payload = mail_message.original.get_payload(payload_no)
if one_payload.get_content_type() in file_format_supported:
uploadfile=Files()
uploadfile.temp_file=one_payload
uploadfile.put()
this code gives me the error
Property temp_file must be convertible to a Blob instance (Blob() argument should be str instance, not instance)
what i want to do converting the payload type as "str instance" to store it as blob property.
**Is there any other method to find the file type after getting the attachment list using mail_message.attachments. File content got from this attachment list can be stored to GAE datastore as blob property after decoding **.

Resources