This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
forked from nickoala/telepot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemodi.py
50 lines (37 loc) · 1.47 KB
/
emodi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import sys
import time
import telepot
import telepot.namedtuple
"""
$ python2.7 emodi.py <token>
Emodi: An Emoji Unicode Decoder - You send me an emoji, I give you the unicode.
Caution: Python's treatment of unicode characters longer than 2 bytes (which
most emojis are) varies across versions and platforms. I have tested this program
on Python2.7.3/Raspbian. If you try it on other versions/platforms, the length-
checking and substring-extraction below may not work as expected.
"""
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
m = telepot.namedtuple.namedtuple(msg, 'Message')
if chat_id < 0:
# group message
print 'Received a %s from %s, by %s' % (content_type, m.chat, m.from_)
else:
# private message
print 'Received a %s from %s' % (content_type, m.chat) # m.chat == m.from_
if content_type == 'text':
reply = ''
# For long messages, only return the first 10 characters.
if len(msg['text']) > 10:
reply = u'First 10 characters:\n'
# Length-checking and substring-extraction may work differently
# depending on Python versions and platforms. See above.
reply += msg['text'][:10].encode('unicode-escape').decode('ascii')
bot.sendMessage(chat_id, reply)
TOKEN = sys.argv[1] # get token from command-line
bot = telepot.Bot(TOKEN)
bot.notifyOnMessage(handle)
print 'Listening ...'
# Keep the program running.
while 1:
time.sleep(10)