Discord Bot - Random Command + Embed - bots

Heyo!
So, I made a discord bot with a command that randomly picks an anime out of a list of variables. But since I am new I kind of no idea how to Combine that with an embed, meaning the Chose item is displayed in an embed.
var facts = ["Item1", "Item2", "Item 3", "Item 4" ];
var fact = Math.floor(Math.random() * facts.length);
if (message.content.startsWith (prefix + "random")){
message.channel.send(facts[fact])
}
I was trying several things but none of my attempts worth showing here - I hope this is enough of code getting the permission to post it here.

Assuming you're using Discord.js because your code looks like Javascript, have a look at AnIdiotsGuide about Discord.js's embeds in messages.
For a very basic embed, your code needs to be something along the lines of
if (message.content.startsWith (prefix + "random")){
message.channel.send({embed: {
color: 3447003, // Or any other color desired
description: "Here is your random Fact: " + facts[fact]
}});
}

Related

How to make a skypebot kick someone?

So if I say for example 'live:1234567_89' I want the bot to kick a certain member.
This is the code by now , it just send a message to group("/kick live:1234567_89"), no kick the member out of the group? how can kick member out of the group by skype Bot?
var activity2 = new Microsoft.Bot.Connector.Activity()
{
Type = ActivityTypes.Message,
Recipient = new ChannelAccount() { Id = conversationGroupId },
From = new ChannelAccount() { Id = botID },
Text = "/kick live:1234567_89"
};
var createMessage = new ConversationParameters()
{
Members = new ChannelAccount[] { new ChannelAccount() { Id = conversationGroupId }
},
Bot = new ChannelAccount() { Id = botID },
};
var conversation2 = await connector.Conversations.CreateConversationAsync(createMessage);
var response2 = await connector.Conversations.SendToConversationAsync(activity2, conversation2.Id);
I tested this and it doesn't appear to be the source of your issue with the BotFramework SDK. I don't know for certain, but it doesn't look to me that bots can perform slash commands in Skype. They either require special permissions (or are blanket restricted) or the commands are only recognized via keyboard entry (i.e., meaning the slash command is typed in first and then is followed by any other values vs. the whole text simply being entered in by the bot).
This doc lists all available commands, including '/kick' which is an admin level command. Regardless, any slash command I attempted to have the bot issue only came thru as text.
There are several resources available at the bottom of the link included above. Perhaps one of them can help shed more light on if, and how, this can be achieved.

Array length is not showing

I am using dialogflow to build a chatbot. Following is a sample question array but the format of the actual and the sample is same:
qstion[{
Question 1, What is 33+32?,
Okay, Question 2, What is 76+32?,
Okay, Last Question, Did you like the game?
}]
I am using the following code to get the length of the array:
app.intent('First', (conv) => {
const ans = conv.parameters.any;
if(ans == 65){
senddata[0] = qstion[0] + ans;
conv.add(qstion.length);
conv.ask(qstion[1]);
}
else{
conv.add('Please enter a valid number');
conv.ask(qstion[0]);
}
});
The app crashes here. Can you help me get the length of the array?
Update:
This is how it shows on the log when I use console.log(qstion) :
qstion[
'Question1, What is 33+32',
'Okay, Question2, What is 76+32',
'Okay, Last Question, Did you like the game'
]
As your question has the "dialogflow" tag I assume you are using the actions-on-google library. The problem you're seeing is not that your array length is wrong, but that you're using the wrong method to send a response.
In actions-on-google, you would call conv.ask or conv.close to ask a question or close the conversation respectively.
Your code snippet shows conv.add(qstion.length). However, in the library you are using conv.add does not exist, likely the cause of the crash. conv.add only works when using the new Actions Builder and the #assistant/conversation library, both of which aren't compatible with Dialogflow.
That is not a valid array, or rather a valid object in an array. I assume you meant for this to be an array of strings like
qstion = [
"Question 1",
"What is 33+32?",
"Okay",
"Question 2",
"What is 76+32?",
"Okay",
"Last Question",
"Did you like the game?"
]

How to insert a link to user profile or chat in message using telethon?

