Trouble writing a proactive message Microsoft Teams Bot - microsoft-teams

I'm currently trying to send a proactive message with my Microsoft Teams bot by using the following example code that I see online:
var address =
{
channelId: 'msteams',
user: { id: userId },
channelData: {
tenant: {
id: tenantId
}
},
bot:
{
id: appId,
name: appName
},
serviceUrl: session.message.address.serviceUrl,
useAuth: true
}
var msg = new builder.Message().address(address);
msg.text('Hello, this is a notification');
bot.send(msg);
The only change made is that I use TeamsMessage instead of a regular Message because I get errors saying that Message isn't a class which has also confused me. The problem when I run the code is that it tells me that my bot doesn't have a function named 'send'. My bot extends the TeamsActivityHandler class. My question is how do I fix this issue?

You need the user’s unique ID and tenant ID to send a proactive message. Typically, these are obtained from a team context, either by fetching the team roster or when a user interacts with your bot in a channel. Please check documentation on how to send proactive message.
Here is the source code for Node.js sample for Proactive Messages.

Related

OnMembersAddedAsync is not getting called

I have Bot developed in .NET core 3.1 c#. I am sending Adaptive card in OnMembersAddedAsync. It ix expected that as soon as end customer open chat control it should send Adaptive card. This is working well in Azure web chat control & emulator. But when I added it web site and open chat control in web site it will not work. Control will wait for end customer to send some message & then it sends card. In console log of BotFrame.html I can see that DIRECT_LINE connection is established :
DIRECT_LINE/CONNECT_PENDING
DIRECT_LINE/UPDATE_CONNECTION_STATUS
DIRECT_LINE/UPDATE_CONNECTION_STATUS
DIRECT_LINE/CONNECT_FULFILLING
DIRECT_LINE/CONNECT_FULFILLED
My code for BotFrame.html is :
const store = window.WebChat.createStore( {}, ( { dispatch } ) => next => async action => {
console.log(action.type);
if ( action.type === 'DIRECT_LINE/INCOMING_ACTIVITY' ) {…..
}
}
Even in Bot Logs I cannot see OnMembersAddedAsync is called unless customer sends message. Am I missing anything. Same is working well in Azure web chat control & emulator.
This sample, 04.api/a.welcome-event, demonstrates best practice on how to setup Web Chat and your bot for sending a welcome message.
In short, as shown in the sample, you will send an event to your bot when direct line connects your bot and the client.
When the event is received by the bot, it will trigger the bot to send the actual welcome message/card/activity to the user via the client.
In Web Chat
const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
// When we receive DIRECT_LINE/CONNECT_FULFILLED action, we will send an event activity using WEB_CHAT/SEND_EVENT
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: { language: window.navigator.language }
}
});
}
In Your Bot
if (context.activity.name === 'webchat/join') {
await context.sendActivity('Welcome, friend!!');
}
Hope of help!

How to send pro-active message to all teams users using botbuilder v4 nodejs

I am able to send the pro-active messages using v3
const address =
{
channelId: 'msteams',
user: { id: '29:1smZ9yn-ggTRUgd2ytQzb7PeTwmMBJs-WVlm3sXGwgjklvxycg' },
channelData: {
tenant: {
id: ''
}
},
bot:
{
id: '',
name: ''
},
serviceUrl: 'https://smba.trafficmanager.net/in/',
useAuth: true
}
var msg = new builder.Message().address(address);
msg.text('Hello, this is a notification');
bot.send(msg);
But I want to send the pro-active messages using v4.I have a accessToken which I got from Oauth 2.0 (code-grant). In the docs everywhere it is mentioned to save the turnContext.activity when user messages bot and use it for future reference. But I want to send message to user even before he messages the bot. What I am trying to do is install bot to users teams (like slack apps) using oauth and ask questions to users.

User specific publish (subscriptions)

Here's a small issue I faced and couldn't find much info in the documentation. I am trying to create private chat messages. We have the following code to subscribe a user to a topic:
export const resolvers = {
Subscription: {
somethingChanged: {
subscribe: () => pubsub.asyncIterator('chat_messages'),
},
},
}
and to publish
pubsub.publish('chat_messages', { somethingChanged: { sender_id: 1, receiver_id: 2, message: 'test' }});
I have used onConnect to verify that the user is authenticated
const server = new ApolloServer({
typeDefs,
resolvers,
subscriptions: {
onConnect: (connectionParams, webSocket) => {
...
if (!authenticated) throw error
...
},
},
...
})
This works well when I want to subscribe users to a particular topic for example. But how do I implement, private user to user communication? I have tried the withFilter but can't seem to implement user specific authorization(with respect to a message) checks.
Here is a demo: https://github.com/mrdulin/apollo-server-express-starter/tree/master/src/subscription/demo-1
With these features:
jwt based auth for websocket connection
User channel which means who can receive message, who can not.
There are some conceptions you need know:
there are two types user: requestUser and subscribeUsers(include requestUser)
you should write the code in filterFn, for who can receive the message which requestUser send.
For example:
There are three subscribe users: s1(client-1), s2(client-2), s3(client-3)
When a request user(client-4) send a message(maybe mutation), you can get subscribe users and request users through context argument of filterFn.
According to these two type users' informations. You can write your own bussiness logic in filterFn to decide who can receive message, who can't.
P.S. beside context, you can get variables argument in filterFn from client. That will give more information to decide who can receive message and who can't
Sorry for my English!

