def init(self): self.groupchats = GroupChatsTelegram(self) self.bot = None # User Bot self.tg = pytg.Telegram(TELEGRAM_BIN, TELEGRAM_PUB) # Create processing pipeline pipeline = broadcast([ tg_message(self.input_message(self.tg)) ]) self.tg.register_pipeline(pipeline) # Start telegram cli self.tg.start()
def main(): import signal signal.signal(signal.SIGINT, sigint_handler) tg = pytg.Telegram(telegram="/path/to/tg/bin/telegram-cli", pubkey_file="/path/to/tg/tg-server.pub") tg.stopCLI() import time print("Wait a bit (100s), you can test Ctrl-C here.") time.sleep(100) print("wait100.1") time.sleep(100) print("wait100.2") while not _QUIT: time.sleep(1) print("Quit it!") return
def __init__(self, args, config): self.args = args self.config = config if not os.path.exists(self.config.telegram.path): LOG.error('Cannot find telegram client! Path: %s' % self.config.telegram.path) exit(-1) self.tg = pytg.Telegram(telegram=self.config.telegram.path, pubkey_file=self.config.telegram.pubkey, custom_cli_args=['--disable-colors']) self.send = self.tg.sender self.recv = self.tg.receiver # Other stuff we need self._me = None self._contacts = []
def start_messaging_Telegram( path_Telegram_CLI_executable="/usr/share/tg/bin/telegram-cli", path_Telegram_CLI_public_key_file="/usr/share/tg/tg-server.pub", launch=True): if not os.path.isfile(path_Telegram_CLI_executable): print("executable not found: {path}".format( path=path_Telegram_CLI_executable)) sys.exit() if not os.path.isfile(path_Telegram_CLI_public_key_file): print("public key not found: {path}".format( path=path_Telegram_CLI_public_key_file)) sys.exit() if not shijian.running("telegram-cli"): print("\nlaunch Telegram CLI\n") command =\ """ {path_Telegram_CLI_executable} \ -R \ -W \ -P 4458 \ -k {path_Telegram_CLI_public_key_file} \ --json \ --permanent-peer-ids \ --permanent-peer-ids \ --disable-output \ --daemonize """.format( path_Telegram_CLI_executable = path_Telegram_CLI_executable, path_Telegram_CLI_public_key_file = path_Telegram_CLI_public_key_file, ) engage_command(command=command, background=True) global tg tg = pytg.Telegram(telegram=path_Telegram_CLI_executable, pubkey_file=path_Telegram_CLI_public_key_file) global tg_sender tg_sender = tg.sender
# Send pong respond to this chat group tg.msg(msg['cmdgroup'], 'pong') # quit command elif cmd[0].lower() == 'quit': if msg['uid'] == '1234567': # Put your user id here tg.msg(msg['cmdgroup'], 'By your command') QUIT = True except GeneratorExit: pass if __name__ == '__main__': # Instantiate Telegram class telegram = './telegram' pubkey = 'tg.pub' tg = pytg.Telegram(telegram, pubkey) # Create processing pipeline # Bot will respond to command the posted in this chat group grpname = 'kangkung for dummies' pipeline = message(command_parser(grpname, tg)) # Register our processing pipeline tg.register_pipeline(pipeline) # Start telegram cli tg.start() while True: # Keep on polling so that messages will pass through our pipeline tg.poll()