Composing new message in telethon I'm trying to make something like 'User (link) in chat (link) posted something', but failed.
According to https://github.com/LonamiWebs/Telethon/wiki/Special-links, I've tried links like tg://user?id=123 and tg://openmessage?chat_id=123, but that gives links in text that are not working.
Here's the example:
#client.on(events.NewMessage)
async def handler(event):
sender = await event.get_sender()
sender_id = event.sender_id
sender_link = 'tg://user?id=' + str(sender_id)
payload = '[%s] (%s) said something' % (sender.first_name, sender_link)
I'm expecting a message with hyperlinked username, but getting [Alex] (tg://user?id=123), and that link leads to nowhere.
You need to remove the space between [...] and (...). You should have [%s](%s).
Twit is right, but there are several alternatives. You can manually insert a MessageEntityMention to the parser, or you can use HTML parse_mode and the format. Note that both of these are better, consider for example a user named hello](tg://user?id=321)[. This user would not get a link, so you should avoid markdown as it's not possible to escape this.

dynamic prompt choices in bot-framework v4 (node.js)

I've got a prompt for an SMS bot in which the user can make multiple choices. I'm looking for a pattern for a ChoicePrompt that allows me to do this:
show multiple selections
then after the user selects and answer, re-prompt them to answer again
Remove their previous choice(s) and add an "exit" option to move on
Automatically end the step if they've selected everything.
I'd like to avoid creating a new prompt w/switch cases for each answer tier, as this pattern needs to be implemented in a lot of places...
Example:
bot: User, what do you do to relax?
Exercise
Read a book
Nothing
user: Exercise
bot: Exercise, cool. What else?
Read a book
Nothing else
user: Read a book
bot: OK, you've done everything so we're moving on!
The botframework don't have a ListPrompt that I can see, at least for v4. They do however, have Suggested Actions you can use for this!!! The Botbuilder-Samples repo has a Suggested Action sample that shows a list of three colors:
async onTurn(turnContext) {
// See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
if (turnContext.activity.type === ActivityTypes.Message) {
const text = turnContext.activity.text;
// Create an array with the valid color options.
const validColors = ['Red', 'Blue', 'Yellow'];
// If the `text` is in the Array, a valid color was selected and send agreement.
if (validColors.includes(text)) {
await turnContext.sendActivity(`I agree, ${ text } is the best color.`);
} else {
await turnContext.sendActivity('Please select a color.');
}
// After the bot has responded send the suggested actions.
await this.sendSuggestedActions(turnContext);
} else if (turnContext.activity.type === ActivityTypes.ConversationUpdate) {
await this.sendWelcomeMessage(turnContext);
} else {
await turnContext.sendActivity(`[${ turnContext.activity.type } event detected.]`);
}
}
An option would be to programatically create the array (in the example above, it's "const validColors") and if the reply is in the list of colors, recreate the array however you want without the chosen option.

botframework choice invalid response typed

I am using nodejs SDK for creating my bot with MSFT botframework.
The code snippet is as follows:
function(session, args, next){
builder.Prompts.choice(session, "Please select one of the options:", ['AAA', 'BBB','CCC'], {retryPrompt: "Invalid choice, Please pick below listed choices",
listStyle: builder.ListStyle.button,
maxRetries: 1
});
},
function(session,results){
if (results.response) {
//Do something
}
}
I have 2 questions:
I would like to navigate to a different dialog Flow in case the user types anything other then the options("AAA","BBB","CCC"). Is that possible?
I would like to change the retryPrompt everytime lets say pick the utterances from a list. Is that possible?
I would like to navigate to a different dialog Flow in case the user types anything other then the options("AAA","BBB","CCC"). Is that possible?
Yes, it's possible. You can define several dialogs with required waterfall steps related to the choices. Like:
bot.dialog('AAA',[...])
And leverage replaceDialog to redirect your user to new dialog flows:
(session,result)=>{
//result.response.entity should be one of string `AAA`,`BBB`,`CCC`
session.replaceDialog(result.response.entity);
}
I would like to change the retryPrompt everytime lets say pick the utterances from a list. Is that possible?
Yes, it's possible. According the choice definition, we can set options extends IPromptChoiceOptions which extends [IPromptOptions][2], we can find that retryPrompt?: TextOrMessageType;, dig into the source code for the definition of TextOrMessageType:
/**
* Flexible range of possible prompts that can be sent to a user.
* * _{string}_ - A simple message to send the user.
* * _{string[]}_ - Array of possible messages to send the user. One will be chosen at random.
...
...
*/
export type TextOrMessageType = string|string[]|IMessage|IIsMessage;
So we can set a string list for retryPrompt, bot builder will pick one randomly. Please try following:
builder.Prompts.choice(session, "Please select one of the options:", ['AAA', 'BBB', 'CCC'], {
listStyle: builder.ListStyle.button,
maxRetries: 1,
retryPrompt:['utterance 1','utterance 2','utterance 3','utterance 4']
});
As you can call a function in retryPrompt you can do both of them:
builder.Prompts.choice(
session,
'This is just a question?',
'Yes|No',
{ retryPrompt: particularRetry() }
);
In the above, you can do what you want in particularRetry function.
For example for the second question, you can call the new propmt with new choices.
You can see more details in this post.

Resources