Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Develop LINE Bot in Swift

iamhands0me
November 08, 2020

Develop LINE Bot in Swift

Develop LINE Bot in Swift by Yu-Che Cheng @ iPlayground 2020

https://iplayground.io/2020/
https://www.youtube.com/watch?v=5aDV5Vb10Ko

iamhands0me

November 08, 2020
Tweet

More Decks by iamhands0me

Other Decks in Technology

Transcript

  1. Creating a channel • Log in to the LINE Developers

    Console • Register as a developer • Create a new provider • Create a channel
  2. Creating a channel • Log in to the LINE Developers

    Console • Register as a developer • Create a new provider • Create a channel
  3. Creating a channel • Log in to the LINE Developers

    Console • Register as a developer • Create a new provider • Create a channel
  4. • Log in to the LINE Developers Console • Register

    as a developer • Create a new provider • Create a channel Creating a channel
  5. • Set a Webhook URL to receive webhook payloads •

    Issue a Channel secret to verify the signature in the request • Issue a Channel access token to make API calls Setting up the bot
  6. • Set a Webhook URL to receive webhook payloads •

    Issue a Channel secret to verify the signature in the request • Issue a Channel access token to make API calls Setting up the bot
  7. Signature Validation send event POST request POST request reply message

    bot server LINE Platform Hello! Hello! BE459576 785039E8 Channel secret
  8. • Set a Webhook URL to receive webhook payloads •

    Issue a Channel secret to verify the signature in the request • Issue a Channel access token to make API calls Setting up the bot
  9. Server-side Swift Frameworks Vapor Kitura Perfect Zewo Smoke 2016 ~

    2020 2016 ~ 2019 2016 ~ 2020 2016 ~ 2018 2018 ~ 2020 19.6k 7.4k 13.8k 1.8k 1.2k
  10. Vapor 4.0 • Swift 5.2 or greater • Xcode 11.4

    or greater • Vapor Toolbox brew install vapor
  11. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": “0f3779fba3b349968c5d07db31eab56f", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "message": { "id": "325708", "type": "text", "text": "Hello, world" } }, { "replyToken": "8cf9239d56244f4197887e939187e19e", "type": "follow", "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." } } ] }
  12. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": “0f3779fba3b349968c5d07db31eab56f", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "message": { "id": "325708", "type": "text", "text": "Hello, world" } }, { "replyToken": "8cf9239d56244f4197887e939187e19e", "type": "follow", "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." } } ] }
  13. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": “0f3779fba3b349968c5d07db31eab56f", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "message": { "id": "325708", "type": "text", "text": "Hello, world" } }, { "replyToken": "8cf9239d56244f4197887e939187e19e", "type": "follow", "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." } } ] }
  14. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": “0f3779fba3b349968c5d07db31eab56f", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "message": { "id": "325708", "type": "text", "text": "Hello, world" } }, { "replyToken": "8cf9239d56244f4197887e939187e19e", "type": "follow", "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." } } ] }
  15. { "destination": "xxxxxxxxxx", "events": [ { "replyToken": “0f3779fba3b349968c5d07db31eab56f", "type": "message",

    "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." }, "message": { "id": "325708", "type": "text", "text": "Hello, world" } }, { "replyToken": "8cf9239d56244f4197887e939187e19e", "type": "follow", "mode": "active", "timestamp": 1462629479859, "source": { "type": "user", "userId": "U4af4980629..." } } ] }
  16. curl -v -X POST https://api.line.me/v2/bot/message/reply \ -H 'Content-Type: application/json' \

    -H 'Authorization: Bearer {channel access token}' \ -d '{ "replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", "messages":[ { "type":"text", "text":"Hello, user" }, { "type":"text", "text":"May I help you?" } ] }'
  17. Fluent • Object–relational mapping (ORM) framework for Swift • Supported

    type • PostgreSQL • SQLite • MySQL • MongoDB
  18. Heroku • Download and Install Heroku CLI • Log in

    to Heroku • Create a new Heroku app from the Heroku dashboard • Connect with Heroku • Set Buildpack • Swift version file • Procfile • Deploy brew install heroku/brew/heroku heroku login heroku git:remote -a {HEROKU_APP_NAME} heroku buildpacks:set vapor/vapor echo 5.2.1 > .swift-version echo web: Run serve --env production --hostname 0.0.0.0 --port $PORT > Procfile git add . git commit -m 'First commit' git push heroku master