diff --git a/disco/api/client.py b/disco/api/client.py index 7470c02..81a69ee 100644 --- a/disco/api/client.py +++ b/disco/api/client.py @@ -13,3 +13,13 @@ class APIClient(LoggingClass): def gateway(self, version, encoding): r = self.http(Routes.GATEWAY_GET) return r['url'] + '?v={}&encoding={}'.format(version, encoding) + + def channels_messages_send(self, channel, content, nonce=None, tts=False): + r = self.http(Routes.CHANNELS_MESSAGES_POST, channel, json={ + 'content': content, + 'nonce': nonce, + 'tts': tts, + }) + + # TODO: return correct types + return r diff --git a/disco/api/http.py b/disco/api/http.py index f7eff59..1ab05d2 100644 --- a/disco/api/http.py +++ b/disco/api/http.py @@ -17,6 +17,8 @@ class Routes(object): GATEWAY_GET = (HTTPMethod.GET, '/gateway') + CHANNELS_MESSAGES_POST = (HTTPMethod.POST, '/channels/{}/messages') + class APIException(Exception): def __init__(self, obj): @@ -37,11 +39,15 @@ class HTTPClient(object): def __call__(self, route, *args, **kwargs): method, url = route - r = requests.request(str(method), self.BASE_URL + url, *args, **kwargs) + kwargs['headers'] = self.headers + + r = requests.request(str(method), (self.BASE_URL + url).format(*args), **kwargs) try: r.raise_for_status() except: + print r.json() + raise # TODO: rate limits # TODO: check json raise APIException(r.json()) diff --git a/disco/types/message.py b/disco/types/message.py index 27e89b8..818fbc7 100644 --- a/disco/types/message.py +++ b/disco/types/message.py @@ -53,7 +53,6 @@ class Message(BaseType): @cached_property def channel(self): - print self.client.state.channels return self.client.state.channels.get(self.channel_id) @cached_property @@ -64,6 +63,9 @@ class Message(BaseType): def mention_users_dict(self): return {i.id: i for i in self.mentions} + def reply(self, *args, **kwargs): + return self.client.api.channels_messages_send(self.channel_id, *args, **kwargs) + def is_mentioned(self, entity): if isinstance(entity, User): return entity.id in self.mention_users diff --git a/examples/basic_plugin.py b/examples/basic_plugin.py index 6a05979..d88ad7c 100644 --- a/examples/basic_plugin.py +++ b/examples/basic_plugin.py @@ -10,6 +10,7 @@ class BasicPlugin(Plugin): @Plugin.command('test') def on_test_command(self, event): + event.msg.reply('HELLO WORLD') print 'wtf' if __name__ == '__main__':