tg-test.py 1.42 KB
Newer Older
1 2
import tgl
import pprint
3 4
from functools import partial

5

6 7 8
our_id = 0
pp = pprint.PrettyPrinter(indent=4)

9 10
binlog_done = False;

11
def on_binlog_replay_end():
12
    binlog_done = True;
13 14 15 16

def on_get_difference_end():
    pass

17 18
def on_our_id(id):
    our_id = id
19
    return "Set ID: " + str(our_id)
20

21 22 23 24
def msg_cb(success, msg):
    pp.pprint(success)
    pp.pprint(msg)

25 26
HISTORY_QUERY_SIZE = 100

27
def history_cb(msg_list, peer, success, msgs):
28 29 30 31
  print(len(msgs))
  msg_list.extend(msgs)
  print(len(msg_list))
  if len(msgs) == HISTORY_QUERY_SIZE:
32
    tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
33

34 35 36

def cb(success):
    print(success)
37

38
def on_msg_receive(msg):
39
    if msg.out and not binlog_done:
40 41
      return;

42 43
    if msg.dest.id == our_id: # direct message
      peer = msg.src
44
    else: # chatroom
45
      peer = msg.dest
46

47
    pp.pprint(msg)
48
    if msg.text.startswith("!ping"):
49
      peer.send_msg("PONG! google.com", preview=False, reply=msg.id)
50 51


52 53 54 55 56 57 58 59 60
def on_secret_chat_update(peer, types):
    return "on_secret_chat_update"

def on_user_update():
    pass

def on_chat_update():
    pass

61 62 63 64 65 66 67 68 69
# Set callbacks
tgl.set_on_binlog_replay_end(on_binlog_replay_end)
tgl.set_on_get_difference_end(on_get_difference_end)
tgl.set_on_our_id(on_our_id)
tgl.set_on_msg_receive(on_msg_receive)
tgl.set_on_secret_chat_update(on_secret_chat_update)
tgl.set_on_user_update(on_user_update)
tgl.set_on_chat_update(on_chat_update)