i am using the microsoft bot sdk in combination with an restify server (In package.json: "botbuilder": "^4.11.0"). I start a waterfall dialog that triggers a long running API. I save the the conversation reference and the id of the sent message to create an reply after the API call is completed:
replyToId = (await stepContext.context.sendActivity({ attachments: [ac]})).id; (in Dialog)
this.conversationReference = TurnContext.getConversationReference(context.activity); (in bot.ts)
After the completion of the API call, I want to create a reply to the last message of the dialog:
await this.adapter.continueConversation(this.conversationReference, async turnContext => {
await turnContext.sendActivity(newMessage);
});
newMessage is the Activity-object that contains further information for the user about the result of the API call.
The problem is that newMessage is not displayed as an reply to the existing message but as a separate message, although newMessage.replyToId is set to this.replyToId:
Additional information: Both messages, the last of the dialog and the "reply" are adaptive cards, but it does not make a difference if I send just simple text, same behaviour.
Would be grateful for any help :)
Instead of using "replyToId", put the id of the message you want to reply to, at the end of the conversation reference. As an example, if your conversationReference has a conversationId of 19:ac....cf#thread.skype, change it to: 19:ac...cf#thread.skype;messageid=12345678, where 12345678 is what you are currently using for "replyToId"
Related
I'm using the Microsoft bot-framework to create a bot and integrate it into teams.
Part of the bot's requirements include proactively messaging users once per day. From what I understand, I can only message users that has been added to the team/groupChat after the bot, or that have messaged the bot directly.
My question is - can I somehow bypass this limitation?
A friend of my referred me to a new feature of graphAPI, as part of the new beta version - https://learn.microsoft.com/en-us/graph/api/user-add-teamsappinstallation?view=graph-rest-beta&tabs=http.
To me it doesn't seem like it could be related to the solution since I'm not getting any data back in the response, so if I have no conversationReference object I still can't message the user.
At the moment my solution is to simply broadcast a message in the channel when it's added, asking users to "register" with it by messaging it. Anyone has any other suggestion?
The easiest way is to:
Install the bot for the team
Query the Team Roster -- The link in Step 3 has an alternative way to do this towards the bottom
Create a conversation with the user and send a proactive message
There's a lot of code in those links and it's better to just visit them than to copy/paste it here.
The end of Step 3 also mentions trustServiceUrl, which you may find handy if you run into permissions/auth issues when trying to send a proactive message.
Edit for Node:
Install Necessary Packages
npm i -S npm install botbuilder-teams#4.0.0-beta1 botframework-connector
Note: The #<version> is important!
Prepare the Adapter
In index.js
const teams = require('botbuilder-teams');
adapter.use(new teams.TeamsMiddleware());
Get the Roster
// Get Team Roster
const credentials = new MicrosoftAppCredentials(process.env.MicrosoftAppId, process.env.MicrosoftAppPassword);
const connector = new ConnectorClient(credentials, { baseUri: context.activity.serviceUrl });
const roster = await connector.conversations.getConversationMembers(context.activity.conversation.id);
Send the Proactive Message
const { TeamsContext } = require('botbuilder-teams');
// Send Proactive Message
const teamsCtx = TeamsContext.from(context);
const parameters = {
members: [
roster[0] // Replace with appropriate user
],
channelData: {
tenant: {
id: teamsCtx.tenant.id
}
}
};
const conversationResource = await connector.conversations.createConversation(parameters);
const message = MessageFactory.text('This is a proactive message');
await connector.conversations.sendToConversation(conversationResource.id, message);
Trust the ServiceUrl, as Necessary
Read about it. You'd want this before the message is sent.
MicrosoftAppCredentials.trustServiceUrl(context.activity.serviceUrl);
EDIT: The Graph API you've referenced is only necessary if you wish to proactively message a user who is not in a channel/groupChat where the bot is installed. If you need to proactively message only people who are in context where the bot is installed already, the answer from mdrichardson is the easiest possible method.
We've identified a couple of issues with the Graph API beta endpoint you referenced that should be fixed in the near term. In the meantime workarounds are as follows:
Calling:
POST https://graph.microsoft.com/beta/me/teamwork/installedApps/
{"teamsapp#odata.bind":"https://graph.microsoft.com/beta/appcatalogs/teamsapps/APP-GUID"}
Will install an app in the personal scope of a user.
Known issue: Currently, if the app contains a bot, then installation will not lead to creation of thread between the bot and the user. However to ensure that any missing chat threads, get created, call:
GET https://graph.microsoft.com/beta/me/chats?$filter=installedApps/any(x:x/teamsApp/id eq 'APP-GUID')
Calling:
GET https://graph.microsoft.com/beta/me/chats?$filter=installedApps/any(x:x/teamsApp/id eq 'APP-GUID')
Gets the chat between a user and an app containing a bot.
Known issue: Calling this API will lead to sending a conversation update event to the bot even though there were no updates to the conversation. Your bot will essentially get two install events and you'll need to make sure you don't send the welcome message twice.
We'll also be adding more detailed documentation for the proactive messaging flow using these Graph APIs
Looking at the docs for the SignalR bindings to send a message to a specified user you include the UserId property on the message as such -
[FunctionName("SendMessage")]
public static Task SendMessage(
[HttpTrigger(AuthorizationLevel.Anonymous, "post")]object message,
[SignalR(HubName = "chat")]IAsyncCollector<SignalRMessage> signalRMessages)
{
return signalRMessages.AddAsync(
new SignalRMessage
{
// the message will only be sent to these user IDs
UserId = "userId1",
Target = "newMessage",
Arguments = new [] { message }
});
}
This example is taken straight from the documentation, but the comment implies you message multiple userids, even though the property is a string and not an array.
How would you specify multiple users? (If for example, they are in a private chat channel together) Or is this mistake in the wording of the comment and you would need to send a message per user?
With other versions of SignalR I would put them in a group, but bindings for this do not exist for functions.
Group operations were introduced in the latest release.
Now you can:
Send a message to a group using GroupName in SignalRMessage
Add/remove user in a group using IAsyncCollector<SignalRGroupAction> output
Unfortunately just like the doc says, right now with Azure function binding we can only send message to one user or to all clients.
See the code of current extension SDK Microsoft.Azure.WebJobs.Extensions.SignalRService v1.0.0-preview1-10002.
It shows the extension has only two methods SendToAll and SendToUser.
Task SendToAll(string hubName, SignalRData data);
Task SendToUser(string hubName, string userId, SignalRData data);
The comment confused you is actually for old sample, the author forgot to modify it.
Good news is that support for group operation is under progress.
I was experimenting with a kik bot using Node.js, while I was trying to get a static keyboard to appear when user sends a 'help' message, it only sent the two replies and the static keyboard does not pop up. According to me it should work.
This is the function that sends the help messages:
/**
*
* #param {Message} message
*
*
*/
function help(message) {
message.reply('Hello!');
message.reply('Choose from the options to get an idea of what I can do! ;)');
message.addResponseKeyboard(['Rate me', 'Set reminder', 'Info']);
}
This is the bot configuration:
let bot = new Bot({
username: 'purppbot',
apiKey: 'dba843db-18bb-45fe-b6d6-3a678f420be2',
baseUrl: 'https://purppbot1-xbeastmode.c9users.io/',
staticKeyboard: new Bot.ResponseKeyboard(['Help', 'Info'])
});
I honestly don't know about Node.js; but as far as I see, I think you are expecting Static Keyboard to do what a Suggested Response Keyboard would do.
Regarding Static Keyboard, according to the API Reference of Kik docs, The static keyboard allows you to define a keyboard object that will be displayed when a user starts to mention your bot in a conversation, whilst regarding Suggested Response Keyboard, A suggested response keyboard presents a set of predefined options for the user.
It means the static keyboard is shown when a user starts to mention your bot in a conversation; and it disappears once a message is sent to the bot. And when the bot sends back a message to the user, it will contain its message(s) and Suggested Response Keyboard sent by the bot along with the message. In case no Suggested Response Keyboard is sent by the bot along with the message(s), the static keyboard is not shown until the user again starts to mention the bot's username.
So, in your case, you might want to send the those responses through Suggestive Responses Keyboard, which your bot would need to send along with the text message, every time a user sends the 'help' message.
I hope this helps.
I am building a slack bot using a third party service to handle responses based on inputs rather than just hard coding them into the bot. This service's API needs a client id & a conversation id to get the response. I found out that each time a slack bot receives a message, it creates a new message object each time so there isn't a way of keeping the clientID and conversation ID within the message object and have slack hold onto it.
rtm.on(RTM_EVENTS.MESSAGE, function(message // <-- new object each time the bot hears a message){
rtm.sendMessage('hello', message.channel);
});
So shortened down, does anyone know of a way to keep a conversation between a single user and the bot while holding onto some type of variable to hold the client and conversation ID?
You can store the message.user ID and track the conversation referring to that specific user. You will need to keep track of all ongoing conversations yourself. Something like this
rtm.on(RTM_EVENTS.MESSAGE, function(message // <-- new object each time the bot hears a message){
if(stored_conversations.indexOf(message.user) > -1){
//customize message depending on history
rtm.sendMessage('I remember you', message.channel);
}
});
Or, you could use Botkit - it manages bot-user conversations for you.
I am testing a bot that I am building using the Bot Framework. The emulator for local testing that Microsoft created has several events that can be provided to the bot to solicit a response.
I looked at the GitHub samples provided for Node.js here, but I can not find any example that responds to the different events within the Bot Framework Emulator.
The states are:
Bot Added to Conversation
Bot Removed from Conversation
User Added to Conversation
User Removed from Conversation
End of Conversation
Ping
Delete User Data
The API also does not make it clear how to achieve any of these actions.
Does anyone have any insight on where I should be looking for a example, or the API entries that I should be using?
In response to one of the answers, I did try code -
.onDefault(function (session) { console.log(session.message.type); }
But it only ever display "message" if a message was sent by the user.
The incoming message.type field will have "BotAddedToConversation" etc.
For the Node SDK, the botConnectorBot is able to trigger custom listeners on events using the on() handler.
Example
var builder = require('botbuilder');
var bot = new builder.BotConnectorBot({ appId: 'APPID', appSecret: 'APPSECRET' });
bot.on('DeleteUserData', function(message) {
// Handle Deleting User Data
});
More information can be found here.
You are also able to configure some standard messages using the configure() method.
Example
bot.configure({
userWelcomeMessage: "Hello... Welcome to the group.",
goodbyeMessage: "Goodbye..."
});
More information on what can be configured through options is located here.
Concerns
This is not part of the question, as the question was to identify how to listen to these events. But as a general concern, the event listener does not return a session object. It is unclear how to act once you handle the event.