<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/" >

<channel>
	<title>tạo bot chat facebook &#8211; AnonyViet</title>
	<atom:link href="https://anonyviet.com/tag/tao-bot-chat-facebook/feed/" rel="self" type="application/rss+xml" />
	<link>https://anonyviet.com</link>
	<description>Webiste chia sẻ kiến thức công nghệ thông tin, mạng máy tính, bảo mật phổ biến nhất Việt Nam. Luôn cập nhật tin tức, thủ thuật nóng hổi nhất</description>
	<lastBuildDate>Wed, 16 Dec 2020 00:49:12 +0000</lastBuildDate>
	<language>vi</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://anonyviet.com/wp-content/uploads/2018/10/cropped-ico-logo-75x75.png</url>
	<title>tạo bot chat facebook &#8211; AnonyViet</title>
	<link>https://anonyviet.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Hướng dẫn tạo BOT Chat Facebook Messenger trên FanPage</title>
		<link>https://anonyviet.com/huong-dan-tao-bot-chat-facebook-messenger-tren-fanpage/</link>
					<comments>https://anonyviet.com/huong-dan-tao-bot-chat-facebook-messenger-tren-fanpage/#comments</comments>
		
		<dc:creator><![CDATA[AnonyViet]]></dc:creator>
		<pubDate>Thu, 05 Jan 2017 15:01:06 +0000</pubDate>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[BOT Chat Facebook Messenger]]></category>
		<category><![CDATA[BOT Chat Facebook Messenger trên FanPage]]></category>
		<category><![CDATA[tạo bot chat]]></category>
		<category><![CDATA[tạo bot chat facebook]]></category>
		<category><![CDATA[tạo bot chat fanpage]]></category>
		<category><![CDATA[tự động chat facebook]]></category>
		<guid isPermaLink="false">https://anonyviet.com/?p=3409</guid>

					<description><![CDATA[Dạo này các Page nổi tiếng đều có sử dụng loại BOT Chat tự động trả lời tin nhắn, giống ứng dụng Simsimi tự kỷ, hôm nay AnonyViet sẽ hướng dẫn tạo BOT Chat Facebook Messenger trên FanPage riêng cho mình, chỉ cần làm tuần tự các bước hướng dẫn dưới đây là được nhé [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>Dạo này các Page nổi tiếng đều có sử dụng loại BOT Chat tự động trả lời tin nhắn, giống ứng dụng Simsimi tự kỷ, hôm nay <a href="https://anonyviet.com">AnonyViet </a>sẽ hướng dẫn tạo BOT Chat Facebook Messenger trên FanPage riêng cho mình, chỉ cần làm tuần tự các bước hướng dẫn dưới đây là được nhé</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full " title="con Bot tự động chat trên Facebook Messenger" src="https://anonyviet.com/wp-content/uploads/2017/01/demo-1.gif" alt="con Bot tự động chat trên Facebook Messenger" width="368" height="598" /></p>
<p>Dưới đây là các bước để tạo một bot chat trên Facebook Messenger.</p>
<h2>1. Thiết lập</h2>
<p>Messenger bot sử dụng web server để xử lý những tin nhắn mà nó nhận được hoặc tìm ra các tin nhắn để gửi.</p>
<h3>1.1. Xây dựng server</h3>
<p>1. Tải và cài đặt Heroku Toolbet tại đây: <a href="https://toolbelt.heroku.com/" target="_blank" rel="noopener">https://toolbelt.heroku.com</a>/ để khởi động, dừng và theo dõi các sự cố.</p>
<p>Nếu chưa có tài khoản, bạn có thể đăng nhập miễn phí tại <a title="tạo tài khoản" href="https://www.heroku.com" target="_blank" rel="nofollow noopener">https://www.heroku.com</a>.</p>
<p>2. Truy cập <a title="nodejs" href="https://nodejs.org" target="_blank" rel="nofollow noopener">https://nodejs.org</a> để cài đặt Node tạo môi trường server.</p>
<p>Sau đó tiến hành mở Terminal hoặc Command Line Prompt để chắc chắn bạn đã cài đặt phiên bản npm mới nhất bằng cách cài đặt nmp một lần nữa:</p>
<pre class="lang:default decode:true">sudo npm install npm –g
</pre>
<p>&nbsp;</p>
<p>3. Tạo một thư mục mới và tạo project Node mới. Nhấn Enter để chấp nhận giá trị mặc định:</p>
<pre class="lang:default decode:true">npm init
</pre>
<p>&nbsp;</p>
<p>4. Cài đặt thêm gói dịch vụ Node Dependencies. Express dành cho server yêu cầu việc gửi tin nhắn và body-parser là để xử lý tin nhắn:</p>
<pre class="lang:default decode:true ">npm install express request body-parser --save</pre>
<p>&nbsp;</p>
<p>5. Tạo file index.js trong thư mục và copy các đoạn mã dưới đây vào để xác nhận bot:</p>
<pre><code>var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var app = express()

app.set('port', (process.env.PORT || 5000))

// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))

// Process application/json
app.use(bodyParser.json())

// Index route
app.get('/', function (req, res) {
    res.send('Hello world, I am a chat bot')
})

// for Facebook verification
app.get('/webhook/', function (req, res) {
    if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
        res.send(req.query['hub.challenge'])
    }
    res.send('Error, wrong token')
})

// Spin up the server
app.listen(app.get('port'), function() {
    console.log('running on port', app.get('port'))
})</code></pre>
<p>6. Tạo một file và đặt tên file là Procfile, sau đó copy dòng mã dưới đây vào để Heroku có thể biết được phải chạy file nào:<br />
web: node index.js</p>
<p>Xác nhận tất cả mã với Git sau đó tạo một mã Heroku mới và &#8220;đẩy&#8221; các mã lên cloud:</p>
<pre><code>web: node index.js</code></pre>
<p>7. Xác nhận tất cả mã với Git sau đó tạo một mã Heroku mới và &#8220;đẩy&#8221; các mã lên cloud:</p>
<pre><code>git init
git add .
git commit --message 'hello world'
heroku create
git push heroku master</code></pre>
<h3>1.2. Thiết lập ứng dụng Facebook App</h3>
<p>1. Truy cập <a title="Facebook" href="http://adf.ly/1hXX6H" target="_blank" rel="nofollow noopener">https://developers.facebook.com/apps/ </a>để tạo hoặc cấu hình Facebook App hoặc Page.</p>
<p><img decoding="async" class="alignnone size-full " title="cấu hình Facebook App hoặc Page" src="https://anonyviet.com/wp-content/uploads/2017/01/facebook-app.jpg" alt="cấu hình Facebook App hoặc Page" width="1280" height="496" /></p>
<p>2. Trên ứng dụng, chuyển qua thẻ Messenger sau đó click chọn<strong> Setup Webhook</strong>. Tại đây nhập mã URL của server Heroku và mã thông báo.</p>
<p><img decoding="async" class="alignnone size-full " title="nhập mã URL của server Heroku và mã thông báo" src="https://anonyviet.com/wp-content/uploads/2017/01/nhap-url.jpg" alt="nhập mã URL của server Heroku và mã thông báo" width="1280" height="611" /></p>
<p>3. Nhận Page Access Token và lưu lại mã này.</p>
<p><img decoding="async" class="alignnone size-full " title="Page Access Token " src="https://anonyviet.com/wp-content/uploads/2017/01/Page-Access-token.jpg" alt="Page Access Token " width="1256" height="1280" /></p>
<p>4. Quay trở lại Terminal và nhập câu lệnh dưới đây để kích hoạt Facebook app gửi tin nhắn. Lưu ý, sử dụng mã thông báo mà bạn đã sử dụng trước đó.</p>
<ol>
<li>
<pre><code>curl -X POST "https://graph.facebook.com/v2.6/me/subscribed_apps?access_token=&lt;PAGE_ACCESS_TOKEN&gt;"</code></pre>
</li>
</ol>
<h3>1.3. Thiết lập Bot</h3>
<p>1. Thêm vào điểm cuối API để index.js thực hiện quá trình xử lý tin nhắn. Lưu ý, bao gồm cả mã thông báo bạn nhận trước đó:</p>
<pre><code>app.post('/webhook/', function (req, res) {
    messaging_events = req.body.entry[0].messaging
    for (i = 0; i &lt; messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i]
        sender = event.sender.id
        if (event.message &amp;&amp; event.message.text) {
            text = event.message.text
            sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
        }
    }
    res.sendStatus(200)
})

var token = "&lt;PAGE_ACCESS_TOKEN&gt;"</code></pre>
<p>2. Thêm một chức năng để phản hồi lại tin nhắn:</p>
<pre><code>function sendTextMessage(sender, text) {
    messageData = {
        text:text
    }
    request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
        method: 'POST',
        json: {
            recipient: {id:sender},
            message: messageData,
        }
    }, function(error, response, body) {
        if (error) {
            console.log('Error sending messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}</code></pre>
<p>3. Xác nhận đoạn mã một lần nữa và đẩy lên Heroku:</p>
<pre><code>git add .
git commit -m 'updated the bot to speak'
git push heroku master</code></pre>
<p>4. Truy cập Facebook và click Message để bắt đầu chat.</p>
<p><img decoding="async" class="alignnone size-full " title="truy cập Facebook và click Message để bắt đầu chat" src="https://anonyviet.com/wp-content/uploads/2017/01/bat-dau-chat.jpg" alt="truy cập Facebook và click Message để bắt đầu chat" width="1280" height="834" /></p>
<h2>2. Tùy chỉnh thông điệp bot &#8220;nói&#8221;</h2>
<h3>2.1. Gửi một cấu trúc thông điệp của Message</h3>
<p>Facebook Messenger có thể gửi một cấu trúc thông điệp Message dưới dạng card hoặc nút.</p>
<p><img decoding="async" class="alignnone size-full " title="cấu trúc thông điệp Message" src="https://anonyviet.com/wp-content/uploads/2017/01/cau-truc-mesage.jpg" alt="cấu trúc thông điệp Message" width="1280" height="521" /></p>
<p>1. Copy đoạn mã dưới đây vài index.js để gửi một tin nhắn kiểm tra lại dưới dạng 2 card:</p>
<pre><code>function sendGenericMessage(sender) {
    messageData = {
        "attachment": {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": [{
                    "title": "First card",
                    "subtitle": "Element #1 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/rift.png",
                    "buttons": [{
                        "type": "web_url",
                        "url": "https://www.messenger.com",
                        "title": "web url"
                    }, {
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for first element in a generic bubble",
                    }],
                }, {
                    "title": "Second card",
                    "subtitle": "Element #2 of an hscroll",
                    "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
                    "buttons": [{
                        "type": "postback",
                        "title": "Postback",
                        "payload": "Payload for second element in a generic bubble",
                    }],
                }]
            }
        }
    }
    request({
        url: 'https://graph.facebook.com/v2.6/me/messages',
        qs: {access_token:token},
        method: 'POST',
        json: {
            recipient: {id:sender},
            message: messageData,
        }
    }, function(error, response, body) {
        if (error) {
            console.log('Error sending messages: ', error)
        } else if (response.body.error) {
            console.log('Error: ', response.body.error)
        }
    })
}</code></pre>
<p>2. Cập nhật Webhook API để tìm kiếm các tin nhắn đặc biệt để kích hoạt trên card:</p>
<pre><code>app.post('/webhook/', function (req, res) {
    messaging_events = req.body.entry[0].messaging
    for (i = 0; i &lt; messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i]
        sender = event.sender.id
        if (event.message &amp;&amp; event.message.text) {
            text = event.message.text
            if (text === 'Generic') {
                sendGenericMessage(sender)
                continue
            }
            sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
        }
    }
    res.sendStatus(200)
})</code></pre>
<h3>2.2. Hồi đáp lại tin nhắn của người dùng</h3>
<p>Chuyện gì sẽ xảy ra khi người dùng click vào nút hoặc card trên Message? Trong trường hợp này update Webhook API một lần nữa để gửi chức năng postback:</p>
<pre><code>    ```
app.post('/webhook/', function (req, res) {
    messaging_events = req.body.entry[0].messaging
    for (i = 0; i &lt; messaging_events.length; i++) {
        event = req.body.entry[0].messaging[i]
        sender = event.sender.id
        if (event.message &amp;&amp; event.message.text) {
            text = event.message.text
            if (text === 'Generic') {
                sendGenericMessage(sender)
                continue
            }
            sendTextMessage(sender, "Text received, echo: " + text.substring(0, 200))
        }
        if (event.postback) {
            text = JSON.stringify(event.postback)
            sendTextMessage(sender, "Postback received: "+text.substring(0, 200), token)
            continue
        }
    }
    res.sendStatus(200)
})
```</code></pre>
<p>Thêm Git, xác nhận và đẩy lên Heroku một lần nữa.</p>
<p>Bây giờ bạn có thể chat với bot và nhập &#8220;Generic&#8221; để nhìn thấy bot.</p>
<p style="text-align: right;">Author: Quantrimang.com</p>
]]></content:encoded>
					
					<wfw:commentRss>https://anonyviet.com/huong-dan-tao-bot-chat-facebook-messenger-tren-fanpage/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
