Wolke / botbuilder-linebot-connector

MIT License
88 stars 16 forks source link
botframework linebot nodejs

botbuilder-linebot-connector

Microsoft Bot Framework V3 connector for Line office account

npm version dependencies Status

Features

LINE Messaging API for Node.js

About LINE Messaging API

Please refer to the official API documents for details.

Installation

npm install --save botbuilder-linebot-connector

Donate; Buy me a Beer

If you want to thank me, or promote your Issue.

Donate

Sorry, but I have work and support for packages requires some time after work. I will be glad of your support and PR's.

Document

Code Sample

javaScript

var builder = require("botbuilder")
var LineConnector = require("botbuilder-linebot-connector");

var express = require('express');
var server = express();

server.listen(process.env.port || process.env.PORT || 3980, function () {
    console.log("listening to");
});

var connector = new LineConnector.LineConnector({
    hasPushApi: false, //you have to pay for push api >.,<
    autoGetUserProfile:true, //default is false
    // your line
    channelId: process.env.channelId || "",
    channelSecret: process.env.channelSecret || "",
    channelAccessToken: process.env.channelAccessToken || ""
});

server.post('/line', connector.listen());

var bot = new builder.UniversalBot(connector)

var push = new builder.Message()
    .address(<any>a)
    .addAttachment(new ImageMap(<any>a,
        "test",
        "https://www.profolio.com/sites/default/files/styles/1920x1040/public/field/image/Bikini_Girls_adx.jpg?itok=uciEvomy",
        {
            "width": 1040,
            "height": 1040
        },
        []
    ))
bot.send(push);

bot.dialog("/", s => {
            try{
                let u = await connector.getUserProfile(s.message.from.id)
                console.log("u" + u)
                if (u === undefined) {
                    s.send("who said:" + s.message.text)
                } else {
                    s.send("hello " + u.displayName)
                }
            }catch(e){
                s.send("can`t get user profile!")
            }
            //image map

        s.send(new builder.Message(s).addAttachment(new ImageMap(s,
            "test",
            "https://www.profolio.com/sites/default/files/styles/1920x1040/public/field/image/Bikini_Girls_adx.jpg?itok=uciEvomy",
            {
                "width": 1040,
                "height": 1040
            },
            [
                {
                    "type": "uri",
                    "linkUri": "https://google.com/",
                    "area": {
                        "x": 0,
                        "y": 0,
                        "width": 333,
                        "height": 1040
                    }
                },
                {
                    "type": "message",
                    "label": "good",
                    "text": "hot",
                    "area": {
                        "x": 333,
                        "y": 0,
                        "width": 333,
                        "height": 1040
                    }
                },
            ]

        )));

    s.send(new builder.Message(s)
        /* Sticker  */
        .addAttachment(
            new LineConnector.Sticker(s, 1, 1)
        )
        /* Location  */
        .addAttachment(
            new LineConnector.Location(s, "my test", "中和", 35.65910807942215, 139.70372892916203)
        )
        /* Audio file */
        .addAttachment(
            new builder.AudioCard(s).media([{
                url: "https://xxx", //file place must be https
                profile: "music"
            }])
            /* Image file */
        ).addAttachment(
            new builder.MediaCard(s).image(builder.CardImage.create(s, 'https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/5d/6b/5d6b74b674e643f522ed68ef83053a1f.JPG'))
            /* Video file */
        ).addAttachment(
            new builder.MediaCard(s).media('https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/5d/6b/5d6b74b674e643f522ed68ef83053a1f.JPG').image(builder.CardImage.create(s, 'https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/5d/6b/5d6b74b674e643f522ed68ef83053a1f.JPG'))
        )
    );

    /* Dialog */
    s.send(new builder.Message(s)
        .addAttachment(
            new builder.HeroCard(s)

            .title("Classic White T-Shirt")
            .subtitle("100% Soft and Luxurious Cotton")
            .text("Price is $25 and carried in sizes (S, M, L, and XL)")
            .images([builder.CardImage.create(s, 'https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/11/b9/11b93df1ec7012f4d772c8bb0ac74e10.png')])

            .buttons([
                //left text message
                builder.CardAction.imBack(s, "buy classic gray t-shirt", "Buy"),
                //set timer
                new builder.CardAction().type("datatimepicker").title("time"),
                //postback
                builder.CardAction.postBack(s, "action=buy&itemid=111", "send data"),
                //open irl
                builder.CardAction.openUrl(s, "https://1797.tw", "1797")

            ])
        )
    )
        /* Carosuel */

        var msg = new builder.Message(s);
        msg.attachmentLayout(builder.AttachmentLayout.carousel)
        msg.attachments([

            new builder.HeroCard(s)
                .title("Classic White T-Shirt")
                .subtitle("100% Soft and Luxurious Cotton")
                .text("Price is $25 and carried in sizes (S, M, L, and XL)")
                .images([builder.CardImage.create(s, 'https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/11/b9/11b93df1ec7012f4d772c8bb0ac74e10.png')])
                .buttons([
                    builder.CardAction.openUrl(s, "https://1797.tw", "1797"),
                    new builder.CardAction().type("datatimepicker").title("time"),
                    builder.CardAction.postBack(s, "action=buy&itemid=111", "send data"),
                ]),
            new builder.HeroCard(s)
                .title("Classic Gray T-Shirt")
                .subtitle("100% Soft and Luxurious Cotton")
                .text("Price is $25 and carried in sizes (S, M, L, and XL)")
                .images([builder.CardImage.create(s, 'https://imagelab.nownews.com/?w=1080&q=85&src=http://s.nownews.com/5d/6b/5d6b74b674e643f522ed68ef83053a1f.JPG')])
                .buttons([
                    new builder.CardAction().type("datatimepicker").title("time"),
                    builder.CardAction.imBack(s, "buy classic gray t-shirt", "Buy"),
                    builder.CardAction.postBack(s, "action=buy&itemid=111", "send data"),
                ])
        ]);
        s.send(msg)

})

bot.dialog('leave'
    , s => {
        s.send("byebye");
        //only work in group or room
        connector.leave();

        s.endDialog()

    }
).triggerAction({
    matches: /^leave$/i
});

bot.on('conversationUpdate', function (message) {
    // detect event
    switch (message.text) {
        case 'follow':
            break;
        case 'unfollow':
            break;
        case 'join':
            break;
        case 'leave':
            break;
    }
    var isGroup = message.address.conversation.isGroup;
    var txt = isGroup ? "Hello everyone!" : "Hello " + message.from.name;
    var reply = new builder.Message()
        .address(message.address)
        .text(txt);
    bot.send(reply);
    bot.beginDialog(message.address, "hello")
});

bot.dialog("hello", [
    s => {
        builder.Prompts.text(s, "go");
    },
    (s, r) => {
        s.send("oh!" + r.response)
        s.endDialog()
    }
])

License

The MIT license

If you like this, Welcome to give me Star