This library is Unofficial
LINEBotAPI is a SDK of the LINE Messaging API for Swift.
- Swift 3 support
- Using Zewo
- Linux Ready
- Send text/image/video/audio/location/sticker message
- Handle follow/unfollow/join/leave/postback/Beacon events
- Send imagemap/template message
LINEBotAPI is currently in development.
Currently LINEBotAPI works with 3.0
.
Before we start, we need to install some tools and dependencies.
Install Swift using swiftenv or use docker images(docker-swift).
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.
Then install Swift 3.0. This process does not need on Mac OS X installed Xcode 8, only Linux.
% swiftenv install 3.0
-
On OS X
% brew install openssl curl % brew link --force openssl
-
On Linux
% sudo apt-get update % sudo apt-get install libcurl4-openssl-dev
We got prepared for a creating project.
Let's create your LINE Bot!
% mkdir linebot && cd linebot
Set Swift version to 3.0
if you need.
% swiftenv local 3.0
Initialize project directory with Swift Package Manager(SPM).
% swift package init --type executable
Then this command will create the basic structure for executable command.
.
├── Package.swift
├── Sources
│ └── main.swift
└── Tests
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: 1, minor: 0),
]
)
Next, write main program in main.swift
.
import LINEBotAPI
let bot = LINEBotAPI(accessToken: "YOUR_ACCESS_TOKEN")
try bot.sendText(to: "USER_ID", text: "Hello! Hello!")
This code:
- get target
userId
(A user id on LINE) - send text to
userId
specified.
Change lib and include paths to your environment.
% swift build
After it compiles, run it.
Your must specify USER_ID
to yours.
USER_ID` is your user id.
% .build/debug/linebot
You will get a message from bot on LINE if you had setup setting on bot management page.
LINEBotAPI supports server mode using Zewo
.
Open main.swift
and make it look like this:
import LINEBotAPI
import HTTPServer
let bot = LINEBotAPI(
accessToken: "YOUR_ACCESS_TOKEN",
channelSecret: "YOUR_CHANNEL_SECRET"
)
let log = LogMiddleware(debug: true)
// Initializer a router.
let router = BasicRouter { (routes) in
// Waiting for POST request on /callback.
routes.post("/callback") { (request) in
// Parsing request and validate signature
return try bot.parseRequest(request) { (event) in
if let textMessage = event as? TextMessage, let text = textMessage.text {
let builder = MessageBuilder()
builder.addText(text: text)
if let messages = try builder.build(), let replyToken = textMessage.replyToken {
try bot.replyMessage(replyToken: replyToken, messages: messages)
}
}
}
}
}
// start server
let server = try Server(port: 8080, middleware: [log], responder: router)
try server.start()
This is echo bot.
Then build and run it.
% swift build
% .build/debug/linebot
The server will be started on port 8080.
This server will be waiting a POST request from Bot Server at /callback
.
TODO
Yes, sure. You can generate a xcode project file with following command.
% swift package generate-xcodeproj
Maybe. We are developing it using reverse proxy, but you must be able to support https because Zewo has HTTPSServer
.
LINEBotAPI is released under the MIT license. See LICENSE for details.