botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

[Question] How to create message with Carousel layout of Hero Cards with images?

Open jer96 opened this issue 9 years ago • 6 comments

Bot Info

  • Bot handle: galahadchatbot
  • App ID: 58820dd1-80a9-4099-87db-2f880dbcd0c5
  • SDK Platform: Node.js
  • SDK Version: 6.9.1
  • Active Channels: Web Chat, Slack, Cortana
  • Deployment Environment: Azure Bot service

Issue Description

I am unable to read image files and display them into a Hero Card. I receive an error status 400 when sending message carrying HeroCard. I am attempting to create a carousel layout message containing 4 HeroCards with images located in a subdirectory of my application running within Azure.

Code Example

Code to create message with Hero Cards

var pic_names = [config.en.names.amy, config.en.names.sarah, config.en.names.rebecca, config.en.names.jasmine];            
var attachmentList = []
for (var i = 0 ; i < pic_names.length; i++){
      attachmentList.push(makeCard(session, pic_names[i]));
}

var msg = new builder.Message(session);
msg.attachmentLayout(builder.AttachmentLayout.carousel);
msg.attachments(attachmentList);
session.send(msg).endDialog();

Function to create HeroCard

function makeCard(session, name){
    var file_name = './images/' + name + '.png';
    var base64 = new Buffer(fs.readFileSync(file_name)).toString("base64");
    var contentType = 'image/png';
    return new builder.HeroCard(session)
        .title(name)
        .images([builder.CardImage.create(session, "data:image/png;base64," + base64)])
        .buttons([
            builder.CardAction.imBack(session, name, "Select")
        ]);
}

Error message

Error: Request to 'https://webchat.botframework.com/v3/conversations/308f8f8dd66647cab9fc08c43329f3ad/activities/308f8f8dd66647cab9fc08c43329f3ad%7C0000018' failed: [400] Bad Request
    at Request._callback (D:\home\site\wwwroot\node_modules\botbuilder\lib\bots\ChatConnector.js:532:46)
    at Request.self.callback (D:\home\site\wwwroot\node_modules\request\request.js:186:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (D:\home\site\wwwroot\node_modules\request\request.js:1163:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (D:\home\site\wwwroot\node_modules\request\request.js:1085:12)
    at IncomingMessage.g (events.js:291:16)
    at emitNone (events.js:91:20)

Reproduction Steps

  1. Store image files in a subdirectory
  2. Read images using file stream
  3. Create HeroCard using base 64 image
  4. Create message containing multiple HeroCards

Expected Behavior

A message with carousel layout of HeroCards should be displayed.

Actual Results

No message with carousel layout is displayed and server throws 400 error.

jer96 avatar Oct 20 '17 22:10 jer96

How large are the images you're trying to send? According to this Stack Overflow post, there's a max size beyond which the request will fail, although in that case the answerer mentions a 500 rather than a 400 status code.

sphanley avatar Oct 20 '17 23:10 sphanley

I did see that post. I'm only using 260 x 300 png images, so I'm not sure where the issue is coming from

jer96 avatar Oct 20 '17 23:10 jer96

@jer96 Is there a reason why you can't just serve the static image files via your bot's server?

Do you experience the same problem when you replace the images in your HeroCard(s) with static file URLs?

nwhitmont avatar Oct 23 '17 22:10 nwhitmont

@nwhitmont I do not experience the same problems when using static file URLs. How would I go about accessing my bot's server and serving the static image files?

jer96 avatar Oct 24 '17 17:10 jer96

@nwhitmont Thank you for your comment, just made the fix.

Currently using express to serve the static image files.

Server

const SITE = 'https://<bothandle>.azurewebsites.net/images/%s.png'
var server = express();
server.use('/images', express.static(__dirname + '/images'));
server.listen(process.env.port || process.env.PORT || 3978, function () {
    console.log('%s listening', server.name);
});

Code to create message with HeroCard(s)

var pic_names = [config.en.names.amy, config.en.names.sarah, config.en.names.rebecca, config.en.names.jasmine];     
var msg = new builder.Message(session)
      .attachmentLayout(builder.AttachmentLayout.carousel);
for (var i = 0; i < pic_names.length; i++) {
       msg.addAttachment(makeCard(session, pic_names[i]));
      }
msg.inputHint(builder.InputHint.acceptingInput);
session.sendBatch();
session.send(msg).endDialog();

Code to create HeroCard

function makeCard(session, name) {
    return new builder.HeroCard(session)
        .title(name)
        .images([builder.CardImage.create(session, util.format(SITE, name))])
        .buttons([
            builder.CardAction.imBack(session, name, "Select")
        ]);
}

jer96 avatar Oct 24 '17 18:10 jer96

this also may help you https://stackoverflow.com/questions/45041317/using-locally-stored-images-in-hero-card

JasonSowers avatar Oct 24 '17 19:10 JasonSowers