RETRY_DELAY = 5 parser = argparse.ArgumentParser(description="Construct and send messages based on observed markov chains.") parser.add_argument("token", help="The bot's token.") parser.add_argument("user", help="The username of the user to simulate.") parser.add_argument("--command", default="talk", help="The command to trigger user simulation; default is 'talk'.") parser.add_argument("--monospace", action="store_true", default=False, help="Use monospace text for messages- beep boop!") parser.add_argument("--database", default="observer.db", nargs=1, help="The path to the SQLite database used to load the generated chains. Default is observer.db.") parser.add_argument("--parallel-chat", type=int, help="The chat ID to post into when SIGUSR1 is obtained.") args = parser.parse_args() bot = telegram.Bot(token=args.token) db = MarkovDatabase(args.database) me = bot.getMe() command = "/%s" % args.command # Fetch user ID from the database. user, first_name, last_name = db.get_user_details(args.user) def format_word(word): """Reformats a given word for display to the user, removing @ symbols in particular.""" # Strip @ symbols to prevent users from being notified. if word.startswith("@") and len(word) > 1: return word[1:] return word
import sys import os import signal from markov.database import MarkovDatabase from markov.config import observer_token, talker_tokens parser = argparse.ArgumentParser(description="Observe and construct markov chains of users' Telegram conversations.") parser.add_argument("--parallel-chat", type=int, help="An optional chat ID to mirror users' messages into.") parser.add_argument("--database", default="observer.db", nargs=1, help="The path to the SQLite database used to store the generated chains. Default is observer.db.") args = parser.parse_args() bot = telegram.Bot(token=observer_token) db = MarkovDatabase(args.database) # Spawn talkers talkers = {} for username, token in talker_tokens.iteritems(): child_args = ["python", os.path.join(sys.path[0], "talker.py"), token, username] if args.parallel_chat: child_args += ["--parallel-chat", str(args.parallel_chat)] talkers[username] = subprocess.Popen(child_args) next_update = 0 while True: try: updates = bot.getUpdates(next_update) except telegram.error.TelegramError as err: print err