Git Product home page Git Product logo

botbuilder-linebot-connector's Introduction

botbuilder-linebot-connector

Microsoft Bot Framework V3 connector for Line office account

npm version dependencies Status

Features

  • ready for Microsoft Bot Framework V3
  • no need a registered bot on dev.botframework.com, but require a certified line developer account, go to apply trial account
  • depend on line and line-message-api packages
  • support receiving and sending almost any line message types
  • for express framework
  • serverless framework support - connector.serverlessWebhock(event)

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

botbuilder-linebot-connector's People

Contributors

aakashkag avatar cataska avatar charx0r avatar dca avatar wolke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

botbuilder-linebot-connector's Issues

sticker 一直跑到heroCard去

s.send(new builder.Message(s)
/* Sticker */
.addAttachment(
new LineConnector.Sticker(s, 1, 1)
)
)
//sticker 一直跑到heroCard去,跳error

Cannot handle messages

2017-11-03T01:31:40.557744+00:00 app[web.1]: TypeError: _this.handler is not a function
2017-11-03T01:31:40.557745+00:00 app[web.1]:     at /app/node_modules/botbuilder-linebot-connector/dist/LineConnector.js:230:23
2017-11-03T01:31:40.557745+00:00 app[web.1]:     at Array.forEach (native)
2017-11-03T01:31:40.557746+00:00 app[web.1]:     at LineConnector.dispatch (/app/node_modules/botbuilder-linebot-connector/dist/LineConnector.js:164:21)
2017-11-03T01:31:40.557749+00:00 app[web.1]:     at /app/node_modules/botbuilder-linebot-connector/dist/LineConnector.js:251:23
2017-11-03T01:31:40.557749+00:00 app[web.1]:     at /app/node_modules/body-parser/lib/read.js:130:5
2017-11-03T01:31:40.557750+00:00 app[web.1]:     at invokeCallback (/app/node_modules/raw-body/index.js:224:16)
2017-11-03T01:31:40.557750+00:00 app[web.1]:     at done (/app/node_modules/raw-body/index.js:213:7)
2017-11-03T01:31:40.557751+00:00 app[web.1]:     at IncomingMessage.onEnd (/app/node_modules/raw-body/index.js:273:7)
2017-11-03T01:31:40.557751+00:00 app[web.1]:     at emitNone (events.js:86:13)
2017-11-03T01:31:40.557751+00:00 app[web.1]:     at IncomingMessage.emit (events.js:185:7)

Can't send multiple message when using promise/await

Hi all,

I just got a problem.

[Env]
push: true

[sample Code]
session.send("msg 1"); // => be run
await function();// => be run

session.send("msg 2"); // => not run
console.log("debug 1"); // => be run
session.endDialog("") // => not run

As below code, session can't run if wait some seconds from wait/promise function.

Anyone can help me ?
Thank in advance!

No way to send message using session.send

I keep having this error saying

Error: no way to send message: [object Object]
    at D:\home\site\wwwroot\node_modules\botbuilder-linebot-connector\line\LineConnector.js:748:23
    at Array.map (native)
    at LineConnector.send (D:\home\site\wwwroot\node_modules\botbuilder-linebot-connector\line\LineConnector.js:727:18)
    at D:\home\site\wwwroot\node_modules\botbuilder\lib\bots\UniversalBot.js:218:31
    at UniversalBot.tryCatch (D:\home\site\wwwroot\node_modules\botbuilder\lib\bots\UniversalBot.js:516:13)
    at D:\home\site\wwwroot\node_modules\botbuilder\lib\bots\UniversalBot.js:212:23
    at D:\home\site\wwwroot\node_modules\botbuilder\lib\bots\UniversalBot.js:536:17
    at D:\home\site\wwwroot\node_modules\async\lib\async.js:52:16
    at replenish (D:\home\site\wwwroot\node_modules\async\lib\async.js:314:29)
    at D:\home\site\wwwroot\node_modules\async\lib\async.js:326:29

Using the sample code provided in readme.

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()
    }
])

The first message Hello <<username>> is replied to my Line account correctly. So I expect that my Line settings are hooked correctly.

FYI, the App is hosted on Azure.

Does not reply message, no error visible / 程序既不回复信息,也不显示错误

2018-01-19T20:01:44.809942+00:00 heroku[router]: at=info method=POST path="/linebot" host=<Censored> request_id=6401d3b2-9897-467e-8940-94ff0de26d25 fwd=<Censored> dyno=web.1 connect=1ms service=5ms status=200 bytes=207 protocol=https
2018-01-19T20:01:44.806314+00:00 app[web.1]: event { type: 'message',
2018-01-19T20:01:44.806329+00:00 app[web.1]:   replyToken: 'bce62e26791b4b5893d298f9e45a7121',
2018-01-19T20:01:44.806331+00:00 app[web.1]:   source: { userId: 'U74e92ce16051d4b129c5f866e230f58b', type: 'user' },
2018-01-19T20:01:44.806332+00:00 app[web.1]:   timestamp: 1516392104124,
2018-01-19T20:01:44.806333+00:00 app[web.1]:   message: { type: 'text', id: '7337750864733', text: 'about' } }

In the script, I set up the connector like the following / 程序内的Line链接则以如下方式设置:

var builder = require('botbuilder');
var connector = new builder.ChatConnector({
    appId: process.env.appid,
    appPassword: process.env.appkey
});
var LineConnector = require("botbuilder-linebot-connector");
var lineConnector = new LineConnector.LineConnector({
    hasPushApi: false,
    channelId: process.env.line1,
    channelSecret: process.env.line2,
    channelAccessToken: process.env.line3
});
bot.connector("line", lineConnector);
...
server.post('/linebot', lineConnector.listen()); // Express

注:可以直接用繁体回复。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.