bot framework not receiving Slack dialog.open form response

I am building bot with Bot Builder SDK for Node.js
The bot is for slack
I have configured the slack and botframework correctly, as per the doc Connect a bot to Slack
All communication between botframework, slack and node service is through HTTPS.
The framework is communicating with slack properly.
In Next step, I am working on slack dialog.open
First of all, I could not find any method in Bot Builder SDK, to open the slack dialog, I even tried to Implement channel-specific functionality
Finally I was able to open dialog with direct post to: https://slack.com/api/dialog.open
But when I submit the form, I am getting error:
We had some trouble connecting. Try again?
Please see the screenshot:
The console displays the following error:
Request URL: https://xxxxx.slack.com/api/dialog.submit
Response: {"ok":false,"error":"curl_error"}
Slack send payload with "type":"dialog_submission".
I have tested that by changing the "Interactive Components -> Request URL", to my bot service.
To make slack communicate directly to my bot service.
Reference: section "Add and Configure Interactive Messages (optional)" in Connect a bot to Slack
So, I am sure message is being sent from the dialog box, when I click on submit button.
The message is lost between slack and my bot service. when Request URL is set to: https://slack.botframework.com/api/Actions
I never receives any indication that service is receiving a message.
Please help me to resolve this problem.
Following is the code that I used to open the dialog:
let RESTClient = require('node-rest-client').Client;
let FrameworkClient = new RESTClient();
let Promise = require('bluebird');
openDialogBox: function(token, trigger_id, callback_id) {
return new Promise(function(resolve, reject) {
var dialog = {
"title": "Issue Details",
"submit_label": "Submit",
"callback_id": callback_id,
"elements": [
{
"type": "text",
"label": "Pickup Location",
"name": "loc_origin"
},
{
"type": "text",
"label": "Dropoff Location",
"name": "loc_destination"
}
]
};
var args = {};
args.headers = {Authorization: 'Bearer ' + token, 'Content-type': 'application/json; charset=utf-8'};
args.data = {"trigger_id": trigger_id, "dialog": dialog};
FrameworkClient.post("https://slack.com/api/dialog.open", args , function(data, response) {
if (data && data.ok) {
resolve(data);
} else {
reject();
}
});
});
}
Thanks
As per #JasonSowers (Engineer - Microsoft Bot Framework)
Microsoft Bot Framework do not support this right now.
They may support this in future.

slack api rtm direct message

I'm using a node package: slack-client to interact with the api at slack. Now with or without using slack-client how do I send a direct message from my bot to a user I want to specify? Here's what have so far with a plain socket connection:
var WebSocket = require('ws')
,ws2 = new WebSocket(myURL); //from rtm start
ws2.on('open', function() {
ws2.send({
"id": 333,
"type": "message",
"channel": "#user1", //User I want to send to
"text": "HEY!!!!"
});
});
ws2.on('message', function(message) {
console.log('received: %s', message);
});
I was hoping that message would go directly to me from the bot but nothing. I get a reply of type hello though? The send details above I got on another post about this but it doesn't work for me. The message Id was one I created.
Ok so when calling the rtm.start via the web api, you would get a list of DM's that would be open for various users otherwise you can easily just open an im with im.open. I'm using the node package slack-client as mentioned in my question so you can do this:
//name of user your bot wants to send a msg to.
var userTest = slack.getUserByName('user1');
slack.openDM(userTest.id, function(res)
{
TestMsg(res.channel.id, 'some other msg');//test function I'm using
});
Next is the TestMsg function:
function TestMsg(userChannelId, msg)
{
request.post({url: 'https://slack.com/api/chat.postMessage',
form: { token: "xxxx-yourbot-token",channel: userChannelId,text: msg ,username: "yourBotNamehere", as_user: false}
}, function(error, response, body){
console.log(response.body);
});
}
I couldn't get it to work yet using the websockets send method but I suppose the api of postMessage will do for now as you can post richly formatted messages with postMessage. Hope this helps someone

Resources