Pro Yearly is on sale from $80 to $50! »

Develop LINE Bot in Swift

Dc25557b412c5e7d040305a713a34cc1?s=47 iamhands0me
November 08, 2020

Develop LINE Bot in Swift

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

https://iplayground.io/2020/

Dc25557b412c5e7d040305a713a34cc1?s=128

iamhands0me

November 08, 2020
Tweet

Transcript

  1. Develop LINE Bot in Swift

  2. 鄭宇哲 myVideo Instagram Blogger (8000 followers)

  3. None
  4. None
  5. None
  6. None
  7. None
  8. None
  9. • Swift

  10. Architecture LINE Platform send event POST request POST request reply

    message bot server
  11. Creating a channel • Log in to the LINE Developers

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

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

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

    as a developer • Create a new provider • Create a channel Creating a channel
  15. • 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
  16. Webhook Webhook send event POST request (webhook payloads) POST request

    reply message bot server LINE Platform
  17. • 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
  18. Signature Validation send event POST request POST request reply message

    bot server LINE Platform Hello! Hello! BE459576 785039E8 Channel secret
  19. • 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
  20. Access Token send event POST request POST request reply message

    bot server LINE Platform
  21. Develop bot server POST request POST request bot server LINE

    Platform
  22. 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
  23. Vapor 4.0 • Swift 5.2 or greater • Xcode 11.4

    or greater • Vapor Toolbox brew install vapor
  24. vapor new lineBot New Project

  25. Swift Package Manager

  26. main.swift

  27. configure.swift

  28. routes.swift

  29. Build & Run http://localhost:8080/ http://localhost:8080/hello

  30. Vapor.Request

  31. None
  32. None
  33. None
  34. None
  35. None
  36. { "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..." } } ] }
  37. { "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..." } } ] }
  38. { "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..." } } ] }
  39. { "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..." } } ] }
  40. { "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..." } } ] }
  41. None
  42. 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?" } ] }'
  43. None
  44. { "replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", "messages":[ { "type":"text", "text":"Hello, user" }, { "type":"text",

    "text":"May I help you?" } ] }
  45. { "replyToken":"nHuyWiB7yP5Zw52FIkcQobQuGDXCTA", "messages":[ { "type":"text", "text":"Hello, user" }, { "type":"text",

    "text":"May I help you?" } ] }
  46. None
  47. None
  48. None
  49. Advanced • Database • Async • Public file

  50. Be a Full Stack Swift Developer

  51. Path Component: Constant http://localhost:8080/hello/vapor

  52. Path Component: Parameter http://localhost:8080/hello/iPlayground

  53. Route Groups

  54. Route Collection routes.swift HelloController.swift

  55. Returning JSON http://localhost:8080/json/iPlayground

  56. Returning JSON http://localhost:8080/json/iPlayground

  57. URL Query http://localhost:8080/query?name=iPlayground

  58. Redirect http://localhost:8080/redirect

  59. Fluent • Object–relational mapping (ORM) framework for Swift • Supported

    type • PostgreSQL • SQLite • MySQL • MongoDB
  60. Configuration

  61. Model

  62. Migration configure.swift CreateTodo.swift

  63. Querying

  64. Async http://localhost:8080/async/iPlayground

  65. Async http://localhost:8080/map/iPlayground

  66. Public files

  67. Public files http://localhost:8080/iPlayground.png

  68. Hosting Heroku ngrok

  69. 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
  70. http://localhost:8080/ ngrok client ngrok server aaaaaaaaa