tg/tg-test.py

72 lines
1.5 KiB
Python
Raw Normal View History

import tgl
import pprint
from functools import partial
2015-05-03 00:32:30 -04:00
our_id = 0
pp = pprint.PrettyPrinter(indent=4)
binlog_done = False;
2015-05-03 00:32:30 -04:00
def on_binlog_replay_end():
binlog_done = True;
2015-05-03 00:32:30 -04:00
def on_get_difference_end():
pass
def on_our_id(id):
our_id = id
2015-05-23 20:09:08 -04:00
return "Set ID: " + str(our_id)
2015-05-03 00:32:30 -04:00
2015-05-12 04:04:47 -04:00
def msg_cb(success, msg):
pp.pprint(success)
pp.pprint(msg)
HISTORY_QUERY_SIZE = 100
2015-05-20 02:43:08 -04:00
def history_cb(msg_list, peer, success, msgs):
print(len(msgs))
msg_list.extend(msgs)
print(len(msg_list))
if len(msgs) == HISTORY_QUERY_SIZE:
2015-05-20 02:43:08 -04:00
tgl.get_history(peer, len(msg_list), HISTORY_QUERY_SIZE, partial(history_cb, msg_list, peer));
2015-05-23 20:09:08 -04:00
def cb(success):
print(success)
def on_msg_receive(msg):
if msg.out and not binlog_done:
2015-05-04 01:42:11 -04:00
return;
2015-05-20 02:43:08 -04:00
if msg.dest.id == our_id: # direct message
peer = msg.src
else: # chatroom
2015-05-20 02:43:08 -04:00
peer = msg.dest
2015-05-20 04:28:23 -04:00
pp.pprint(msg)
2015-05-20 02:43:08 -04:00
if msg.text.startswith("!ping"):
print("SENDING PONG")
2015-05-23 20:09:08 -04:00
peer.send_msg("PONG!", msg_cb)
peer.send_contact(msg.src.phone, msg.src.first_name, msg.src.last_name , cb)
2015-05-20 02:43:08 -04:00
2015-05-03 00:32:30 -04:00
def on_secret_chat_update(peer, types):
return "on_secret_chat_update"
def on_user_update():
pass
def on_chat_update():
pass
2015-05-09 05:10:24 -04:00
# 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)