Skip to content

Instantly share code, notes, and snippets.

@choupi
Last active June 11, 2017 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choupi/7ac0c7386998b2d1cd42eb9f87db126d to your computer and use it in GitHub Desktop.
Save choupi/7ac0c7386998b2d1cd42eb9f87db126d to your computer and use it in GitHub Desktop.
FB chatbot for pycon 2017 QA game with modified fbchat
--- client.py.orig 2017-06-11 21:28:19.232693710 +0800
+++ client.py 2017-06-09 15:25:46.000000000 +0800
@@ -442,7 +442,7 @@
users.append(User(entry))
return users # have bug TypeError: __repr__ returned non-string (type bytes)
- def send(self, recipient_id=None, message=None, is_user=True, like=None, image_id=None, add_user_ids=None):
+ def send(self, recipient_id=None, message=None, is_user=True, like=None, image_id=None, add_user_ids=None, payload=None):
"""Send a message with given thread id
:param recipient_id: the user id or thread id that you want to send a message to
@@ -510,6 +510,9 @@
data['has_attachment'] = image_id is not None
data['specific_to_list[0]'] = 'fbid:' + str(recipient_id)
data['specific_to_list[1]'] = 'fbid:' + str(self.uid)
+ if payload:
+ for i in payload:
+ data[i]=payload[i]
if image_id:
data['image_ids[0]'] = image_id
(dp0
V\u984c\u76ee: \u4e0b\u5217\u54ea\u500b\u4e0d\u662f Python \u5be6\u4f5c\u7684 web framework
p1
VLaravel
p2
sV\u984c\u76ee: \u8acb\u554f\u6c9b\u661f\u4e92\u52d5\u79d1\u6280\u76ee\u524d\u6709\u54ea\u4e9b\u8077\u4f4d\u958b\u7f3a\uff1f
p3
V\u5168\u90e8\u7686\u6709
p4
sV\u984c\u76ee: \u5728 Python \u88e1, (i for i in range(3)) \u662f\u4ec0\u9ebc\uff1f
p5
Vgenerator
p6
sV\u984c\u76ee: Python \u6c92\u6709\u5167\u5efa\u4e0b\u5217\u54ea\u4e00\u500b command line \u5de5\u5177
p7
VFTP client
p8
sV\u984c\u76ee: PyCon Taiwan 2017 \u6c92\u6709\u4f86\u81ea\u54ea\u4e00\u500b\u570b\u5bb6\u7684\u8b1b\u8005\uff1f
p9
V\u7d10\u897f\u862d
p10
sV\u984c\u76ee: PyCon APAC 2017 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p11
V\u99ac\u4f86\u897f\u4e9e
p12
sV\u984c\u76ee: PyCon APAC \u76ee\u524d\u5728\u53f0\u7063\u8209\u8fa6\u904e\u5e7e\u6b21\uff1f
p13
V2
p14
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u8005\u4e0d\u662f CPython \u5c08\u6848\u88ab\u8f49\u79fb\u5230 GitHub \u4e0a\u7684\u539f\u56e0\u4e4b\u4e00\uff1f
p15
VGitHub \u7684 issues \u7cfb\u7d71\u6bd4\u8f03\u597d\u505a\u5c08\u6848\u7ba1\u7406
p16
sV\u984c\u76ee: \u4e0b\u5217\u54ea\u4e00\u500b\u5957\u4ef6\u300c\u4e0d\u5728\u300dPython \u6a19\u6e96\u5eab\u5167\uff1f
p17
Vrequests
p18
sV\u984c\u76ee: \u4ee5\u4e0b\u54ea\u7a2e\u985e\u578b\u4e0d\u53ef\u4ee5\u4f5c\u70ba dict \u7684 key
p19
Vdict
p20
sV\u984c\u76ee: range(10) \u7684\u9996\u500b\u503c\u662f\uff1f
p21
V0
p22
sV\u984c\u76ee: TibaMe \u7d66\u8b1b\u5e2b\u7684\u5206\u6f64\u65b9\u5f0f\u70ba
p23
V\u5168\u90e8\u7686\u662f
p24
sV\u984c\u76ee: \u4ee5\u4e0b\u54ea\u500b\u7db2\u7ad9\u7684\u5f8c\u7aef\u4e0d\u4f7f\u7528 Python\uff1f
p25
VFacebook
p26
sV\u984c\u76ee: Python \u7684 antigravity module \u9019\u500b\u5f69\u86cb\u7684\u4f86\u6e90\u662f\u54ea\u500b\u6f2b\u756b\u7db2\u7ad9\uff1f
p27
Vxkcd
p28
sV\u984c\u76ee: \u4ee5\u4e0b\u6558\u8ff0\u4e0d\u662f\u5408\u6cd5\u7684 Python \u8a9e\u6cd5\uff1f
p29
Va >< b
p30
sV\u984c\u76ee: python.org.tw \u6700\u65e9\u662f\u8ab0\u8a3b\u518a\u7684\uff1f
p31
VSong Huang
p32
sV\u984c\u76ee: \u8acb\u554f Appier \u514d\u8cbb\u4f9b\u61c9\u904e\u4ee5\u4e0b\u54ea\u7a2e\u98f2\u6599\uff1f
p33
V\u5168\u90e8\u7686\u6709
p34
sV\u984c\u76ee: Python \u521d\u7248\u662f\u4ec0\u9ebc\u6642\u5019\u5beb\u51fa\u4f86\u7684\uff1f
p35
V1991
p36
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u8005\u4e0d\u662f Python \u7684\u57fa\u672c\u7d50\u69cb
p37
VTree
p38
sV\u984c\u76ee: PyCon APAC 2014 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p39
V\u53f0\u7063
p40
sV\u984c\u76ee: \u5982\u4f55\u7528 Python \u8868\u793a\u300c2 \u7684\u958b\u6839\u865f\u300d\uff1f\uff08\u4e0d\u4f7f\u7528 import\uff09
p41
V2 ** 0.5
p42
sV\u984c\u76ee: assert statement \u6703\u6839\u64da\u54ea\u500b global variable \u6c7a\u5b9a\u5ffd\u7565\u8207\u5426\uff1f
p43
V__debug__
p44
sV\u984c\u76ee: Python \u7684\u6a19\u6e96\u51fd\u5f0f\u5eab\u5305\u542b\u4ee5\u4e0b\u54ea\u500b\u8cc7\u6599\u5eab\u7684\u8b80\u5beb\u652f\u63f4\uff1f
p45
VSQLite3
p46
sV\u984c\u76ee: Python \u539f\u55ae\u5b57\u610f\u601d\u4ee3\u8868\u4f55\u7a2e\u52d5\u7269\uff1f
p47
V\u87d2\u86c7
p48
sV\u984c\u76ee: \u70ba\u4f55 Python \u4f5c\u8005\u6703\u4f7f\u7528 Python \u7576\u4f5c\u7a0b\u5f0f\u8a9e\u8a00\u7684\u540d\u7a31\uff1f
p49
V\u82f1\u570b\u559c\u5287\u5718\u5718\u540d Monty Python
p50
sV\u984c\u76ee: \u54ea\u500b Python \u5167\u5efa\u6a21\u7d44\u63d0\u4f9b\u4efb\u610f\u4f4d\u6578\u7cbe\u78ba\u7684\u5c0f\u6578\uff08\u6d6e\u9ede\u6578\uff09\u8a08\u7b97\uff1f
p51
Vdecimal
p52
sV\u984c\u76ee: Python \u9810\u8a2d\u4e92\u52d5\u4ecb\u9762\u7684\u63d0\u793a\u5b57\u5143\u6709\u5e7e\u500b\u300c>\u300d\uff1f
p53
V3
p54
sV\u984c\u76ee: \u6c9b\u661f\u4e92\u52d5\u79d1\u6280\u7684\u82f1\u6587\u540d\u5b57\u662f
p55
VAppier
p56
sV\u984c\u76ee: PyCon Taiwan 2017 \u57fa\u8abf\u8b1b\u8005 Carol Willing \u7684\u4e2d\u6587\u540d\u5b57\u662f
p57
V\u738b\u6d77\u82e5
p58
sV\u984c\u76ee: PyCon Taiwan \u7684\u5b98\u65b9\u7db2\u7ad9\u662f\u7528\u54ea\u500b web framework \u5beb\u6210\uff1f
p59
VDjango
p60
sV\u984c\u76ee: Python \u4f5c\u8005\u662f\u8d81\u4ec0\u9ebc\u5047\u671f\u5b8c\u6210 Python \u521d\u7248\uff1f
p61
V\u8056\u8a95\u7bc0
p62
sV\u984c\u76ee: \u8acb\u554f\u5fae\u8edf\u6524\u4f4d\u4e0a\u4e3b\u8981\u5c55\u793a\u4f55\u7a2e\u61c9\u7528\uff1f
p63
VCognitive Services \u667a\u80fd\u8fa8\u8b58\u670d\u52d9
p64
sV\u984c\u76ee: \u8acb\u554f\u6c9b\u661f\u4e92\u52d5\u79d1\u6280\u7684\u6700\u6709\u5438\u5f15\u529b\u7684\u5730\u65b9\u662f\u4ec0\u9ebc\uff1f
p65
V\u5168\u90e8\u7686\u662f
p66
sV\u984c\u76ee: \u8acb\u554f Top Career \u7684\u7e3d\u516c\u53f8\u5728\u90a3\uff1f
p67
V\u6771\u4eac
p68
sV\u984c\u76ee: TibaMe \u600e\u9ebc\u5ff5\uff1f
p69
V\u63d0\u62d4\u7c73
p70
sV\u984c\u76ee: (Python 3) 1/2 \u662f
p71
Vfloat
p72
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u8005\u4e0d\u662f Python 3 \u7684 built-in function
p73
Vreduce
p74
sV\u984c\u76ee: \u7b2c\u4e00\u500b\u8209\u8fa6\u7684\u5927\u578b Python \u611b\u597d\u8005\u7814\u8a0e\u6703\u7d44\u7e54\u4eca\u5929\u88ab\u7a31\u4f5c
p75
VEuroPython
p76
sV\u984c\u76ee: Python \u7684\u6a19\u6e96\u51fd\u5f0f\u5eab\u4e0d\u5305\u542b\u4ee5\u4e0b\u54ea\u500b\u6587\u4ef6\u683c\u5f0f\u7684\u652f\u63f4\uff1f
p77
VYAML
p78
sV\u984c\u76ee: \u5982\u4f55\u53d6\u51fa\u4e00\u500b list \u4e2d\u6240\u6709\u6bd4 5 \u5927\u7684\u503c
p79
V[k for k in alist if k > 5]
p80
sV\u984c\u76ee: \u8acb\u554f Python \u76ee\u524d\u7684\u6700\u65b0\u7248\u672c\u662f\uff1f
p81
V3.6.x
p82
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u7a2e\u662f Python \u6a19\u6e96\u5eab\u6c92\u6709\u652f\u6301\u7684 hash \u65b9\u5f0f
p83
Vprotobuf
p84
sV\u984c\u76ee: a[::-1] \u8207\u4ee5\u4e0b\u54ea\u500b\u7a0b\u5f0f\u6700\u63a5\u8fd1
p85
Vreversed(a)
p86
sV\u984c\u76ee: (Python 2) 1/2 \u662f
p87
Vint
p88
sV\u984c\u76ee: type(object) \u6703\u8f38\u51fa
p89
Vtype
p90
sV\u984c\u76ee: PyCon Taiwan \u5f9e\u54ea\u4e00\u5e74\u958b\u59cb\u8209\u8fa6\uff1f
p91
V2012
p92
sV\u984c\u76ee: PyCon Taiwan 2017 \u731c\u8b0e\u6a5f\u5668\u4eba\u4e2d\u4f7f\u7528\u7684\u8cc7\u6599\u5eab\u70ba
p93
VPostgreSQL
p94
sV\u984c\u76ee: \u8acb\u554f\u6c9b\u661f\u4e92\u52d5\u79d1\u6280\u76ee\u524d\u6709\u54ea\u4e9b\u798f\u5229\uff1f
p95
V\u5168\u90e8\u7686\u6709
p96
sV\u984c\u76ee: \u5728\u6c9b\u661f\u4e92\u52d5\u79d1\u6280\u4f60\u6703\u9047\u5230
p97
V\u5168\u90e8\u7686\u6709
p98
sV\u984c\u76ee: \u54ea\u4e00\u53e5\u8a71\u4e0d\u5728 import this \u88e1\uff1f
p99
VLife is short, use Python!
p100
sV\u984c\u76ee: CPython \u9810\u8a2d\u7684\u905e\u8ff4\u6df1\u5ea6\u662f\uff1f
p101
V99
p102
sV\u984c\u76ee: 2020 \u5e74 Python 2.x EOL \u5728\u5373\uff0c\u4f46 Python 2/3 \u5927\u6230\u5f9e\u672a\u505c\u6b47\u3002Python 3.0 \u662f\u5728\u4f55\u6642\u6b63\u5f0f\u767c\u5e03\uff1f
p103
V2008
p104
sV\u984c\u76ee: Python \u4e2d a + b \u7b49\u50f9\u65bc
p105
Va.__add__(b)
p106
sV\u984c\u76ee: PyCon Taiwan 2017 \u5b98\u65b9\u7db2\u7ad9 repository \u540d\u70ba
p107
Vpycontw2016
p108
sV\u984c\u76ee: \u60f3\u8981\u7528 Python \u8655\u7406\u4efb\u610f\u7cbe\u78ba\u5ea6\u7684\u6578\u503c\u8a08\u7b97\uff0c\u61c9\u8003\u616e\u4f7f\u7528\u4f55\u578b\u5225\uff1f
p109
VDecimal
p110
sV\u984c\u76ee: PyCon APAC 2016 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p111
V\u97d3\u570b
p112
sV\u984c\u76ee: \u4ee5\u4e0b\u54ea\u500b\u662f PyCon Taiwan 2017 \u65b0\u7684\u6d3b\u52d5\uff1f
p113
VUnconference
p114
sV\u984c\u76ee: \u6b63\u898f\u8868\u793a\u5f0f [0-9a-e]{2}\u005cd \u4e0d\u80fd match \u54ea\u500b\u5b57\u4e32
p115
V03e
p116
sV\u984c\u76ee: \u300cPython \u5b98\u65b9\u8aaa\u660e\u6587\u4ef6\u4e2d\u6587\u7ffb\u8b6f\u8a08\u756b\u300d\u662f\u7531\u8ab0\u767c\u8d77\u7684\uff1f
p117
Vliang2
p118
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u8005\u4e0d\u662f Python \u5beb\u7684\u5de5\u5177
p119
VChef
p120
sV\u984c\u76ee: Python \u4e2d list(range(1,10,2)) \u7684\u7d50\u679c\u70ba
p121
V[1, 3, 5, 7, 9]
p122
sV\u984c\u76ee: \u4ee5\u4e0b\u4f55\u8005\u4e0d\u662f Python \u5408\u6cd5\u7684\u8a9e\u6cd5
p123
V\u7686\u5408\u6cd5
p124
sV\u984c\u76ee: \u57f7\u884c "abc" * True \u6703\u5f97\u5230\uff1f
p125
V\u005c"abc\u005c"
p126
sV\u984c\u76ee: SyntaxError: not a chance \u767c\u751f\u5728\u4ee5\u4e0b\u54ea\u500b\u6307\u4ee4
p127
Vfrom __future__ import braces
p128
sV\u984c\u76ee: PyCon APAC 2015 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p129
V\u53f0\u7063
p130
sV\u984c\u76ee: Python \u6c92\u6709\u4ee5\u4e0b\u54ea\u500b\u7279\u6027\uff1f
p131
Vmulti-line comment
p132
sV\u984c\u76ee: \u57f7\u884c "abc" * False \u6703\u5f97\u5230\uff1f
p133
V\u005c"\u005c"
p134
sV\u984c\u76ee: \u4e0b\u5217\u54ea\u500b\u8cc7\u6599\u5206\u6790\u5de5\u5177\u662f\u7528 Python \u5beb\u6210\u7684\uff1f
p135
VPandas
p136
sV\u984c\u76ee: \u54ea\u4e00\u500b\u8d0a\u52a9\u5546\u548c\u672c\u6b21\u7684 keynote speakers \u95dc\u4fc2\u6700\u5927\uff1f
p137
VAppier
p138
sV\u984c\u76ee: PyCon APAC 2013 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p139
V\u65e5\u672c
p140
sV\u984c\u76ee: \u4ee5\u4e0b\u54ea\u500b\u8a9e\u6cd5\u5728 Python 2 \u4e0d\u6703\u56de\u50b3 True\uff1f
p141
VTrue + True
p142
sV\u984c\u76ee: PyCon Taiwan 2017 \u731c\u8b0e\u6a5f\u5668\u4eba\u7684\u6700\u4f4e Python \u7248\u672c\u9700\u6c42\u70ba
p143
V3.6
p144
sV\u984c\u76ee: \u4ee5\u4e0b\u54ea\u500b\u4e0d\u662f Python \u5e38\u7528\u7684\u8ff4\u5708\u65b9\u5f0f
p145
Vdo: ...; wihle True
p146
sV\u984c\u76ee: \u7d66\u5b9a year = 2016; country ='TW' \u4e0b\u5217\u4f55\u8005\u5b57\u4e32\u64cd\u4f5c\u5728 Python 3.6 \u4e2d\u4e0d\u5408\u6cd5\uff1f
p147
V\u005c"PyCon %d %s\u005c" % year, country
p148
sV\u984c\u76ee: \u4e0b\u5217\u54ea\u4e00\u500b\u793e\u7fa4\u4e3b\u8fa6\u4eba\u4e4b\u4e00\u300c\u4e0d\u662f\u300d GliaCloud \u7684\u54e1\u5de5\uff1f
p149
VRails Girls
p150
sV\u984c\u76ee: import this \u6703\u5370\u51fa Python \u7bb4\u8a00\u3002this.py \u6a21\u7d44\u4e2d\u5c0d\u7bb4\u8a00\u5b57\u4e32\u4f7f\u7528\u4e86\u4f55\u7a2e\u7de8\u78bc\u8f49\u63db\uff1f
p151
Vrot13
p152
sV\u984c\u76ee: \u4e0b\u5217\u4f55\u8005\u4e0d\u662f Python \u7684 built-in function\uff1f
p153
Vsqrt
p154
sV\u984c\u76ee: \u7531 Python \u4f5c\u8005\u5275\u5efa\uff0c\u76ee\u524d Python \u8a9e\u8a00\u7684\u53c3\u8003\u5be6\u4f5c\u7a31\u70ba
p155
VCPython
p156
sV\u984c\u76ee: round(1234.5678, -2) \u7684\u7d50\u679c\u662f\uff1f
p157
V\u005c"1200.0\u005c"
p158
sV\u984c\u76ee: PyCon APAC 2012 \u5728\u54ea\u570b\u8209\u8fa6\uff1f
p159
V\u65b0\u52a0\u5761
p160
sV\u984c\u76ee: \u4e0b\u5217\u4f55\u8005\u300c\u4e0d\u662f\u300d\u898f\u7bc4 Python \u7a0b\u5f0f\u78bc\u98a8\u683c\u7684 PEP\uff1f
p161
VPEP3107
p162
sV\u984c\u76ee: \u5982\u4f55\u53d6\u51fa\u4e00\u500b list \u4e2d\u7684\u6240\u6709\u5076\u6578\u6b04\u4f4d\u503c\uff08\u4f8b\uff1a[1, 2, 3, 4] \u2192 [2, 4]\uff09
p163
Vx[1::2]
p164
sV\u984c\u76ee: Python 2.7 \u7684 End of Life \u5728\u54ea\u4e00\u5e74\uff1f
p165
V2020
p166
sV\u984c\u76ee: type(type) \u6703\u8f38\u51fa
p167
Vtype
p168
sV\u984c\u76ee: Python \u4f5c\u8005\u662f\u54ea\u570b\u4eba\uff1f
p169
V\u8377\u862d
p170
sV\u984c\u76ee: PyCon Taiwan 2017 mobile app \u958b\u767c\u6c92\u6709\u4f7f\u7528\u4ec0\u9ebc\u6280\u8853\uff1f
p171
VRollout
p172
sV\u984c\u76ee: \u53f0\u7063\u54ea\u500b\u5730\u5340\u6c92\u6709 Python \u793e\u7fa4\u805a\u6703\uff1f
p173
V\u5b9c\u862d
p174
sV\u984c\u76ee: asyncio \u65bc Python \u54ea\u500b\u7248\u672c\u9032\u5165\u6a19\u6e96\u5eab\uff1f
p175
V3.4
p176
sV\u984c\u76ee: \u4e0b\u5217\u54ea\u500b\u4e0d\u662f Python \u8cc7\u6599\u5206\u6790\u6642\u53ef\u80fd\u7528\u5230\u7684\u5957\u4ef6\uff1f
p177
Ve1071
p178
s.
import fbchat
import sys
import getpass
import random
import pickle
import time
import json
try:
with open('sess.pickle', 'rb') as f:
session_cookies=pickle.load(f)
client = fbchat.Client(None, None, debug=False, session_cookies=session_cookies)
#client._session.cookies=session_cookies
except:
ID=raw_input('email:')
PW=getpass.getpass('passwd:')
client = fbchat.Client(ID, PW)
session_cookies = client._session.cookies
print session_cookies
with open('sess.pickle', 'wb') as f:
pickle.dump(session_cookies, f)
#friend=client.getUsers("pycontwchatbot")
fuid='299082580532144'
try:
with open('db.pickle', 'rb') as f:
quiz_dict=pickle.load(f)
except: quiz_dict={}
print type(quiz_dict)
msgs= client.getThreadInfo(fuid, last_n=2)
#for step in xrange(1000):
while True:
try:
for m in msgs:
print m.body.encode('utf8')
m=msgs[1]
q=m.body.split('\n')
quiz=q[0]
print quiz.encode('utf8')
if quiz in quiz_dict:
print 'Answer found'
a=quiz_dict[quiz]
else:
m=msgs[0]
j=json.loads(getattr(m, 'platform_xmd'))
print getattr(m, 'platform_xmd').encode('utf8')
#"quick_replies":[{"content_type":"text","title":"\u65b0\u7af9","payload":"\u65b0\u7af9","image_url":null,"data":null}
#print j
try: ans=[i['payload'].replace('"', r'\"') for i in j["quick_replies"]]
except:
time.sleep(2)
msgs= client.getThreadInfo(fuid, last_n=2)
continue
print ans
a=random.choice(ans)
print a.encode('utf8')
ap='{"quick_replies":{"content_type":"text","title":"%s","payload":"%s","image_url":null,"data":null},"ignore_for_webhook":false}'%(a, a)
p={'platform_xmd':ap}
#print p
client.send(fuid, a, payload=p) # modified client.send() for payload
time.sleep(2)
msgs= client.getThreadInfo(fuid, last_n=3)
m=msgs[2]
print m.body.encode('utf8')
print [m.body]
#if u'\u932f' not in m.body:
if u'\u5c0d' in m.body:
print 'Correct'
if quiz not in quiz_dict:
quiz_dict[quiz]=a
with open('db.pickle', 'wb') as f:
pickle.dump(quiz_dict, f)
print len(quiz_dict), 'answer in db'
except Exception as e:
print 'error sleep'
print e
time.sleep(5)
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment