SlideShare ist ein Scribd-Unternehmen logo
1 von 19
Downloaden Sie, um offline zu lesen
Line Messaging Bot
練習實作一個簡單的回話機器人
Ethan.Y
介紹
● Line在前陣子有提供(開發帳號),
可以測試Bot的功能,現在算是(正
式)提供給大家使用。
● 因為是要開發一個機器人,可以針
對使用者所發送的內容進行回應,
所以我們在這裡需要用的的服務
為(Messaging API)。
2
帳號種類
● 免費的帳號種類有兩種:
○ Developer Trial和免費版。
○ 兩種在功能及限制上有一些
不同,(Push api)和(好友人數)
是兩個帳號上有差異的部分。
3
建立帳號
● 在這裡就跳過建立帳號的過程,因
為我原本就有用Line@,當初開發
帳號的時候我也有申請,所以可能
不太準。
● 申請好帳號之後,回到(Business
Center)→(帳號清單),就可以看到
我們剛剛申請的帳號。
● 接著我們就來看看Messaging API
是什麼東西!
4
Take a Look!
5
帳號管理畫面
● 進到(Messaging api)的管理畫面,
可以在(Basic Information)當中看
到一些設定。
● 在這裡需要注意的東西有兩個,在
我們要實作一個簡單的回話機器
人會用到:
○ Webhook URL
○ Channel Access Token
6
Webhook URL
● 在使用這類API的時候,必須要有
一個Server,當使用者發送訊息的
時候,可以去(接收)並且作區適當
的回應。
● 在使用Facebook的Messenger Bot
也有同樣的機制(有關facebook的
使用介紹會在別的地方介紹)
● 官方對訂定一些規範,Webhook
URL要確認那些訊息,以及給予什
麼樣的回應,都會在官方的文件裡
面去定義。
7
Webhook URL cont.
會傳送一個HTTPS的POST Request
8
Webhook URL cont.
須回傳Status Code(200)
9
Channel Access Token
● 在中文的翻譯上,Token稱作權杖
,其代表的意思大概就如何鑰匙一
樣。
● 當Webhook URL的Server要呼叫
Line的API的時候,要一同傳送手
上所持有的Token,這樣一來Line
便會知道傳過來的資訊要給誰。
10
Channel Access Token cont.
11
Code Explanation
● 必要的套件有(flask、requests及json)。
● flask:python的web framework。
● requests:對Line的API進行呼叫。
● json:將要回傳的data轉換成json格式。
12
Code Explanation cont.
● 在前面的步驟有提到,Webhook URL會收到一個HTTPS POST Request。
● 要取出(replyToken),在之後進行reply的時候需要用到。
13
Code Explanation cont.
● 依照Line Messaging API Document所規範的格式,將準備回呼Line API的
Headers和Data準備好。
14
Code Explanation cont.
● 準備好相關的資料後,利用(requests)對Line API進行回呼。
● data要利用json.dumps()進行處理,因為回傳要是json格式。
15
Discussion
● 在前面的部分,主要是介紹Line Messaging API的使用流程。
● 但實在實作的時候,還有幾個部分是需要注意的,否則沒辦法成功。
○ 因為使用Webhook的時候,Line會針對我們設定的(Webhook URL)進行
HTTPS POST的呼叫,也就是說,我們執行程式碼的Sserver,是需要支援
HTTPS。
○ 在我的實作過程當中,我練習了兩種方式:
■ flask和ngrok的搭配(Server是在local)。
■ heroku和flask的搭配(Server是在雲端)。
● Keywords: ngrok, heroku, flask, git
16
Future
● 有關ngrok, heroku, flask, git,希望接下來都能整理出一些自己使用上的心得。
● 這次練習Line API的過程是蠻順利的,有鑑於之前已經先對(Facebook Messager
Bot)、(Heroku)、(flask)做了一些研究及練習,所以在官方文件的閱讀、環境的架
設上順利許多。
● 本次練習的程式碼放在gist上,如果有需要參考:
https://gist.github.com/ctosib/4a671fe6f73ae9168a9d24d7740d5304
17
Note
● flask
● json
● requests
● beautifulsoup
● git
● heroku
● ngrok
18
End by Ethan.Y
ctosib@gmail.com
19

Weitere ähnliche Inhalte

Empfohlen

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Empfohlen (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

First line messaging bot