forked from robbcocco/PokemonGo-Map-forTelegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
79 lines (68 loc) · 2.62 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
import sys
import time
import telepot
import os
import datetime
from selenium import webdriver
"""
Run with
$ python3.4 bot.py <token> <PTC username> <PTC password> <steps> <host> <port> <gmaps key>
"""
def handle(msg):
actual_date = time.time()
flavor = telepot.flavor(msg)
chat_id = msg['chat']['id']
command = msg['text']
sender = msg['from']
msg_date = msg['date']
summary = telepot.glance(msg, flavor=flavor)
if command.lower().startswith('/pokemap'):
if command.count(' ') >= 1:
# print info
print('Sender: ', sender)
print('Command: ', command)
print('Msg date: ', msg_date)
print('Act date: ', actual_date)
# avoid old messages
if actual_date-msg_date < 3:
# save the location into a variable
locTemp = command.split(' ', 1)
location = locTemp[1]
# run the shell command
#os.system('python2.7 PokemonGo-Map-develop/runserver.py -a ptc -u %s -p %s -l "%s" -st %s -H %s -P %s >mapstd.txt 2>maperr.txt &' % (USER, PASS, location, STEP, HOST, PORT))
os.system('python2.7 PokemonGo-Map-1.0/example.py -a ptc -u %s -p %s -l "%s" -st %s -H %s -P %s >mapstd.txt 2>maperr.txt &' % (USER, PASS, location, STEP, HOST, PORT))
# let the map load a minute
bot.sendMessage(chat_id, 'Wait a minute...')
time.sleep(60)
# initialize the page
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 1024)
driver.get('http://%s:%s' % (HOST, PORT))
# let the page load
time.sleep(3)
# save a screenshot
driver.save_screenshot('loc.png')
# kill the map
#os.system('pkill -f runserver.py')
os.system('pkill -f example.py')
os.system('pkill -f node')
os.system('pkill -f phantomjs')
# send the screenshot
bot.sendChatAction(chat_id, 'upload_photo')
bot.sendPhoto(chat_id, open('loc.png', 'rb'), caption=location)
else:
bot.sendMessage(chat_id, 'I\'m now avaiable')
else:
bot.sendMessage(chat_id, 'Correct syntax is "/pokemap location"')
TOKEN = sys.argv[1] # get token from command-line
USER = sys.argv[2]
PASS = sys.argv[3]
STEP = sys.argv[4]
HOST = sys.argv[5]
PORT = sys.argv[6]
#GKEY = sys.argv[7]
bot = telepot.Bot(TOKEN)
bot.message_loop(handle)
# Keep the program running.
while 1:
time.sleep(10)