Get Url of attachment (IMessage.Attachments) - c#

I'm trying to code a chat-log function into my bot. I know of IMessage.Attachments, but I don't know how to actually use it.
I don't have my exact code on hand (for now), but it looks like this:
_client.MessageReceived += async (message) =>
{
var attach = message.Attachments.Url as IMessage; //get Url of attachment
if ('message contains attachment (?)' && !message.Author.IsBot)
await message.Channel.SendMessageAsync("Attachment: " + attach); //send attachment
};
Of course, that doesn't work. The documentation didn't really help me either. It would be nice if someone could explain this to me.

Got it to work with this:
message.Attachments.FirstOrDefault().Url

Related

Delete Message When Detects A Certain Word

I am pretty new to making discord bots and have tried to make a filter to when my bot detects a certain word in a message it will delete it. It is throwing no errors, it just will not delete the message. I might just have the code in the wrong spot, but I do not know. If anyone can help me out, I'd appreciate it.
private async Task BadWordsWarn(SocketMessage message)
{
string[] badWords = File.ReadAllLines(Environment.CurrentDirectory + "/wordstodelete.txt");
if (badWords.Any(message.Content.Contains))
await message.DeleteAsync();
}
How are you calling BadWordsWarn? I suspect that you perhaps are not awaiting the async call, or badWords isn't actually being populated for the version of code you're running e.g. is the file being copied to the output directory if you're running the release version?
The C# code works - not optimal as the badWords array should be passed in as opposed to being read every time but there's nothing wrong with the syntax.
Perhaps the Discord message contents is in JSON and needs to be parsed?
If it's a string, this code is case-insensitive & definitely works.
var badWords = new [] {
"grape",
"apple"
};
var messageContent = "good good good grape";
foreach(var word in badWords) {
var contentHasBadWord = messageContent.IndexOf(word, StringComparison.OrdinalIgnoreCase) >= 0;
if (contentHasBadWord) {
Console.WriteLine("Bad word found - " + word);
// delete message
break;
}
}
dotnetfiddle

Azure Storage API "StartCopyFromBlob"

I am trying to copy a blob from one location to another and it seems like this method is obsolete. Everything I've read says I should use "StartCopy". However, when I try this it doesn't copy the blob. I just get a 404 error at the destination.
I don't seem to be able to find any documentation for this. Can anyone advise me on how to do this in the latest version of the API or point me in the direction of some docs.
Uri uploadUri = new Uri(destinationLocator.Path);
string assetContainerName = uploadUri.Segments[1];
CloudBlobContainer assetContainer =
cloudBlobClient.GetContainerReference(assetContainerName);
string fileName = HttpUtility.UrlDecode(Path.GetFileName(model.BlockBlob.Uri.AbsoluteUri));
var sourceCloudBlob = mediaBlobContainer.GetBlockBlobReference(fileName);
sourceCloudBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length > 0)
{
IAssetFile assetFile = asset.AssetFiles.Create(fileName);
var destinationBlob = assetContainer.GetBlockBlobReference(fileName);
destinationBlob.DeleteIfExists();
destinationBlob.StartCopyFromBlob(sourceCloudBlob);
destinationBlob.FetchAttributes();
if (sourceCloudBlob.Properties.Length != destinationBlob.Properties.Length)
model.UploadStatusMessage += "Failed to copy as Media Asset!";
}
I'm just posting my comment as the answer to make it easier to see.
It wasn't the access level of the container. It wasn't anything to do with StartCopy either. It turned out to be these lines of code.
var mediaBlobContainer = cloudBlobClient.GetContainerReference(cloudBlobClient.BaseUri + "temporarymedia");
mediaBlobContainer.CreateIfNotExists();
Apparently I shouldn't be supplying the cloudBlobClient.BaseUri, just the name temporarymedia.
var mediaBlobContainer = cloudBlobClient.GetContainerReference("temporarymedia");
There was no relevant error message though. Hopefully it'll save another Azure newbie some time in future.

