Send slack message as any username - bots

I'm trying to create a new integration to combine slack with another chat system.
If someone sends a message from skype for example, I want his name to be used when the message is sent to slack. Right now I'm using IncomingWebhook to send messages.
private getWebClient(configuration: ConfigurationData): IncomingWebhook {
return new IncomingWebhook('https://hooks.slack.com/services/...');
}
const webhook = this.getWebClient(configuration);
await webhook.send(
{
text: 'How are you today?',
as_user: false,
username: 'slack_integration',
icon_emoji: ':chart_with_upwards_trend:',
thread_ts: event.ts
}
);
As you can see, I'm also trying to reply to the message in a thread. This is not working either. The following event I get looks as follows:
{ text: 'How are you today?',
bot_id: 'B9G8LHME1',
type: 'message',
subtype: 'bot_message',
ts: '1519830326.000061',
channel: 'C9G4BJ8BW',
event_ts: '1519830326.000061' }
The message still appears to be from my App. The name is "Slack Integration Test" (with a badge showing App right next to it).

Related

How to send message with viber bot to group chat?

Im trying to make a vibe bot that will only send announcements to group of people. The bot will not respond or interact with anyone, as its only and primary task is sending given text as an announcement to a group of people in the same chat.

I don’t understand:

a. What is wrong?
b. Why its not working?
c. Where to find the viber chat id?
d. Can I hook up this bot to any chat group (group/community/channel)?

const ViberBot = require('viber-bot').Bot;
const TextMessage = require('viber-bot').Message.Text;
const viberBot = new ViberBot({
authToken: process.env.VIBER_AUTH_TOKEN,
name: "...name...",
avatar: "...img-url..."
});
viberBot.sendMessage({ id: process.env.VIBER_GROUP_ID }, new TextMessage("hello world!"))
I triple-checked the auth tokens, and it's correct. But I still can't find the Viber Chat Id.
I tried to get the ending of a share link (to use it as the chat id), and the result I got was the following error:
{
status: 10,
status_message: 'webhookNotSet',
chat_hostname: 'SN-CHAT-16_'
}
{
status: 10,
status_message: 'webhookNotSet',
chat_hostname: 'SN-CHAT-20_'
}
{
status: 10,
status_message: 'webhookNotSet',
chat_hostname: 'SN-CHAT-10_'
}
Does anybody know how to fix this?

Error: Activity resulted into multiple skype activities bot FrameWork

I am trying to send an attachment using proactive messaging to a channel,
below is my code.
async function sendToChannelWithImage(message,channelId,img) {
MicrosoftAppCredentials.trustServiceUrl('');
var credentials = new MicrosoftAppCredentials('app-id', 'password');
var client = new ConnectorClient(credentials, { baseUri: 'https://smba.trafficmanager.net/us/' });
var conversationResponse = await client.conversations.createConversation({
bot: {
id: 'app-id',
name: 'test'
},
isGroup: true,
conversationType: "channel",
channelData: {
channel: { id: channelId }
},
activity: {
type: 'message',
text: message,
attachments: [img]
}
});
}
const img = {contentType: 'image/*',contentUrl: "https://theysaidso.com/img/qod/qod-inspire.jpg"};
message = 'test'
channelId = 'testid'
In this I am getting trying to send the message using bot framework,
what i have tried : https://learn.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-send-receive-attachments
Sending a proactive message to a Teams team channel is no different from sending a proactive message to a personal conversation, so you should be using sendToConversation instead of createConversation. To send a proactive message anywhere, you'll need to make sure you have the conversation ID. In Teams, it's also important to trust the service URL.
If you want to start a new thread in a team channel, you can do that by removing the thread ID from the conversation ID. See here: Microsoft Bot - Node SDK: How to post to a public channel *without replying to a prev. activity*
It looks like there's a sample about starting new threads in team channels if you'd like to check that out: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/58.teams-start-new-thread-in-channel

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 to send a private message to user on MSTeams?

I want to send a private message to user. Currently, what I have are:
user id
tenant id
bot
Here is my sample code to try to send message:
var address =
{
channelId: 'msteams',
user: { id: user.id },
channelData: {
tenant: {
id: "cscportal.onmicrosoft.com"
}
},
bot:
{
id: bot.id,
name: bot.name
},
serviceUrl: "https://smba.trafficmanager.net/amer-client-ss.msg/",
useAuth: true
}
var msg = new builder.Message().address(address);
msg.text("Hello! This is a sample message.");
msg.textLocale('en-US');
bot.send(msg);
My sample code does not work because of something wrong in my address. Can you help me to find a way to send a private message to user?
Note: I don't have conversation ID
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 create personal conversation. Please also read about Proactive Messages.
Here is the source code for Node.js sample for Proactive Messages.

Slack Bot Replying to his own message in Node js

I am trying to make a slack bot in nodejs that replys the user based on their input. But As far now the bot keeps replying to his own messages
this is the code for my bot
let Bot = require('slackbots');
// create a bot
let settings = {
token: 'xoxb-10584202949',
name: 'BotHelp'};
let bot = new Bot(settings);
bot.on('start', function() {
bot.postMessageToChannel('general', 'At your service');
});
bot.on('message',function (data) {
console.log(data);
if (data.username != "BotHelp" && data.subtype != 'bot_message'){
bot.postMessageToChannel('general', 'Yoooo');
}
});
The Console log for data Prints
{ type: 'hello' }
{ text: 'At your service',
username: 'BotHelp',
bot_id: 'B336WGVSM',
type: 'message',
subtype: 'bot_message',
team: 'T2ZAW44P3',
user_team: 'T2ZAW44P3',
channel: 'C303W2D4M',
ts: '1479877794.000266' }
{ type: 'presence_change',
presence: 'active',
user: 'U33QS0VEF' }
So why my validation is failing to check that the message has been sent from bot itself or not?
Thank you for your time
I had the same issue. The message event in slackbots fires for every event ie, user_typing, message_marked, desktop_notification. All events can be found here.
Now what i did to ensure my bot didn't send a message after receiving its own was:
bot.on('message',function (data) {
if (data.user != "Uxxxxxx" && data.message === 'message'){
bot.postMessageToChannel('general', 'Yoooo');
}
the data.user gives a unique id, you can get this by console.log(data) and find your user id.
Also, if you want to specify which message you want to listen to, say direct message. channel id inside data will start with a D. For private channels, the channel id starts with G
Hope this helps. I know there is much more to do here but this is the basic. And you can grow on this. I know I will be trying to :D
I know that the question is already answered but I will like to add my answer, because the slack api has made some changes.
if (data.type !== "message" || data.subtype === 'bot_message' ){
return;
}
else{
bot.postMessageToChannel('general', 'Yoooo');
}
If I were you I'd check if the parameters are really user_name and subtype . If it's right, I would try to check the type of the objects that are coming(Maybe there is a cast in objects coming from the Slack api). I've done a small project very similar to yours, using the same library slackbots and also a library I built myself,nodejslack(https://github.com/marcogbarcellos/nodejslack) but didn't get this issue.

Resources