This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhangoutsbot.py
201 lines (179 loc) · 9.16 KB
/
hangoutsbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env python3
import logging
import asyncio
import re
import sys
import hangups
import settings
from models.user import User
from models.conversation import Conversation
from models.message import Message
from models.command import Command
from models.hook import Hook
from utils.commands import register_commands
from utils.hooks import register_hooks
from utils.enums import EventType, ConversationType
from utils.textutils import spacing
from datetime import datetime
logger = logging.getLogger(__name__)
logging.basicConfig(format="%(name)s - %(levelname)s: %(asctime)s | %(message)s")
if settings.DEBUG:
logger.setLevel(logging.DEBUG)
class HangoutsBot(object):
def __init__(self):
self.command_matcher = re.compile(settings.COMMAND_MATCH_REGEX)
register_commands()
register_hooks()
self.client = hangups.client.Client(self.login())
self.user = User.get_or_create(id=settings.BOT_ID, defaults={'first_name': settings.BOT_FIRST_NAME, 'last_name': settings.BOT_LAST_NAME})[0]
self.hooks = Hook.select()
def login(self):
return hangups.auth.get_auth_stdin(settings.COOKIES_FILE_PATH)
def run(self):
logger.debug(self.client)
self.client.on_state_update.add_observer(self.handle_update)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(self.client.connect())
except KeyboardInterrupt:
sys.exit(0)
@asyncio.coroutine
def handle_update(self, state_update):
logger.debug("Handling event update")
if state_update.event_notification.event.event_type == EventType.EVENT_TYPE_REGULAR_CHAT_MESSAGE.value:
yield from self.handle_message(state_update)
elif state_update.event_notification.event.event_type == EventType.EVENT_TYPE_CONVERSATION_RENAME.value:
yield from self.handle_rename(state_update)
elif state_update.event_notification.event.event_type == EventType.EVENT_TYPE_REMOVE_USER.value:
yield from self.handle_user_removed(state_update)
elif state_update.event_notification.event.event_type == EventType.EVENT_TYPE_ADD_USER.value:
yield from self.handle_user_added(state_update)
else:
pass
@asyncio.coroutine
def handle_message(self, state_update):
logger.debug("Handling message")
conversation = self.get_or_create_conversation(state_update.conversation)
if state_update.event_notification.event.sender_id.gaia_id == settings.BOT_ID:
message = Message.create(conversation=conversation, user=self.user, text="".join(
[seg.text for seg in state_update.event_notification.event.chat_message.message_content.segment]), time=datetime.now())
message.conversation.logger.info(message.text.replace("\n", " "), extra={
"username": message.user.username,
"message_time": datetime.strftime(message.time, "%Y-%m-%d %X"),
})
return True
sending_user = User.get(User.id == state_update.event_notification.event.sender_id.gaia_id)
message_body = ""
for seg in state_update.event_notification.event.chat_message.message_content.segment:
message_body += seg.text
message = Message.create(conversation=conversation, user=sending_user, text=message_body, time=datetime.now())
message.conversation.logger.info(message.text.replace("\n", " "), extra={
"username": message.user.username,
"message_time": datetime.strftime(message.time, "%Y-%m-%d %X"),
})
matched = self.command_matcher.match(message.text)
if matched:
try:
cmd_to_run = Command.get(name=matched.group(1).lower())
yield from cmd_to_run.run(bot=self,
conversation=message.conversation,
user=message.user,
args=message.text.split(matched.group(1), 1)[-1].split())
except Command.DoesNotExist:
pass
for hook in self.hooks:
matcher = re.compile(r"{}".format(hook.regex))
if matcher.match(message.text):
yield from hook.run(bot=self, conversation=message.conversation, user=message.user, text=message.text)
return True
@asyncio.coroutine
def handle_rename(self, state_update):
conversation = self.get_or_create_conversation(state_update.conversation)
try:
user = User.get(id=state_update.event_notification.event.sender_id.gaia_id)
except User.DoesNotExist:
user = self.create_user_from_id(state_update.event_notification.event.sender_id.gaia_id, state_update.conversation)
new_name = state_update.event_notification.event.conversation_rename.new_name
conversation.logger.info("{} changed topic to {}".format(user.username, new_name), extra={
'username': "****",
"message_time": datetime.strftime(datetime.now(), "%Y-%m-%d %X")
})
return True
@asyncio.coroutine
def handle_user_removed(self, state_update):
conversation = self.get_or_create_conversation(state_update.conversation)
removing_user = User.get(id=state_update.event_notification.event.sender_id.gaia_id)
removed_user = User.get(id=state_update.event_notification.event.membership_change.participant_ids[0].gaia_id)
conversation.members.remove(removed_user)
conversation.logger.info("{} was kicked by {}".format(removed_user.username, removing_user.username), extra={
"username": "****",
"message_time": datetime.strftime(datetime.now(), "%Y-%m-%d %X")
})
return True
@asyncio.coroutine
def handle_user_added(self, state_update):
conversation = self.get_or_create_conversation(state_update.conversation)
self.check_conversation_participants(state_update.conversation)
adding_user = User.get(id=state_update.event_notification.event.sender_id.gaia_id)
added_user = User.get(id=state_update.event_notification.event.membership_change.participant_ids[0].gaia_id)
conversation.logger.info("{} was added by {}".format(added_user.username, adding_user.username), extra={
"username": "****",
"message_time": datetime.strftime(datetime.now(), "%Y-%m-%d %X")
})
def create_user_from_id(self, user_id, conversation):
logger.debug("Creating User with id {}".format(user_id))
participant_object = None
for participant in conversation.participant_data:
if participant.id.gaia_id == user_id:
participant_object = participant
break
name_split = participant_object.fallback_name.split(" ", 1)
if len(name_split) > 1:
user = User.create(id=user_id, first_name=name_split[0], last_name=name_split[1])
else:
user = User.create(id=user_id, first_name=name_split[0], last_name="")
return user
def get_or_create_conversation(self, conversation):
return Conversation.get_or_create(id=conversation.conversation_id.id, defaults={
'group': conversation.type == ConversationType.CONVERSATION_TYPE_GROUP.value
})[0]
def check_conversation_participants(self, conversation):
logger.debug("Checking Conversation participants")
conv = self.get_or_create_conversation(conversation)
for participant in conversation.participant_data:
try:
user = User.get(User.id == participant.id.gaia_id)
except User.DoesNotExist:
user = self.create_user_from_id(participant.id.gaia_id, conversation)
if user not in conv.members:
logger.debug("Adding User {} to Conversation {}".format(user.id, conv.id))
conv.members.add(user)
return True
@asyncio.coroutine
def send_message(self, conversation, message, image_file=None, filter_to_use=None):
if filter_to_use == "spacing":
message = spacing(message)
if image_file:
image_id = yield from self.client.upload_image(image_file)
else:
image_id = None
request = hangups.hangouts_pb2.SendChatMessageRequest(
request_header=self.client.get_request_header(),
event_request_header=hangups.hangouts_pb2.EventRequestHeader(
conversation_id=hangups.hangouts_pb2.ConversationId(
id=conversation.id
),
client_generated_id=self.client.get_client_generated_id(),
),
message_content=hangups.hangouts_pb2.MessageContent(
segment=[seg.serialize() for seg in hangups.ChatMessageSegment.from_str(message)],
),
)
if image_id:
request.existing_media.photo.photo_id = image_id
try:
yield from self.client.send_chat_message(request)
except hangups.exceptions.NetworkError:
logger.error("Unable to send message to {} with text '{}'".format(conversation, message))
if __name__ == "__main__":
print("Run the bot using the manage.py file: ./manage.py run")