Is there rich card support for Cisco spark (now Webex teams) Bot - bots

I am implementing a cisco spark bot , which is now known as Webex teams. I am able to send receive simple text message from user. Is there a way to send rich card to user from Bot? I could not find any helpful documentation.
Here is my code I use to interact with bot
var SparkBot = require("node-sparkbot");
var SparkAPIWrapper = require("node-sparkclient");
// Starts your Webhook with default configuration where the SPARK API access
token is read from the SPARK_TOKEN env variable
var bot = new SparkBot();
var spark = new SparkAPIWrapper(process.env.SPARK_TOKEN);
bot.onMessage(function (trigger, message) {
if (message.personEmail != "mytestbot#webex.bot")
spark.createMessage(message.roomId, "You said " + message.text, {
"markdown":
true }, function (err, message) {
if (err) {
console.log("WARNING: could not post message to room: " +
message.roomId);
return;
}
});
});
Rich cards may contain buttons , links, list , thumbnail etc..

This is not supported yet on the platform side. The best you have at the moment is you can use markdown to style messages.

Related

Direct Messaging custom slack bot built on node.js

I have built a slack bot using the slack/bots apis in node.js: https://slack.dev/bolt-js/tutorial/getting-started
Currently, it is working fine when I type <bot> help in a channel I have set up for using webhooks. I am trying to run those same commands in a DM with the bot using the app.event('app_mention',...) method but it is not working. its like the message doesn't register in a DM with the bot for some reason but it works in a public channel. code snippet below:
app.event('app_mention', async ({ event, client}) => {
console.log(event);
const text = event.text;
const parentMessageId = event.ts;
const eventChannel = event.channel;
if (text.includes("help")) {
console.log(event);
try {
await client.chat.postMessage({
channel: eventChannel,
text: helpMessage,
thread_ts: parentMessageId
});
} catch (err) {
console.error(err);
}
I should have permissions set up correctly as well. I basically have all the permissions that I can add for the bot
The documentation of the app_mention api specifically mentions that this event does not work with DMs.
Messages sent to your app in direct message conversations are not
dispatched via app_mention, whether the app is explicitly mentioned or
otherwise. Subscribe to message.im events to receive messages directed
to your bot user in direct message conversations.
Check here : https://api.slack.com/events/app_mention

How to create Quick Replies using MS Bot Framework on Facebook Messenger?

I have been using Node.js and the MS Bot Framework(3.0) for my bot development needs for quite some time now.
One of my needs is to request a user to share its e-mail address with the bot.
Facebook offers a Quick Replies API exactly for that.
I am having a hard time understanding how should i utilize the framework to create a custom message with the quick reply option.
One of my first attempts was to pass native metadata to a channel using custom channel data
I have succeeded implementing various templates which are supported by Messenger platform, but quick replies are sort of other beast compared to buttons, lists and other templates. currently i struggle to create a quick reply message using the framework provided tools.
Please point me in the right direction.
You can send Facebook quick replies either through the source data in V3 of the BotFramework or through the channel data in V4 of the framework. See the two examples below:
Node
V4
await turnContext.sendActivity({
text: 'What is your email?',
channelData: {
"quick_replies":[
{
"content_type": "user_email"
}
]
}
});
V3
var message = new botbuilder.Message(session)
.text('What is your email?')
.sourceEvent({
facebook: {
"quick_replies":[
{
"content_type": "user_email"
}
]
}
});
session.send(message);
CSharp
V4
Activity reply = turnContext.Activity.CreateReply();
reply.Text = "What is your location?";
reply.ChannelData = JObject.FromObject( new {
quick_replies = new object[]
{
new
{
content_type = "location",
},
},
});
await turnContext.SendActivityAsync(reply, cancellationToken);
Hope this helps!
On v3 you can just add the JSON of the quick_reply template defined by facebook to the channeldata as JSON object (JObject)
reply.channelData = new JOBject("[JSON HERE]");

How do I get notifications when a bot sends a message in Teams?

I developed a bot for Microsoft Teams using the Microsoft Bot Framework v4 Nodejs SDK (botbuilder-sdk for nodejs). We have implemented the bot in such a way that, when we receive data using a REST API call from one of our CRMs, the data is posted to the channels on Microsoft Teams. However, when I do that, we do not receive a notification on the devices. Has anyone faced such an issue?
I am saving the context state initially. Everytime we receive data from a CRM, I am incrementing the activity id of the message (to send it as a new message and not a reply) and sending it to Microsoft Teams using context.sendActivity().
When we receive that adaptive card, we do not receive a notification in the activity feed or on any of the devices.
I have gone through all the steps as you described above. I've also gone through the troubleshooting steps. However, it still doesn't give me a notification for the card. However, when I initiate a conversation with the bot, I get a notification when the bot responds.
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
In this image, I get a notification when I get the TMS Bot started! message. However, I don't get a notification for the next two messages.
Edit: OP and I have exchanged a few emails to get this answered. This answer, as a whole, is good information for accomplishing Teams Proactive messaging, in general, but the main answer is in the last section, Simplified Code.
This is a long answer that covers many areas, simply because I'm not 100% sure I know what kind of notification you aren't receiving.
Troubleshooting
Troubleshooting Guide
Pay special attention to the many areas where notifications need to be enabled
In particular, the user may need to "Follow" and/or "Favorite" the channel to receive notifications from it
If a user has the desktop app open, they will receive the notification there and will not receive one on their phone unless they have been inactive on the desktop app for 3+ minutes. Otherwise, it's likely a bug in Teams.
Chat Notifications
If you've followed the Troubleshooting Guide linked above, your users should receive chat notifications. If not, you can try updating your MS Teams desktop or mobile client. As #KyleDelaney mentioned, it may be helpful to # mention users and/or channels
Activity Feed Notifications
You can also create Activity Feed Notifications. The gist of it is that you need to:
Include text and summary in the message
Include channelData that sets notifications.alert to true
This code will accomplish that:
const msg = MessageFactory.text('my message');
msg.summary = 'my summary';
msg.channelData = {
notification: {
alert: true,
},
};
return await dc.context.sendActivity(msg);
Result:
Note: If your bot only creates notifications and doesn't have conversations, you may benefit from creating a notifications-only bot.
Full Implementation Code
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
await turnContext.sendActivity(activity);
Result:
Using the Teams Extension
There's a Teams Extension for BobBuilder V4 that's currently in Beta, but seems to accomplish what you need. I believe the reason you weren't getting notifications while using the above is because your bot is creating a new reply chain in the channel and not replying directly to a user. I believe you can do all of this without the extension (by manually editing activity/context properties), but the extension should make it easier.
Here's the code I used to get working notifications within a channel:
In index.js (or app.js):
import * as teams from 'botbuilder-teams';
[...]
// Change existing to use "new teams.TeamsAdapter..."
const adapter = new teams.TeamsAdapter({
appId: endpointConfig.appId || process.env.microsoftAppID,
appPassword: endpointConfig.appPassword || process.env.microsoftAppPassword,
});
Wherever you're sending the message:
import * as teams from 'botbuilder-teams';
import * as adaptiveCard from '../src/adaptiveCard.json';
...
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
text: 'Test Card',
summary: 'my summary',
channelData: {
notification: {
alert: true,
},
},
};
const adapter = context.adapter as teams.TeamsAdapter;
await adapter.createReplyChain(context, [activity]);
Simplified Code
OP and I have emailed back and forth a bit and the key issue is that he needed to add the trustServiceUrl code from below. Normally, this manifests itself with a 500 error, but in this case, it appears to not create notifications.. After significant testing, here's all you really have to do to send different notifications to different channels. It basically amounts to setting a couple of properties of turncontext.activity and trusting the serviceUrl. No touching activity ID or using the Teams Extension at all. My code below is how I sent messages from Emulator that could then send cards to different Teams channels:
public onTurn = async (turnContext: TurnContext) => {
const dc = await this.dialogs.createContext(turnContext);
const dialogResult = await dc.continueDialog();
// Route message from Emulator to Teams Channel - I can send "1", "2", or "3" in emulator and bot will create message for Channel
let teamsChannel;
switch (turnContext.activity.text) {
// You can get teamsChannel IDs from turnContext.activity.channelData.channel.id
case '1':
teamsChannel = '19:8d60061c3d104exxxxxxxxxxxxxxxxxx#thread.skype';
break;
case '2':
teamsChannel = '19:0e477430ebad4exxxxxxxxxxxxxxxxxx#thread.skype';
break;
case '3':
teamsChannel = '19:55c1c5fb0d304exxxxxxxxxxxxxxxxx0#thread.skype';
break;
default:
break;
}
if (teamsChannel) {
const card = CardFactory.adaptiveCard(adaptiveCard);
const activity = {
attachments: [card],
summary: 'my summary',
text: 'Test Card',
};
const serviceUrl = 'https://smba.trafficmanager.net/amer/';
turnContext.activity.conversation.id = teamsChannel;
turnContext.activity.serviceUrl = serviceUrl;
// This ensures that your bot can send to Teams
MicrosoftAppCredentials.trustServiceUrl(serviceUrl);
await turnContext.sendActivity(activity);
} else {
[...Normal onTurn Code...]
await this.conversationState.saveChanges(turnContext);
}
Note: To receive notifications, you and your users must follow the channel.
I have gone through all the steps as you described above. I've also gone through the troubleshooting steps. However, it still doesn't give me a notification for the card. However, when I initiate a conversation with the bot, I get a notification when the bot responds.
https://i.stack.imgur.com/Bi4fc.png
https://i.stack.imgur.com/ab6uP.png
In this image, I get a notification when I get the TMS Bot started! message. However, I don't get a notification for the next two messages.

How can I post a welcome message to a newly joined user via a Slack bot?

I am trying to leverage Slack's Real-time messaging (RTM) API to post a welcome message to newly joined users of my team from a "Greeter Bot".
The event I'm listening for is team_join.
I have confirmed that the event is firing, and that messages can be sent to previously joined users out upon receiving that event. However, when trying to notify the newly joined user, nothing comes through.
I've played around with adding a delay (up to 45secs) after receiving the event before notifying the user, but still no dice.
Here my index.js file:
var SlackBot = require('slackbots');
var bot = new SlackBot({
token: process.env.SLACK_TOKEN || '',
name: process.env.BOT_NAME || 'greeterbot'
});
bot.on('message', function(data) {
var self = this;
if ('team_join' === data.type) {
setTimeout(function() {
var message = 'hello.';
// this works. 'some crusty old user' gets a DM message from my greeterbot.
self.postMessageToUser('some crusty old user', message, { as_user: true });
// FAIL. what am i doing wrong?!
self.postMessageToUser(data.user.name, message, { as_user: true });
}, 45000);
console.log("'" + data.user.name + "' has joined the team."); // works. the user name is present in the log.
}
});
I figured it out. There is a caching issue with the underlying bot library that I'm using.
For reference, here is a link to a PR that aims to address this issue.
https://github.com/mishk0/slack-bot-api/pull/25
Switching libs now.
Our Slack team, SKGTech.io uses Janitr that we wrote. Janitr is a Slack bot that welcomes new users based on your preferences.

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
});
}
});

Resources