iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 24
0
var express = require('express');
import * as builder from "botbuilder";
import { LineConnector, Sticker, Location } from "./line/LineConnector"
import { CardAction } from "botbuilder";
var server = express();
server.listen(process.env.port || process.env.PORT || 3980, function () {
    console.log("listening to");
});


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

server.post('/line', connector.listen());
// var connector = new builder.ConsoleConnector().listen();

var bot = new builder.UniversalBot(connector)

bot.dialog('/', [
    s => {
        let m = new builder.Message(s)
            .text("hello world")
            .suggestedActions(
            builder.SuggestedActions.create(
                s, [
                    new CardAction().type("datatimepicker").title("time"),
                    new builder.CardAction().title("1").type("message").value("1"),
                    // builder.CardAction.openUrl(s, "https://1797.tw", "1797"),
                    // builder.CardAction.postBack(s, "action=buy&itemid=111", "send data")
                //這四種類型的按鈕都可以用了
                //datatimepicker 這個傳時間 滿有趣的

                ]
            ));
        s.send(m)
        s.send(new builder.Message(s)
            .addAttachment(
            new Sticker(s, 1, 1) //傳貼圖
            )
            
            .addAttachment(
            new Location(s, "my test", "中和", 35.65910807942215, 139.70372892916203) //傳位置
            )
            .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([
                    builder.CardAction.imBack(s, "buy classic gray t-shirt", "Buy"),
                    new CardAction().type("datatimepicker").title("time"),

                    builder.CardAction.postBack(s, "action=buy&itemid=111", "send data"),
                    builder.CardAction.openUrl(s, "https://1797.tw", "1797")

                ])
                //傳送 對話框
            )

        )
        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 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 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"),
                ])
        ]);
        builder.Prompts.text(s, msg);
    },
    async (s, r) => {
        s.send("hola:" + s.message.from.name + r.response)
    }
]);

bot.dialog('leave'
    , s => {
        s.send("byebye");
        connector.leave();

        s.endDialog()

    }
).triggerAction({
    matches: /^leave$/i
});
//輸入 leave LineBot就會離開了

bot.on('conversationUpdate', function (message) {
    // console.log("conversationUpdate", message)
    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")
});
//理論上 應該一被加入 就會 自動hello , 但只有一對一模式,會出來,應該是line 的 bug

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

上一篇
自幹 Line Builder 完工
下一篇
linebot demo : 讀冊狼
系列文
Microsoft Bot Framework 30天上手 + 自幹 Line Builder 串接 + Line Bot Demo30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言