-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegram.py
149 lines (135 loc) · 5.01 KB
/
telegram.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import requests, json
from vlcclient import VLCClient
from lyrics import getLyrics
from threading import Thread
from BasicInfo import getName, getStatus
from queue import Queue
from yPlayer import ytubePlayer
class NetworkError(Exception):
pass
class VLCConnectionError(Exception):
pass
# token = 'bot238441806:AAHC5l1Bm3kvR38Ap-WsPYBmq0i7EaMXvHA'
token = ''
with open('data/token.txt') as f:
for line in f:
token = line
break
class Telegram():
def __init__(self):
self.running = True
self.vlc = VLCClient("::1")
self.media = 'vlc'
self.base = "https://api.telegram.org/bot%s/" % token
try:
self.vlc.connect()
print getStatus()
if not getStatus():
print "fndf"
raise
except:
self.vlc = ytubePlayer()
self.media = 'ytube'
def get(self, command, payload=None):
try:
r = requests.get(self.base + command, params=payload)
except:
raise NetworkError
return json.loads(r.text)
def start(self):
updates = self.get('getupdates')
while self.running:
if self.media == 'vlc':
try:
self.vlc.status()
except:
print "ss"
raise VLCConnectionError
if updates['result'] != []:
offset = updates['result'][-1]['update_id']
payload = {"offset": offset + 1}
else:
payload = None
updates = self.get('getupdates', payload)
if updates['result'] != []:
for result in updates['result']:
message = result['message']['text']
user_id = result['message']['from']['id']
# Replying
reply = self.action(message)
if reply:
keyboardLayout = [["Play", "Pause"], ["Lyrics", "Next"]]
reply_markup = {"keyboard": keyboardLayout, "resize_keyboard": False, "one_time_keyboard": False}
reply_markup = json.dumps(reply_markup)
payload = {'text': reply, 'chat_id': str(user_id), "reply_markup": reply_markup}
send_msg = self.get('sendmessage', payload)
# print send_msg
if reply == send_msg['result']['text']:
pass # Success
def action(self, msg):
msg = msg.lower().strip()
try:
if msg == 'play':
self.vlc.play()
elif msg == 'pause':
self.vlc.pause()
elif msg == 'stop' and self.media == 'vlc':
self.vlc.stop()
elif msg == 'next' and self.media == 'vlc':
self.vlc.next()
elif msg == 'prev' and self.media == 'vlc':
self.vlc.prev()
elif msg == 'fscreen' and self.media == 'vlc':
self.vlc.set_fullscreen(True)
elif msg == 'rfscreen' and self.media == 'vlc':
self.vlc.set_fullscreen(False)
elif msg == 'rewind' and self.media == 'vlc':
self.vlc.rewind()
elif msg == 'volume' and self.media == 'vlc':
return "Current volume is", self.vlc.volume()
elif (msg == 'vup' or msg == 'volup') and self.media == 'vlc':
return "Current volume is", self.vlc.volup(2)
elif (msg == 'vdown' or msg == 'voldown') and self.media == 'vlc':
return "Current volume is", self.vlc.voldown(2)
elif msg == 'help':
s = ''' Commands:-
Pause, play, stop, next, prev
vup- for volume Up
vdown - for volume down
fscreen - to set full screen
rfscreen - for reverse.
lyrics - lyrics for current song.
'''
return s
elif msg == 'lyrics':
q = Queue()
song = getName()
status = getStatus()
if status and song != '':
song = song[:-4]
else:
try:
song = requests.get('http://127.0.0.1:5000/getSong').text
song = song.strip("'b").strip()
except:
pass
if song:
threadl = Thread(target=getLyrics, args=(song, q))
threadl.daemon = True
threadl.start()
threadl.join()
self.lyrics = q.get()
return self.lyrics
else:
print "Invalid command ", msg
return "Invalid Command."
return "Command executed successfully."
except Exception as e:
print "Error", str(e)
raise VLCConnectionError
def stop(self):
try:
self.vlc.disconnect()
except:
pass
self.running = False