def update_messages_nodes(self, groupme: GroupMe): before_id = -1 new_last_message = -1 print('Updating messages', end='') messages = groupme.get_messages() while messages is not None: for message in messages: properties = "{" for key, value in message.items(): if key != "attachments" and key != "event" and value is not None: properties += f"{key}: {repr(value)}, " properties = properties[:-2] + "}" query = f"merge (m:Message {properties})" self.graph.run(cypher=query) new_last_message = max(new_last_message, int(message["id"])) before_id = int(messages[-1]["id"]) if int(messages[-1]["id"]) < self.last_message: break print('.', end='', flush=True) messages = groupme.get_messages(before_id=before_id) print(flush=True) self.last_message = new_last_message with open(LAST_MESSAGE_FILE, "wb") as f: pickle.dump(self.last_message, f)
def group(groupID): gm = GroupMe(api_token=os.environ["GROUPME_ACCESS_TOKEN"]) group = gm.get_group_info(groupID) members = group["members"] members.sort(key=lambda x: x["name"]) messages = gm.get_messages(groupID, limit=25) return render_template("group.html", group=group, messages=messages, members=members)
def __init__(self, chat=False, group=False, name=None, filt=None, last=0): self.groupme = GroupMe() self.chatid = None self.groupid = None self.last_mess_id = last self.filt = filt if chat: self.chatid = self.groupme.get_chat_id(name) elif group: self.groupid = self.groupme.get_group_id(name)
def update_user_nodes(self, groupme: GroupMe): groups = groupme.get_groups_index() members = None for group in groups: if int(group["id"]) == groupme.get_group(): members = group["members"] for member in members: properties = "{" for key, value in member.items(): if value is not None: properties += f"{key}: {repr(value)}, " properties = properties[:-2] + "}" query = f"merge (u:User {properties})" self.graph.run(cypher=query)
class MessageIterator: def __init__(self, chat=False, group=False, name=None, filt=None, last=0): self.groupme = GroupMe() self.chatid = None self.groupid = None self.last_mess_id = last self.filt = filt if chat: self.chatid = self.groupme.get_chat_id(name) elif group: self.groupid = self.groupme.get_group_id(name) def next(self): if self.chatid: page = self.groupme.get_1page_messages(chatid=self.chatid, before=self.last_mess_id, chat=True) if page is not None and len(page) > 0: self.last_mess_id = page[-1]['id'] if self.filt: page = self.filt(page) return page else: return page elif self.groupid: page = self.groupme.get_1page_messages(groupid=self.groupid, before=self.last_mess_id, group=True) if page is not None and len(page) > 0: self.last_mess_id = page[-1]['id'] if self.filt: page = self.filt(page) return page else: return page def has_next(self): if self.chatid: page = self.groupme.get_1page_messages(chatid=self.chatid, before=self.last_mess_id, chat=True) if page is not None and len(page) > 0: return True else: return False if self.groupid: page = self.groupme.get_1page_messages(groupid=self.groupid, before=self.last_mess_id, group=True) if page is not None and len(page) > 0: return True else: return False
from message_iterator import MessageIterator from groupme import GroupMe GM_INSTANCE = GroupMe() def group_rank_num_posts(name, filt=None, filtstr=None): """ leaderboard of total messages sent per user in group chat """ it = MessageIterator(name=name, group=True, filt=filt) members = GM_INSTANCE.get_group_members(name=name) scoreboard = {} for member in members: scoreboard[member['user_id']] = 0 page = it.next() while (page != None): for message in page: if 'sender_id' in message and message[ 'sender_id'] != "system" and message[ 'sender_id'] in scoreboard: scoreboard[message['sender_id']] += 1 page = it.next() score_format = [] for user_id in scoreboard: for member in members: if user_id == member['user_id']: score_format += [(scoreboard[user_id], member['name'])] break
def index(): gm = GroupMe(api_token=os.environ["GROUPME_ACCESS_TOKEN"]) groups = gm.get_groups_index() return render_template("index.html", groups=groups)
from groupme import GroupMe api = GroupMe() api.handshake() api.auth() api.user_details() api.user_subscribe() api.connect()
def main(): # create a GroupMe API wrapper instance g = GroupMe() usage = "usage: interact with GroupMe API -- list chats/DMs/messages, send messages, " + \ "filter messages by text/sender/date" # set up command line options parser = OptionParser(usage) parser.add_option("--get_dms", action="store", dest="get_dms", default=None, help="List all user Direct Messages i.e. --get_groups=True") parser.add_option("--get_groups", action="store", dest="get_groups", default=None, help="List all user group chats i.e. --get_groups=True") parser.add_option("--get_group_id", action="store", dest="get_group_id", default=None, help="Get internal id for group chat e.g. --get_group_id='Football Chat'") parser.add_option("--get_chat_id", action="store", dest="get_chat_id", default=None, help="Get internal id for direct message e.g. --get_chat_id='Rob'") parser.add_option("--get_group_members", action="store", dest="get_group_members", default=None, help="List members of chosen group message e.g. --get_group_members='Football Chat'") parser.add_option("--get_chat_messages", action="store", dest="get_chat_messages", default=None, help="Get all messages from a direct messages e.g. --get_chat_messages='Rob'") parser.add_option("--get_group_messages", action="store", dest="get_group_messages", default=None, help="Get all messages from a group message e.g. --get_group_messages='Football Chat'") parser.add_option("--filter_text", action="store", dest="filter_text", default=None, help="Filter messages by regex text e.g. --filter_text='^Hello!$'") parser.add_option("--filter_user", action="store", dest="filter_user", default=None, help="Filter messages by user who sent e.g. --filter_user='******'") parser.add_option("--filter_dateOn", action="store", dest="filter_dateOn", default=None, help=DATEON_HELP) parser.add_option("--filter_dateAfter", action="store", dest="filter_dateAfter", default=None, help=DATEAFTER_HELP) parser.add_option("--filter_dateBefore", action="store", dest="filter_dateBefore", default=None, help=DATEBEFORE_HELP) parser.add_option("--count", action="store", dest="count", default=None, help="return number of messages fitting criteria i.e. --count=True") parser.add_option("--send_message", action="store", dest="send_message", default=None, help=SEND_HELP) parser.add_option("--group_name", action="store", dest="group_name", default=None, help="Specify group to send message to e.g. --group_name='Football Chat'") parser.add_option("--chat_name", action="store", dest="chat_name", default=None, help="Specify user to send direct message to e.g. --chat_name='Rob'") # group_stats stuff parser.add_option("--group_rank_num_posts", action="store", dest="group_rank_num_posts", default=None, help="Get leaderboard of total messages sent per user in group chat e.g." + \ " --group_rank_num_posts='Football Chat'") parser.add_option("--group_rank_num_likes", action="store", dest="group_rank_num_likes", default=None, help="Get leaderboard of total likes received per user in group chat e.g. " + \ " --group_rank_num_likes='Football Chat'") parser.add_option("--group_rank_num_liked", action="store", dest="group_rank_num_liked", default=None, help="Get leaderboard of total likes given per user in group chat e.g. " + \ " --group_rank_num_liked='Football Chat'") parser.add_option("--group_rank_len_posts", action="store", dest="group_rank_len_posts", default=None, help="Tally total number of characters each user has sent in group and avg characters/post " + \ "e.g. --group_rank_len_posts='Football Chat'") parser.add_option("--group_most_liked_post", action="store", dest="group_most_liked_post", default=None, help="Return the message(s) with the most likes in a group chat and its like count " + \ "e.g. --group_most_liked_post='Football Chat'") parser.add_option("--orphaned_users", action="store", dest="orphaned_users", default=None, help="Find users that have left a group and list their usernames/GroupMe ID #s " + \ "e.g. --orphaned_users='Football Chat'") # TODO: add orphaned_users option when finished with that method # grab input from command line (options, _) = parser.parse_args() # carry out specified action if options.get_dms: dms = g.get_chats() print ("\nUser direct messages:") for dm in dms: print (" " + dm['other_user']['name']) elif options.get_groups: dms = g.get_groups() print ("\nUser group messages:") for dm in dms: print (" " + dm['name']) elif options.get_group_id: gid = g.get_group_id(options.get_group_id) if gid: print (f"\nGroup ID for group name \"{options.get_group_id}\": {gid}") else: print (f"\nNo group by that name! (name: \"{options.get_group_id}\")") elif options.get_chat_id: cid = g.get_chat_id(options.get_chat_id) if cid: print (f"\nGroup ID for chat with user \"{options.get_chat_id}\": {cid}") else: print (f"\nNo direct message with that user! (name: \"{options.get_chat_id}\")") elif options.get_group_members: members = g.get_group_members(name=options.get_group_members, groupid=options.get_group_members) if members: print (f"\nGroup members for group \"{options.get_group_members}\":") for member in members: print (f" Name: \"{member['name']}\" | Nickname: \"{member['nickname']}\"") else: print (f"\nNo group by that name! (name: \"{options.get_group_members}\")") elif options.get_chat_messages: filters = { "text": None, "user": None, "dateOn": None, "dateAfter": None, "dateBefore": None, } if options.filter_text: filters["text"] = rf"{options.filter_text}" if options.filter_dateOn: filters["dateOn"] = parse_input_date(options.filter_dateOn) else: if options.filter_dateBefore: filters["dateBefore"] = parse_input_date(options.filter_dateBefore) if options.filter_dateAfter: filters["dateAfter"] = parse_input_date(options.filter_dateAfter) if options.filter_user: filters["user"] = options.filter_user user_filter = g.filter_lambda(text=filters["text"], user=filters["user"], dateOn=filters["dateOn"], dateAfter=filters["dateAfter"], dateBefore=filters["dateBefore"]) try: messages = g.get_all_messages(name=options.get_chat_messages, chat=True, filt=user_filter) if not messages: print ("No messages match filter") return if options.count: print (f"\n# messages matching filter: {len(messages)}") else: print ("") if not messages: print ("No messages match filter") return for message in messages: message_meta = f"Sender: {message['name']} | " + \ f"Date: {g.epoch_to_datetime(message['created_at']).date()}" print (message_meta) print (f" Text: {message['text']}\n") except BadNameException: print (f"\nNo direct message with that user! (name: \"{options.get_chat_messages}\")") elif options.get_group_messages: filters = { "text": None, "user": None, "dateOn": None, "dateAfter": None, "dateBefore": None, } if options.filter_text: filters["text"] = rf"{options.filter_text}" if options.filter_dateOn: filters["dateOn"] = parse_input_date(options.filter_dateOn) else: if options.filter_dateBefore: filters["dateBefore"] = parse_input_date(options.filter_dateBefore) if options.filter_dateAfter: filters["dateAfter"] = parse_input_date(options.filter_dateAfter) if options.filter_user: filters["user"] = options.filter_user user_filter = g.filter_lambda(text=filters["text"], user=filters["user"], dateOn=filters["dateOn"], dateAfter=filters["dateAfter"], dateBefore=filters["dateBefore"]) try: messages = g.get_all_messages(name=options.get_group_messages, group=True, filt=user_filter) if not messages: print ("No messages match filter") return groupid = messages[0]['group_id'] members = g.get_group_members(groupid=groupid) if options.count: print (f"\n# messages matching filter: {len(messages)}") else: print ("") for message in messages: for member in members: if member['user_id'] == message['sender_id']: sender = member sender_name = sender['nickname'] sender_nickname = sender['name'] message_meta = f"Sender: {sender_nickname} ({sender_name}) | " + \ f"Date: {g.epoch_to_datetime(message['created_at']).date()}" print (message_meta) print (f" Text: {message['text']}\n") except BadNameException: print (f"\nNo group message with that name! (name: \"{options.get_group_messages}\")") elif options.send_message: if options.group_name: sent = g.send_message(options.send_message, name=options.group_name, group=True) print (sent) elif options.chat_name: sent = g.send_message(options.send_message, name=options.chat_name, chat=True) print (sent) else: print ("Provide a --chat_name or --group_name! Try --help") # group_stats stuff elif options.group_rank_num_posts: print (group_rank_num_posts(options.group_rank_num_posts)) elif options.group_rank_num_likes: print (group_rank_num_likes(options.group_rank_num_likes)) elif options.group_rank_num_liked: print (group_rank_num_liked(options.group_rank_num_liked)) elif options.group_rank_len_posts: print (group_rank_len_posts(options.group_rank_len_posts)) elif options.group_most_liked_post: print (group_most_liked_post(options.group_most_liked_post)) elif options.orphaned_users: print (orphaned_users(options.orphaned_users)) else: # no input print ("Provide an action! Try --help")