How capture audio message receive or image receive in BotKit Facebook - image

I have been using Botkit Facebook Messenger and I can receive text messages from Facebook perfectly, however I can not capture audio messages, images or attachments.
Has anyone been able to capture these types of messages?
var Botkit = require('botkit');
var controller = Botkit.facebookbot({
access_token: process.env.access_token,
verify_token: process.env.verify_token,
})
var bot = controller.spawn({
});
// if you are already using Express, you can use your own server instance...
// see "Use BotKit with an Express web server"
controller.setupWebserver(process.env.port,function(err,webserver) {
controller.createWebhookEndpoints(controller.webserver, bot, function() {
console.log('This bot is online!!!');
});
});
// this is triggered when a user clicks the send-to-messenger plugin
controller.on('facebook_optin', function(bot, message) {
bot.reply(message, 'Welcome to my app!');
});
// user said hello
controller.hears(['hello'], 'message_received', function(bot, message) {
bot.reply(message, 'Hey there.');
});
controller.hears(['cookies'], 'message_received', function(bot, message) {
bot.startConversation(message, function(err, convo) {
convo.say('Did someone say cookies!?!!');
convo.ask('What is your favorite type of cookie?', function(response, convo) {
convo.say('Golly, I love ' + response.text + ' too!!!');
convo.next();
});
});
});

there is an example for stickers, images, and audio replies in the facebook starter project: https://github.com/howdyai/botkit-starter-facebook/blob/master/skills/sample_events.js
If you have trouble using them, feel free to create an issues on the github!

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!

Bot Framework web chat + speech + specific voice

I'm trying to get web chat with my bot (V4 bot and web chat) to work with the speech cognitive service, using a specific voice. I've got it almost working as per this sample and others in the same folder (https://github.com/Microsoft/BotFramework-WebChat/blob/master/samples/06.c.cognitive-services-speech-services-js/index.html)
The only part of the equation I'm missing is whether I can specify the voice. I can't find how to specify voice in the samples and the web chat source code.
This page linked to from the speech cog service docco (https://learn.microsoft.com/en-gb/azure/cognitive-services/speech-service/speech-synthesis-markup) mentions specifying voice inside the SSML but I don't want to have to somehow crack open and modify the SSML being generated by the bot if I can avoid it.
Does anyone have any idea if this is possible, and if so how?
Thanks
Lee
OK I came to an answer on this myself after looking at the pony fill code. Partial snippet below. Update the list of voice to locale mappings to match the voice you want to use for the specified locale.
const speechServicesPonyfillFactory = await window.WebChat.createCognitiveServicesSpeechServicesPonyfillFactory({ authorizationToken, region });
return options => {
const ponyfill = speechServicesPonyfillFactory(options);
var speechSynthesisUtterance = ponyfill.SpeechSynthesisUtterance;
var speechSynthesis = ponyfill.speechSynthesis;
speechSynthesis.getVoices = function () {
return [
{ lang: 'en-US', gender: 'Male', voiceURI: 'Microsoft Server Speech Text to Speech Voice (en-US, BenjaminRUS)' }
];
}
return {
SpeechGrammarList: ponyfill.SpeechGrammarList,
SpeechRecognition: ponyfill.SpeechRecognition,
speechSynthesis: speechSynthesis,
SpeechSynthesisUtterance: speechSynthesisUtterance
}
};
};
...
var ponyfillFactory = await createSpeechPonyfillFactory({ authorizationToken, region });
...
// Do the usual stuff from the sample to get auth token and region...
window.WebChat.renderWebChat({
directLine: directLine,
webSpeechPonyfillFactory: ponyfillFactory,
store
}, document.getElementById('webchat'));

MS BotBuilder : How can the bot receive parameters and initiate conversation at webchat start

I'm using the direct-line method to communicate with this bot :
var connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID || config.appId,
appPassword: process.env.MICROSOFT_APP_PASSWORD || config.appPassword
});
// Initialize bot
var bot = universalBot(connector);
var server = restify.createServer();
server.listen(process.env.port || port, function () {
console.log('%s listening to %s', server.name, server.url);
});
var botListener = connector.listen();
server.post('/api/messages', (req, resp) => {
token = req.query.token;
console.log(token); //prints the token to the terminal
botListener(req, resp);
});
var msg = new builder.Message()
.text(notification);
//.address(address)
bot.send(msg, function (err) {
// Return success/failure
res.status(err ? 500 : 200);
res.end();
});
In order to pro-actively send the message i still need the address of the user and conversation id.
Is there a way to obtain these information at the time this initialisation on the browser ;
var bot = {
id: params['botid'] || 'botid',
name: params['botname'] || 'botname',
screen: params['screen'] || null
};
BotChat.App({
directLine: {
//secret: params['s'],
token: params['t'],
//domain: params['domain'],
//webSocket: params['webSocket']
},
user: user, //Need to access this user object at server on the webchat start
bot: bot
}, document.getElementById("BotChatGoesHere"));
Or any other way where the bot can start the conversation when the user loads the html in the browser.
UPDATE : The conversationUpdate dialog serves for triggering and initiating a dialog, but how can I access the parameter (token) and user object sent along, inside conversationUpdate dialog?
Thanks
If I understand you correctly, you want your bot to prompt the user with something like Hi, what can I help you with today? the minute the webchat loads, right? I haven't tried the direct line, always used the provided iframe, and here's what I do in my bot to send the welcome message:
bot.on('conversationUpdate', (message) => {
(message.membersAdded || [])
.filter((identity) => identity.id == message.address.bot.id)
.forEach((identity) => {
const reply = new builder.Message()
.address(message.address)
.text("Hi, How can I help you today?");
bot.send(reply);
});
});
I believe what you are looking for is the backchannel. With this, you can send values to your bot. the backchannel documentation is at the bottom of the readme on that repo.

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

Socket.IO subscribe to multiple channels

I want to build a simple chat room system on top of Socket.IO where user can create a new chat room and then people can start chatting.
This sound simple but as the Socket.IO 0.9.4 I'm running now, after reading a few SO posts together with the document on socket.io, i'm getting more and more confused. So, I hope that someone can provide me with instruction that WORK with 0.9.4:
I need a way to subscribe to a room. The room name is chosen by user. When a message is posted in a room, the user should receive it. How should I write the server code, how should I write the client code?
A user can join multiple rooms simultaneously.
I want another system to send a message to all user in a certain room. This 'another system' send the message through a request handled by express. How would I write that request handler?
This is all pretty straightforward with the socket.io rooms feature. Take a look at the documentation on LearnBoost wiki.
https://github.com/LearnBoost/socket.io/wiki/Rooms
It allows for being connected to multiple rooms over a single socket. I put together a quick test with the following code.
Server
io.sockets.on('connection', function(client){
client.on('subscribe', function(room) {
console.log('joining room', room);
client.join(room);
})
client.on('unsubscribe', function(room) {
console.log('leaving room', room);
client.leave(room);
})
client.on('send', function(data) {
console.log('sending message');
io.sockets.in(data.room).emit('message', data);
});
});
Client
var socket = io.connect();
socket.on('message', function (data) {
console.log(data);
});
socket.emit('subscribe', 'roomOne');
socket.emit('subscribe', 'roomTwo');
$('#send').click(function() {
var room = $('#room').val(),
message = $('#message').val();
socket.emit('send', { room: room, message: message });
});
Sending a message from an Express route is pretty simple as well.
app.post('/send/:room/', function(req, res) {
var room = req.params.room
message = req.body;
io.sockets.in(room).emit('message', { room: room, message: message });
res.end('message sent');
});

Resources