This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
telegram.py
100 lines (79 loc) · 2.48 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
#!/usr/bin/env python3
from socket import socket, AF_INET, SOCK_STREAM
import re
import json
class LineBuffer(object):
def __init__(self):
self.sep = re.compile(b'\r?\n')
self.buf = b''
def feed(self, bytes):
self.buf += bytes
def lines(self):
lines = self.sep.split(self.buf)
self.buf = lines.pop()
return iter(lines)
def __iter__(self):
return self.lines()
class Telegram(object):
"""A class to connect telegram-cli.
"""
def __init__(self, ip_addr='127.0.0.1', port='4444'):
self._socket_init(ip_addr, port)
self.main_session()
self.buf = LineBuffer()
self.event_handlers = {
'message': None,
'read': None,
'update': None,
}
def __del__(self):
self.sock.close()
def _socket_init(self, ip_addr, port):
s = socket(AF_INET, SOCK_STREAM)
s.connect((ip_addr, port))
self.sock = s
def register_handler(self, event, handler):
self.event_handlers[event] = handler
def remove_handler(self, event):
self.event_handlers[event] = None
def process_recieved(self):
"""Process messages been recieved.
Returns:
None
"""
for m in self.buf:
try:
msg = json.loads(m.decode('utf-8', errors='ignore'))
except ValueError as e:
continue
event = msg.get('event', None)
if event is not None:
handler = self.event_handlers.get(event, None)
if callable(handler):
handler(self, msg)
def process_loop(self):
while True:
self.buf.feed(self.sock.recv(2 ** 12))
self.process_recieved()
def send_cmd(self, cmd):
if '\n' != cmd[-1]:
cmd += '\n'
self.sock.send(cmd.encode())
def main_session(self):
self.send_cmd('main_session')
def send_msg(self, peer, msg):
if not (peer.startswith('user#') or peer.startswith('chat#')):
peer = peer.replace(' ', '_').replace('#', '@')
cmd = 'msg ' + peer + ' ' + msg
self.send_cmd(cmd)
if __name__ == '__main__':
tele = Telegram('127.0.0.1', 1235)
tele.send_msg('user#67655173', 'hello')
while True:
ret = tele.recv_one_msg()
if ret == -1:
print('Connect closed')
break
else:
print(ret)
tele = None