Giter Site home page Giter Site logo

linebotapi's Introduction

#LINEBotAPI

Swift Platform License Bot

Overview

LINEBotAPI is a SDK of the LINE BOT API Trial for Swift.

  • Swift 3 support
  • Using Zewo
  • Linux Ready

Features

  • Send text/image/video/audio/location/sticker message
  • Send multiple messages
  • Send rich message
  • Handle received operation(add as friend or blocked)

A Work In progress

LINEBotAPI is currently in development.

Attention

Currently LINEBotAPI works with DEVELOPMENT-SNAPSHOT-2016-04-12-a.

Getting started

Installation

Before we start, we need to install some tools and dependencies.

swiftenv

swiftenv allows you to easily install multiple versions of swift.

% git clone https://github.com/kylef/swiftenv.git ~/.swiftenv

and add settings for your shell(For more information, please see swiftenv's wiki).

Swift

Install swift using swiftenv.

% swiftenv install DEVELOPMENT-SNAPSHOT-2016-04-12-a

Install Zewo

Install libraries depend on Zewo.

  • On OS X

    Install Zewo dependencies

    % brew install zewo/tap/zewo
    
  • On Linux

    Install Zewo dependencies

    % echo "deb [trusted=yes] http://apt.zewo.io/deb ./" | sudo tee --append /etc/apt/sources.list
    % sudo apt-get update
    % sudo apt-get install zewo
    

    Install Clang and ICU

    % sudo apt-get install clang libicu-dev
    

Install other libraries.

  • On OS X

    % brew install openssl curl
    % brew link --force openssl
    
  • On Linux

    % sudo apt-get install build-essential libcurl4-openssl-dev
    

Create project

We got prepared for a creating project.

Let's create your LINE Bot!

Make project directory.

% mkdir linebot && cd linebot

Set Swift version to DEVELOPMENT-SNAPSHOT-2016-04-12-a.

% swiftenv local DEVELOPMENT-SNAPSHOT-2016-04-12-a

Initialize project directory with swift package manager(SPM).

% swift build --init

Then this command will create the basic structure for app.

.
├── Package.swift
├── Sources
│   └── main.swift
└── Tests

Package.swift

Open Package.swift and make it looks like this:

import PackageDescription

let package = Package(
    name: "linebot",
    dependencies: [
        .Package(url: "https://github.com/yoshik/LINEBotAPI.git", majorVersion: 0, minor: 1),
    ]
)

main.swift

Next, write main program in main.swift.

import LINEBotAPI

do {
    if let mid = getVar(name: "TO_MID") {
        let bot = try LINEBotAPI()
        try bot.sendText(to: mid, text: "Hello! Hello!")
    } else {
        print("set env TO_MID")
    }
} catch let e {
    print(e)
}

This code:

  • get target mid(A user id on LINE)
  • send text to mid specified.

Build project

Change lib and include paths to your environment.

% swift build -Xlinker -L/usr/local/lib -Xcc -I/usr/local/include -Xswiftc -I/usr/local/include

On Linux only, currently, venice's directory structure is invalid. So you must run following command only once before build. For more information, see LINEBotAPI's Makefile.

# fetch dependencies only.
% swift build --fetch

# and move source files to upper directory.
% mv ./Packages/Venice-*/Source/Venice/*/* ./Packages/Venice-*/Source/
% rm -fr ./Packages/Venice-*/Source/Venice

Run it

After it compiles, run it.

Your must specify LINE_CHANNEL_ID, LINE_CHANNEL_SECRET, LINE_BOT_MID and TO_MID to yours. TO_MID is your mid.

% env LINE_CHANNEL_ID=XXXXXXXXX LINE_CHANNEL_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX LINE_BOT_MID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX TO_MID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX .build/debug/linebot

You will get a message from bot on LINE if you had setup setting on bot management page.

If you got an error .build/debug/linebot: error while loading shared libraries: libCLibvenice.so: cannot open shared object file: No such file or directory, add .build/debug/ to your LD_LIBRARY_PATH.

% export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.build/debug/

Start Server

LINEBotAPI allows you to start server using Zewo.

main.swift

Open main.swift and make it look like this:

import LINEBotAPI
import HTTPServer
import Router

do {
    let bot = try LINEBotAPI()

    // Initializer a router.
    let router = Router { (route) in
        // Waiting for POST request on /linebot/callback.
        route.post("/linebot/callback") { request -> Response in
            // Parsing request and validate signature.
            return try bot.parseRequest(request) { content in
                // Processing each messages or operations.
                // The contents can be enqueued to external storages as you wish.
                if let content = try ContentParser.parse(content) {
                    if let message = content as? TextMessage,
                        mid = message.fromMid {
                        // send text if content is a message.
                        try bot.sendText(to: mid, text: "Hi! Hi!")
                    } else if let op = content as? Operation,
                        mid = op.fromMid,
                        type = op.opType where type = .Added {
                        // send text if content is a operation to add.
                        try bot.sendText(to; mid, text: "Thanks to add as friend")
                    }
                }
            }
        }
    }

    // start server
    try Server(router).start()
} catch let e {
    print(e)
}

Then build and run it.

% env LINE_CHANNEL_ID=XXXXXXXXX LINE_CHANNEL_SECRET=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX LINE_BOT_MID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX .build/debug/linebot

The server will be started on port 8080.

This server will be waiting a POST request from Bot Server at /linebot/callback.

Other examples

Send multiple messages

try bot.sendMultipleMessage { builder in
    builder.addText(text: "Hello, bot!")
    builder.addImage(
        imageUrl: "http://www.example.com/some.jpg",
        previewUrl: "http://www.example.com/some-thumb.jpg",
    )
}

Send rich messages

try bot.sendRichMessage(to: mid, imageUrl: "http://example.com/images", altText: "Text for low-level devices") { (builder) in
    let action0 = RichMessageAction(name: "openHomepage", text: "Open Homepage", linkUri: "http://example.com/homepage")
    let listener0 = RichMessageListener(bounds: Bounds(x: 0, y: 0, width: 1040, height: 520), action: action0)
    builder.addListener(listener: listener0)

    let action1 = RichMessageAction(name: "showItem", text: "Show Item", linkUri: "http://example.com/showItem")
    let listener1 = RichMessageListener(bounds: Bounds(x: 0, y: 520, width: 520, height: 520), action: action1)
    builder.addListener(listener: listener1)

    let action2 = RichMessageAction(name: "search", text: "Search", linkUri: "http://example.com/search")
    let listener2 = RichMessageListener(bounds: Bounds(x: 520, y: 520, width: 520, height: 520), action: action2)
    builder.addListener(listener: listener2)
}

imageUrl must serve images of some size(For more information, see official docs).

width must be 1040px for Bounds() and a maximum size of height must be less than 2080px.

Tips

Can I develop on Xcode?

Yes, sure. You can generate a xcode project file with following command.

% swift build -Xlinker -L/usr/local/lib -Xcc -I/usr/local/include -Xswiftc -I/usr/local/include -X

Can I use https server?

Maybe. We are developing it using reverse proxy, but you must be able to support https because Zewo has HTTPSServer.

License

LINEBotAPI is released under the MIT license. See LICENSE for details.

linebotapi's People

Contributors

yoshiki avatar

Watchers

Satoshi SUZUKI avatar peiyou avatar  avatar

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.