how to send image from whatsapp C# api

I am not getting how to send image to any number using WhatsApiNet ?
case "/image":
byte[] imgData = File.ReadAllBytes(#"d:\My Creations\DSC_0423 copy.jpg");
wa.SendMessageImage(tmpUser.GetFullJid(), imgData, ApiBase.ImageType.JPEG);
break;!
But its not working..
I too was facing the same problem, fortunately I got the solution :)
byte[] img = File.ReadAllBytes("e:\\img.gif");
wa.OnConnectSuccess += () =>
{
MessageBox.Show("Connected to whatsapp...");
wa.OnLoginSuccess += (phoneNumber, data) =>
{
wa.SendMessage(to, msg);
wa.SendMessageImage(to + "#s.whatsapp.net",img,ApiBase.ImageType.GIF);
MessageBox.Show("Message Sent...");
};
}
You will just need to modify only 1 line in your code.
wa.SendMessageImage(tmpUser.GetFullJid()+ "#s.whatsapp.net", imgData, ApiBase.ImageType.JPEG);
Hop this will help others too :D Hav a Nyc day :)
See: Is it legal to use WhatsAPI?
There seems to be an indication of ongoing legal issues between WhatsApp and the creator of the API open-source project.
There's also mention that WhatsApp have changed some of the details around authentication, so it may be the case that you can't send the message simply because WhatsApp have stopped allowing access.

MailItem.Reply() doesn't reply inline

I want to be able to reply to an email inline, from within my program, but calling the reply function seems to only fill in the To: and Subject: field for me. I can't seem to find anything online about it and it doesn't seem to have an overload that turns on inline replies.
I found that each MailItem has a Conversation so I'm wondering if I have to manually do the inline reply? (I would really like to avoid that, it would be a pain to iterate through a conversation, some of ours can be hundreds of emails long).
I found that this code helped make this happen;
Document oldDoc = itemToReplyTo.GetInspector.WordEditor;
Document newDoc = newMailItem.GetInspector.WordEditor;
if (oldDoc != null)
{
Microsoft.Office.Interop.Word.Selection oldSelect = oldDoc.Windows[1].Selection;
Microsoft.Office.Interop.Word.Selection newSelect = newDoc.Windows[1].Selection;
oldSelect.Find.Execute("From:");
oldSelect.Collapse(WdCollapseDirection.wdCollapseStart);
oldSelect.MoveEnd(WdUnits.wdStory, 1);
oldSelect.Copy();
newSelect.Move(WdUnits.wdStory, 1);
newSelect.InlineShapes.AddHorizontalLineStandard();
newSelect.Paste();
newSelect.Move(WdUnits.wdStory, -1);
newSelect.InsertAfter("Reply Text here");
newSelect.Find.ClearFormatting();
newSelect.Find.Execute(mailItem.SenderEmailAddress);
}
I tried the below and it worked for me using Outlook 2010.
Outlook.MailItem reply1 = mailItem.Reply();
//prepend text to email
reply1.HTMLBody = "<html><body><strong>Test Body</strong><p><p></body></html>" + reply1.HTMLBody;
reply1.Display(false); //display message or .Send() to send

Blocking a contact with Skype4com

I'm not sure how to actually block or remove a user using 'SKYPE4COMlib' extention for Visual C#. I can change my status, return messages but I do not know how to do that.
If anyone has an idea of any other useful commands please list them here also.
User LiamaCloud is correct.
There's a IsBlocked property in User interface.
You can do something like this:
ISkype skype = _skype;
var tbb = skype.Friends.Cast<User>().Where(u => u.FullName.Contains("xxx");
foreach(User notAFriend in tbb)
{
notAFriend.IsBlocked = true;
MessageBox.Show(friend.FullName + " " + friend.IsBlocked);
}
I've tested it and it works.
Best

Categories

Resources