Skip to content

Commit 1a61180

Browse files
committed
add translated for 'Facebook chat bot aka joke bot with django tutorial'
1 parent 9068023 commit 1a61180

4 files changed

Lines changed: 46 additions & 126 deletions

‎Django/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
- [Django, ELB健康检查和持续交付](./Django, ELB健康检查和持续交付.md)
1414

15-
15+
- [带django教程的Facebook聊天机器人,又名笑话机器人](./带django教程的Facebook聊天机器人,又名笑话机器人.md)
16+
17+
"我决定尝试开发一个聊天机器人,它只做一件事。不管用户输入什么,它都发送一个像下面这样的不含图片的随机笑话。"

raw/Facebook chat bot aka joke bot with django tutorial.md renamed to Django/带django教程的Facebook聊天机器人,又名笑话机器人.md

Lines changed: 24 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,33 @@
55

66
![](https://qph.is.quoracdn.net/main-qimg-d3f48b427c16a968c79301b80a2b7aa9?convert_to_webp=true)
77

8-
The age of bots has begun. Chat bots are a sensation now a days and are being
9-
hailed as the next generation replacement to websites and apps. Many platforms
10-
are getting opened up with chat bots as interaction tools such as in Facebook,
11-
Telegram etc. Chat bots are already quite famous in China’s messaging giant We
12-
Chat where people book cabs and make financial transactions via chat.
8+
机器人时代已经开始了。如今,聊天机器人引起轰动,并被誉为下一代的网站和应用的替代品。许多平台都开发了聊天机器人作为交互式工具,例如Facebook, Telegram等等。聊天机器人在中国的通讯巨头WeChat中颇负盛名,其中,人们通过聊天预定出租车和进行金融交易。
139

14-
I have decided to try to develop a chat bot which does only one thing. Send a
15-
random joke like the below one without an image irrespective of what the user
16-
types
10+
我决定尝试开发一个聊天机器人,它只做一件事。不管用户输入什么,它都发送一个像下面这样的不含图片的随机笑话。
1711

1812
![](https://qph.is.quoracdn.net/main-qimg-0a5a4526932b6ee315cc64b6106d72a4?convert_to_webp=true)
1913

20-
Getting started we are going to develop it in django. Why django? , I am good
21-
in python so I felt to go forward in Django, you can choose whatever you like.
22-
The official docs are for node.js .
14+
作为开始,我们将要在django中开发它。为什么是django呢?我擅长于python,因此使用Django,你可以使用任何你喜欢的。官方文档是用于node.js的。
2315

24-
1. Create a facebook page from here [Create a Page |
25-
Facebook](https://www.facebook.com/pages/create/) . Choose some page type and
26-
go forward with the settings.
16+
1. 从这里[创建一个页面 | Facebook](https://www.facebook.com/pages/create/)创建一个facebook页面。选择一些页面类型并进行设置。
2717

28-
2.Create a facebook app from here [Log into Facebook |
29-
Facebook](https://developers.facebook.com/quickstarts/) and fill in the
30-
required details to create an app.
18+
2. 从这里[登录到Facebook | Facebook](https://developers.facebook.com/quickstarts/)创建一个facebook应用,并填写所需的详细信息来创建一个应用。
3119

32-
3.Once both the page and app are created go to the app settings and under
33-
Product Settings click Add Product and then Select **Messenger **.
20+
3. 一旦页面和应用都创建好了,那么转到应用设置,并在Product Settings下点击Add Product,然后选择**Messenger **。
3421

35-
4.Now you need to setup webhooks. We need to deploy the django project online
36-
and make it available for facebook to make requests and facebook allows only
37-
https urls. [Your development environment, in the cloud](http://c9.io) is the
38-
best option I found online and it’s free for a basic app. Create a workspace
39-
in c9 and choose **Django **as platform. Now you will be directed to your
40-
online workspace.
22+
4. 现在,你需要设置网络钩子。我们需要将该django项目部署在线,并且使其可为facebook创建请求,而facebook只允许https url。[Your development environment, in the cloud](http://c9.io)是我发现在线的最佳选择,而它对于一个基本的应用是免费的。在c9中创建工作空间,选择**Django**作为平台。现在,你将被定向到你的在线工作空间。
4123

42-
5. From the workspace use the below command to create a django app.
24+
5. 在工作区中,使用下面的命令来创建一个django应用。
4325

4426
```python
4527

4628
django-admin.py startapp jokebot
4729

4830
```
4931

50-
This should create an app jokebot.
32+
这应该创建一个应用jokebot。
5133

52-
Go to [settings.py](http://settings.py) file in the workspace, find
53-
INSTALLED_APPS and add jokebot.
34+
转到工作空间中的[settings.py](http://settings.py)文件,找到INSTALLED_APPS,并添加jokebot。
5435

5536
```python
5637

@@ -66,9 +47,7 @@ INSTALLED_APPS and add jokebot.
6647

6748
```
6849

69-
Modify [urls.py](http://urls.py) to include an endpoint for webhook. The
70-
webhook will hit the endpoint url/james and we have to respind with a token
71-
for facebook to validate.
50+
修改[urls.py](http://urls.py)以包含网络钩子的端点。该网络钩子将拦截端点url/james,而我们必须返回一个token以供facebook验证。
7251

7352
```python
7453

@@ -83,8 +62,7 @@ for facebook to validate.
8362

8463
```
8564

86-
Now go to [views.py](http://views.py) in the app jokebot and modify it to the
87-
following.
65+
现在,打开应用jokebot中的[views.py](http://views.py),并如下修改它。
8866

8967
```python
9068

@@ -127,28 +105,21 @@ following.
127105

128106
```
129107

130-
We create a class jokebot which responds with
108+
我们创建了一个类jokebot,它响应一个网络钩子请求中的
131109

132110
```python
133111

134112
self.request.GET['hub.challenge']
135113

136114
```
137115

138-
on a webhook request.
139-
140-
Under the "PRODUCT SETTINGS" section, click on the "Messenger" product you
141-
just added, find the Webhooks section and click Setup Webhooks. Enter a URL
142-
for a webhook, enter a Verify Token and select message_deliveries, messages,
143-
messaging_optins, and messaging_postbacks under Subscription Fields.
116+
在"PRODUCT SETTINGS"部分,点击你刚刚添加的"Messenger"产品,找到Webhooks部分,点击Setup Webhooks。为网络钩子输入一个URL,输入一个验证令牌,并在Subscription字段下选择message_deliveries, messages, messaging_optins, 和messaging_postbacks。
144117

145118
![](https://qph.is.quoracdn.net/main-qimg-70f9ecaffdc3ae1100c3402099af9364?convert_to_webp=true)
146119

147-
Choose any verify_token and place it in facebook and in the code. To get the
148-
url click on **Run** in c9 and place the url/james in callback url option. Now
149-
click on Verify and Save and you should get the webhook verified.
120+
选择任意verify_token,并将其放到facebook和代码中。要获得url,点击c9中的**Run**,并将url/james放到回调的url选项中。现在,点击Verify and Save,然后应该就可以让该网络钩子被验证。
150121

151-
Now we handle the message and send a joke using this code
122+
现在,我们使用这个代码处理消息并发送一个笑话
152123

153124
```python
154125

@@ -162,7 +133,7 @@ Now we handle the message and send a joke using this code
162133

163134
```
164135

165-
This basically reads the post request of a message and responds with a joke.
136+
这基本上读取一个消息的Post请求,并用一个笑话进行响应。
166137

167138
```python
168139

@@ -175,24 +146,19 @@ This basically reads the post request of a message and responds with a joke.
175146

176147
```
177148

178-
This function gets a random joke using an online api [The Internet ChuckNorris Database](http://www.icndb.com/) and sends it back to the user.
149+
这个函数使用一个在线API [The Internet ChuckNorris Database](http://www.icndb.com/)获取一个随机笑话,然后将其发送回给用户。
179150

180-
To get page access token in the Token Generation section, select your Page. A
181-
Page Access Token will be generated for you. Copy this Page Access Token.
182-
Note: The generated token will NOT be saved in this UI. Each time you select
183-
that Page a new token will be generated. However, any previous tokens created
184-
will continue to function.
151+
要获得Token Generation部分的页面访问令牌,选择你的Page。一个Page Access Token将会为你生成。复制此Page Access Token。注意:生成的令牌将不在此UI进行保存。每次选择那个Page,都将会生成新的令牌。然而,任何之前创建令牌将继续发挥作用。
185152

186153
![](https://qph.is.quoracdn.net/main-qimg-0d58fc1759de26a3fadfafeda03e7e8e?convert_to_webp=true)
187154

188-
So now run the server again and send a message and you should get a Chuck
189-
Norris joke. Remember that the app now works only for you. To make it publicly
190-
available you have to request fb to approve your app and publish it.
155+
所以现在再次运行服务器并发送一条消息,你应该会得到一个Chuck
156+
Norris笑话。请记住,应用程序现在仅适用于你。为了使其公开,你必须请求fb批准您的应用程序并进行发布。
191157

192-
You can find the entire source code here [Anil1331/Facebook-chat-bot](https://github.com/Anil1331/Facebook-chat-bot) .
158+
你可以在这里发现完整的代码[Anil1331/Facebook-chat-bot](https://github.com/Anil1331/Facebook-chat-bot)。
193159

194-
Sources:-
160+
来源:-
195161

196-
1. [How To Build Bots for Messenger - Facebook for Developers](https://developers.facebook.com/blog/post/2016/04/12/bots-for-messenger/)
162+
1. [如何为Messenger建立通讯机器人 - 对于Facebook的开发者](https://developers.facebook.com/blog/post/2016/04/12/bots-for-messenger/)
197163

198164

‎Python Weekly/Python Weekly Issue 246.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ SortedContainers在大规模上运行得相当好。本文在理论和实践层
3333

3434
[带django教程的Facebook聊天机器人,又名笑话机器人](https://codeexperiments.quora.com/Facebook-chat-bot-aka-joke-bot-with-django-tutorial)
3535

36+
[中文版](../Django/带django教程的Facebook聊天机器人,又名笑话机器人.md)
37+
3638
[使用人工智能来评估手写数学公式](http://www.willforfang.com/computer-vision/2016/4/9/artificial-intelligence-for-handwritten-mathematical-expression-evaluation)
3739

3840
[使用Python的动力系统:Wilson和Cowan模型](http://martinosorb.github.io/blog/2016/05/26/wilsoncowan.html)

‎raw/Web Scraping with Python – Scraping Digital Comics information from Comixology.md‎

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,24 @@
1-
[ ![](http://0.gravatar.com/avatar/f450c214808965f494b965d438ba780e?s=100&d=mm
2-
&r=g) ](http://felipegalvao.com.br/blog)
3-
4-
# [Felipe Galvão](http://felipegalvao.com.br/blog "View all posts by Felipe
5-
Galvão" )
6-
7-
##
8-
9-
## Published
10-
11-
## [maio 24, 2016](http://felipegalvao.com.br/blog/2016/05/24/web-scraping-
12-
with-python-scraping-digital-comics-information-from-comixology/ "Web Scraping
13-
with Python - Scraping Digital Comics information from Comixology" )
14-
15-
Skip to content
16-
17-
* [Data Science / Analysis with Python - Basics](http://felipegalvao.com.br/blog/data-science-analysis-with-python-basics/)
18-
* [Ciência / Análise de Dados com Python - Básico](http://felipegalvao.com.br/blog/ciencia-de-dados-com-python-basico/)
19-
* [Contatos / Contact Me](http://felipegalvao.com.br/blog/contatos-contact-me/)
20-
21-
## [Felipe Galvão](http://felipegalvao.com.br/blog "View all posts by Felipe
22-
Galvão" ) in [Python (English)](http://felipegalvao.com.br/blog/category
23-
/python-english/ "View all posts in Python \(English\)" ) | [maio 24,
24-
2016](http://felipegalvao.com.br/blog/2016/05/24/web-scraping-with-python-
25-
scraping-digital-comics-information-from-comixology/ "Web Scraping with Python
26-
- Scraping Digital Comics information from Comixology" )
271

282
原文:[Web Scraping with Python - Scraping Digital Comics information from Comixology](http://felipegalvao.com.br/blog/2016/05/24/web-scraping-with-python-scraping-digital-comics-information-from-comixology/)
293

304
---
315

32-
## Introduction
6+
## 简介
337

348
Well, after having talked about the previous analysis that I did about
35-
Comixology in the [previous post in this
36-
series](http://felipegalvao.com.br/blog/2016/05/01/comixology-analise-de-
37-
quadrinhos-digitais-parte-1-analise-do-site-para-web-scraping/), we'll now
9+
Comixology in the [previous post in this series](http://felipegalvao.com.br/blog/2016/05/01/comixology-analise-de-quadrinhos-digitais-parte-1-analise-do-site-para-web-scraping/), we'll now
3810
talk about the web scraping with Python, the code itself. As we have already
3911
talked about, we will use requests to make requests to web pages and lxml with
4012
Xpath for parsing and information extraction from the desired pages. Let's go.
4113

42-
## Starting to write the code
14+
## 开始写代码
4315

4416
To start the conversation, we will import the necessary libraries, requests
4517
and, from lxml, the html function. To access the HTML parts, we will use
4618
Xpath. So, let's talk about Xpath before we move on to the code. I'll try to
4719
keep it short.
4820

49-
## Xpath: What it is and how to use it
21+
## Xpath:它是什么以及如何使用它
5022

5123
Xpath is a query language used to extract content from XML / HTML documents.
5224
It can be used to extract information form nodes and attributes, being very
@@ -224,22 +196,20 @@ something that does not exist in the document, an empty list is returned.
224196
And now that we saw this simple example, let's move on to something more
225197
complex and fun.
226198

227-
## Returning to the code
199+
## 回到代码
228200

229201
Now that we know what Xpath is about, let's use it to extract the Comixology
230202
website itself. For web scraping with Xpath, one of our best friends will be
231203
the "Inspect Element" feature from Chrome or Firefox. It will allow us to see
232204
the source code and the HTML structure and path for a certain element in the
233205
page.
234206

235-
Let's move on to the Publishers section on Comixology. ([click
236-
here](https://www.comixology.com/browse-publisher)). We will extract our code
207+
Let's move on to the Publishers section on Comixology. ([点击这里](https://www.comixology.com/browse-publisher)). We will extract our code
237208
defining the function and creating an empty list which will hold the Publisher
238209
links that we will extract. As we talked on the first post, we will define the
239210
page quantity manually (4 pages, so, we will have a for page in range (1,5)),
240211
and we will pass through each of them. If we go to the site and change to the
241-
next page, we will see that the link becomes: https://www.comixology.com
242-
/browse-publisher?publisherList_pg=2. If we change it back to the first page,
212+
next page, we will see that the link becomes: https://www.comixology.com/browse-publisher?publisherList_pg=2. If we change it back to the first page,
243213
the final part of the link turn back to 1. The first pages of the code will be
244214
as following:
245215

@@ -282,10 +252,7 @@ class "list FeaturedPublisherList", items from the All Publishers table are in
282252
a div that has the class "list publisherList". Let's make our Xpath sentence
283253
going from there:
284254

285-
[![comixology_2_1-publishers](http://felipegalvao.com.br/blog/wp-
286-
content/uploads/2016/05/comixology_2_1-publishers-
287-
1024x925.jpg)](http://felipegalvao.com.br/blog/wp-
288-
content/uploads/2016/05/comixology_2_1-publishers.jpg)
255+
[![comixology_2_1-publishers](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_1-publishers-1024x925.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_1-publishers.jpg)
289256

290257
Now, I'll create a Xpath string for the extraction of the elements, going from
291258
the div with the class that we just saw (//div[@class="list publisherList"]),
@@ -385,8 +352,7 @@ you will have them on the list returned by the function. First part, concluded
385352
🙂
386353

387354
I'll take this time to talk about a library that will help us in this task,
388-
which is the Pickle package ([to know more about it, click
389-
here](https://wiki.python.org/moin/UsingPickle)). This library will allow us
355+
which is the Pickle package ([想了解更多,点击这里](https://wiki.python.org/moin/UsingPickle)). This library will allow us
390356
to export data to files, to load them back later on. In this first moment,
391357
this will not seem very useful, since this extraction function runs very fast
392358
and does not take much time to be completed, because there are only 4 pages to
@@ -444,28 +410,19 @@ possible cases: A - when the publisher has only one page, B - when there is a
444410
small number of pages (like, from 2 to more or less 10), C - when a publisher
445411
has a great number of pages. See in the image:
446412

447-
[![A - Publisher with 1 page](http://felipegalvao.com.br/blog/wp-
448-
content/uploads/2016/05/comixology_2_2-publisher_no_pages-
449-
300x238.png)](http://felipegalvao.com.br/blog/wp-
450-
content/uploads/2016/05/comixology_2_2-publisher_no_pages.png)
413+
[![A - Publisher with 1 page](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_2-publisher_no_pages-300x238.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_2-publisher_no_pages.png)
451414

452415
A - Publisher with 1 page
453416

454417
|
455418

456-
[![B - Publisher with few pages](http://felipegalvao.com.br/blog/wp-
457-
content/uploads/2016/05/comixology_2_3-publisher_few_pages-
458-
208x300.png)](http://felipegalvao.com.br/blog/wp-
459-
content/uploads/2016/05/comixology_2_3-publisher_few_pages.png)
419+
[![B - Publisher with few pages](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_3-publisher_few_pages-208x300.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_3-publisher_few_pages.png)
460420

461421
B - Publisher with few pages
462422

463423
|
464424

465-
[![C - Publisher with many pages](http://felipegalvao.com.br/blog/wp-
466-
content/uploads/2016/05/comixology_2_4-publisher_many_pages-
467-
228x300.png)](http://felipegalvao.com.br/blog/wp-
468-
content/uploads/2016/05/comixology_2_4-publisher_many_pages.png)
425+
[![C - Publisher with many pages](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_4-publisher_many_pages-228x300.png)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_4-publisher_many_pages.png)
469426

470427
C - Publisher with many pages
471428

@@ -484,10 +441,7 @@ available, the Publisher has only one page of Series. Simple, isn't it? Well,
484441
let's inspect some elements to see how we are going to build the Xpath
485442
sentence, and then we can go to the code:
486443

487-
[![Path to quantity of Series](http://felipegalvao.com.br/blog/wp-
488-
content/uploads/2016/05/comixology_2_5-series_pages-
489-
1024x469.jpg)](http://felipegalvao.com.br/blog/wp-
490-
content/uploads/2016/05/comixology_2_5-series_pages.jpg)
444+
[![Path to quantity of Series](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_5-series_pages-1024x469.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_5-series_pages.jpg)
491445

492446
Path to quantity of Series
493447

@@ -651,7 +605,7 @@ export periodically our data in order to avoid that.
651605
pickle.dump(series_links, open("series_links.p","wb"))
652606
```
653607

654-
## From series to Comics
608+
## 从series到Comics
655609

656610
Now, we have to take one more step to go through Series links and extract
657611
links for the comics itself. Prepare yourselves, cause the execution of this
@@ -693,11 +647,7 @@ perform this extraction code. We will inspect the element to understand the
693647
structure that contains the links that we want, and then, via XPath, we will
694648
see if this structure exists:
695649

696-
[![Comixology - Series - Collected Editions
697-
div](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05
698-
/comixology_2_X-series_collected-
699-
1024x351.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05
700-
/comixology_2_X-series_collected.jpg)
650+
[![Comixology - Series - Collected Editions div](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_X-series_collected-1024x351.jpg)](http://felipegalvao.com.br/blog/wp-content/uploads/2016/05/comixology_2_X-series_collected.jpg)
701651

702652
Comixology - Series - Collected Editions div
703653

@@ -1177,7 +1127,7 @@ And the final part of the function will be like this:
11771127
And finally, to the last step! The extraction of information from each comic
11781128
link.
11791129

1180-
## The last step: extraction of information from comics
1130+
## 最后一步:从comics提取信息
11811131

11821132
The last step is relatively simple. We'll have to go into each comic link and
11831133
extract all the information that is possible from these links. One of the
@@ -1508,7 +1458,7 @@ there is a clear element that contains this number. Let's see:
15081458
And so, we complete the information gathering functions. Now, let's set a few
15091459
things so that they all get linked.
15101460

1511-
## Final Touches
1461+
## 最后的润色
15121462

15131463
We will need three more functions. The first will simply join the comics of
15141464
the links, which are scattered in files, in a single list. The second function

0 commit comments

Comments
 (0)