-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
115 lines (87 loc) · 3.32 KB
/
bot.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/python3
import datetime
import time
import os
import pprint
import telepot
import pytz
from tzwhere import tzwhere
import sqlite3
from telepot.loop import MessageLoop
import random
# print = pprint.PrettyPrinter().pprint
HINT = '''
Send location to set your timezone
可以向我發送地點設置時區喔
'''
SET_SUCCESS = '''Timezone has been set to {0}
時區已經設置為 {0}
'''
DEFAULT_TIMEZONE_STR = 'America/Toronto'
def doit(chat_id, timezone):
temp = " 已經\n "
temp += datetime.datetime.now(timezone).strftime("%H:%M")
temp += "\n 了!!\n"
num = random.randint(1, 100)
if num <= 10:
temp = " 我\n 餓\n 了\n !\n"
elif num <= 20:
temp = " 幾\n 點\n 了\n ?\n"
elif num <= 30:
temp = " 愛\n 幾\n 點\n 幾\n 點\n !"
print (temp)
with open('temp.txt', 'w') as fout:
fout.write(temp)
os.system('soffice --convert-to jpg temp.txt')
os.system('convert temp.jpg -crop 125x300+50+0 temp.jpg')
os.system('convert temp.jpg -resize 200% temp.jpg')
os.system('convert temp.jpg -crop 125x300+40+50 temp.jpg')
os.system('montage temp.jpg bot.jpg -tile 1x2 -geometry +0+0 temp.jpg')
os.system('montage left.jpg temp.jpg -tile 2x1 -geometry +0+0 temp.jpg')
fout = open('temp.jpg', 'rb')
print ('sending photo...')
bot.sendPhoto(chat_id, fout)
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
# print ('Content type: %s' % content_type)
print (content_type, chat_type, chat_id)
if chat_type == 'private':
conn = sqlite3.connect('timezone.db')
c = conn.cursor()
timezone_str = ''
if content_type == 'location':
where = tzwhere.tzwhere()
loc_data = msg['location']
print (loc_data['longitude'], loc_data['latitude'])
timezone_str = where.tzNameAt(loc_data['latitude'], loc_data['longitude'])
c.execute('''select * from timezone where chatid=?''', (chat_id,))
if c.fetchone() is None:
c.execute('''insert into timezone values(?, ?)''', (chat_id, timezone_str))
else:
c.execute('''update timezone set timezone=? where chatid=?''', (timezone_str, chat_id))
bot.sendMessage(chat_id, SET_SUCCESS.format(timezone_str, timezone_str))
if timezone_str == '':
c.execute('''select * from timezone where chatid=?''', (chat_id,))
result = c.fetchone()
if result is not None:
timezone_str = result[1]
print (timezone_str)
else:
timezone_str = DEFAULT_TIMEZONE_STR
bot.sendMessage(chat_id, HINT)
conn.commit()
conn.close()
timezone = pytz.timezone(timezone_str)
doit(chat_id, timezone)
elif content_type == 'text' and '@time_image_bot' in msg['text']:
timezone = pytz.timezone(DEFAULT_TIMEZONE_STR)
doit(chat_id, timezone)
api_key = ''
with open('key_file.txt', 'r') as fin:
api_key = fin.readlines()[0].strip()
print (api_key)
bot = telepot.Bot(api_key)
MessageLoop(bot, handle).run_as_thread()
print ('I am listening ...')
while 1:
time.sleep(10)