Created
February 20, 2020 20:33
-
-
Save mkirlin/0c98392fee572fac3546796387c52898 to your computer and use it in GitHub Desktop.
An attempt at sending a message to a GroupMe group that includes @mentions for users.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _send_message(bot, message_to_send): | |
attachments = _format_attachments(bot, message_to_send) | |
print(attachments) | |
response = requests.post( | |
f"{GROUPME_URL}/bots/post", | |
params={"token": GROUPME_TOKEN}, | |
data = { | |
"bot_id": bot.get("bot_id"), | |
"text": message_to_send, | |
"attachments": attachments | |
} | |
) | |
def _format_attachments(bot, message_to_send): | |
if "@" in message_to_send: | |
group_members = _get_group_members(bot.get('group_name')) | |
user_id_list = [] | |
loci_list = [] | |
for instance in re.finditer("@\w+\s\w+", message_to_send): | |
mention_found = instance.group(0) | |
member_to_mention = [ | |
member for member in group_members | |
if mention_found.replace('@', '') == member.get('nickname') | |
] | |
if member_to_mention: | |
first_name = mention_found.split(' ')[0] | |
print(first_name) | |
member_to_mention = member_to_mention[0] | |
user_id_list.append(member_to_mention.get('user_id')) | |
loci_list.append([instance.start(), len(first_name)]) | |
attachments = [ | |
{ | |
"type": "mentions", | |
"user_ids": user_id_list, | |
"loci": loci_list | |
} | |
] | |
print(attachments) | |
return attachments | |
return [] | |
def _get_group_members(group_name): | |
response = requests.get( | |
f"{GROUPME_URL}/groups", | |
params={ | |
"token": GROUPME_TOKEN | |
} | |
) | |
response = response.json().get('response') | |
group = [ | |
group for group in response | |
if group.get('name') == group_name | |
][0] | |
members = group.get('members') | |
print(members) | |
return members |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment