How to reply with an image with discord bot - rust

I'm new to rust and as a practice project I'm building a bot using Serenity to handle the interactions with Discord. The bot should be able to reply to a message with an image. I can get the bot to post the image on the channel using CreateMessage like this:
let f = [(&tokio::fs::File::open("image.png").await?, "image.png")];
return match msg.channel_id.send_message(&context.http, |m| {
m.content(replied_message.author);
m.files(f);
return m;
}).await {
Ok(_) => Ok(()),
Err(why) => Err(CommandError::from(why)),
};
Unfortunately this method doesn't work with reply, which wants a content that implements std::fmt::Display. I could use MessageBuilder, but it constructs a string and I don't know how to add the image to that, unless I add an URL. The image is an image::DynamicImage instance and serving it from another service is unpractical to say the least.
How can I use message.reply_ping(&context.http, &reply) to send an image?

You can use the send_message() approach like in your existing code, with some small additions.
CreateMessage has a reference_message() method you can use to set the message to reply to. And it has the allowed_mentions() method to configure pings:
match msg
.channel_id
.send_message(&context.http, |m| {
// Reply to the given message
m.reference_message(&replied_message);
// Ping the replied user
m.allowed_mentions(|am| {
am.replied_user(true);
am
});
// Attach image
m.files(f);
m
})
.await
{
// ...
}

Related

How to make bot send Direct Message To Someone that the bot don't have it in mutal servers and have his id

I want to know the code that I can get the user that the bot isn't in his mutual servers and I have his id I want the bot to send him Direct Message what is the code?
I didn't try anything I'm a noob at this in learning
Use the Client.fetchUser() method.
Example 1:
let ID = '123456789012345678';
client.fetchUser(ID)
.then(user => user.send('hello'))
.catch(console.error);
Example 2 (must be within an async function):
let ID = '123456789012345678';
try {
let user = await client.fetchUser(ID);
await user.send('hello');
} catch(err) {
console.error(err);
}

How to forward message from channel to the groups on telegram bot?

im trying to implement my bot a function. Function that if the channel write any message it will be forwarded to the groups where the bot already is.
Trying to use scope method that worked like a charm on welcome message when new user joined the group.
//index.js
const Telegram = require('telegram-node-bot'),
tg = new Telegram.Telegram('MYAPI', {
workers: 1
});
const ForwardController = require('./controllers/forward')
tg.router.when(new Telegram.TextCommand('/info', 'infoCommand'), new InfoController())
.otherwise(new ForwardController());
//forward.js
const Telegram = require('telegram-node-bot');
class ForwardController extends Telegram.TelegramBaseController {
handle(scope) {
if ('channel' == scope.message.chat._type) {
scope.api.forwardMessage(scope.message._chat._id, _forwardFromChat._text);
}
}
}
module.exports = ForwardController;
I tried many combinations but the message is never forwarded... The bot is already administrator on the channel and is also putted in the groups. (Have also private message opened with bot so i think it should forward also there)
Take a look at the API reference for the library, the documentation page appears to be down so Github is your friend.
The forwardMessage call you are making has incorrect arguments and is accessing the private class variables. It is also returning a promise so you should await the promise or chain a .then to it. You can use the class methods on the Scope instance itself.
It should be more like:
// using async/await - note the containing function must be async for this approach
const result = await forwardMessage(<id of chat here>, scope.message().id());
// or to chain a .then
forwardMessage(<id of chat here>, scope.message().id())
.then(result => /* do something with result */)
.catch(err => /* handle the error */);
This will use the Scopes instance method and handle sending the id of the current chat for you, all you need is the id of the chat you want to send the message to and then replace the <id of chat here> with that id.

Private messaging a user

I am currently using the discord.js library and node.js to make a discord bot with one function - private messaging people.
I would like it so that when a user says something like "/talkto #bob#2301" in a channel, the bot PMs #bob#2301 with a message.
So what I would like to know is... how do I make the bot message a specific user (all I know currently is how to message the author of '/talkto'), and how do I make it so that the bot can find the user it needs to message within the command. (So that /talkto #ryan messages ryan, and /talkto #daniel messages daniel, etc.)
My current (incorrect code) is this:
client.on('message', (message) => {
if(message.content == '/talkto') {
if(messagementions.users) { //It needs to find a user mention in the message
message.author.send('Hello!'); //It needs to send this message to the mentioned user
}
}
I've read the documentation but I find it hard to understand, I would appreciate any help!
The send method can be found in a User object.. hence why you can use message.author.send... message.author refers to the user object of the person sending the message. All you need to do is instead, send to the specified user. Also, using if(message.content == "/talkto") means that its only going to run IF the whole message is /talkto. Meaning, you can't have /talkto #me. Use message.content.startsWith().
client.on('message', (message) => {
if(message.content.startsWith("/talkto")) {
let messageToSend = message.content.split(" ").slice(2).join(" ");
let userToSend = message.mentions.users.first();
//sending the message
userToSend.send(messagToSend);
}
}
Example use:
/talkto #wright Hello there this is a dm!

Messaging a user a bot does not know

I am using the Slack RTM node client and having a bit of an issue with DM's. Say a user joins the channel who has never DM'ed the bot before, the user types a command in the channel that the bot usually will respond to and by default the bot responds in a private message to the user. However, the bot cannot do this because the dataStore does not contain any DM data for this user. Code sample below...
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
user = rtm.getUserById(message.user);
console.log(user); // It gets the user object fine
dm = rtm.getDMByName(user.name);
console.log(dm); // This is always undefined unless the user has DM'ed the bot previously
});
Is there a way around this? I can't seem to find anything in the docs or code to suggest there might be.
You can use the im.open method of the web API. Here's roughly how you'd do it with #slack/client (untested, apologies in advance!):
var webClient = new WebClient(token);
...
rtm.on(RTM_EVENTS.MESSAGE, function (message) {
var dm = rtm.getDMById(message.user);
if (dm) {
console.log(`Already open IM: ${dm}`);
// send a message or whatever you want to do here
} else {
webClient.im.open(message.user, function (err, result) {
var dm = result.channel.id;
console.log(`Newly opened IM: ${dm}`);
// send a message or whatever you want to do here
});
}
});

How do I figure out which channels my bot is in?

Using MS Bot Framework, how do I figure out which channels my bot is in, on slack?
Is it possible, without doing something slack specific?
I have tried doing something with the message type BotAddedToConversation but without any luck.
Basically, I would like to write to a channel, without having a message to reply to.
In the Message object that you get in the Post function of your Api, the ChannelConversationId property contains the Channel.
public async Task<Message> Post([FromBody]Message message)
{
if (message.Type == "Message")
{
var channel = message.ChannelConversationId;
[...]
}
}

Resources