Completed
Push — master ( 72b331...d4b7d2 )
by Steffen
02:14
created

Telegram.send_message()   B

Complexity

Conditions 6

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 34
rs 8.5666
c 0
b 0
f 0
cc 6
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
4
import requests
5
6
from kuon.api_response import APIResponse
7
from kuon.watcher.settings import Settings
8
9
10
class InvalidApiSettingsException(Exception):
11
    pass
12
13
14
class Telegram:
15
    """
16
    Class to notify the user via telegram bot.
17
    The telegram API documentation can be found here: https://core.telegram.org/bots/api
18
    """
19
20
    def __init__(self):
21
        """Initializing function"""
22
        if not Settings.Notification.Telegram.token:
23
            raise InvalidApiSettingsException('You need an API token to use the Telegram API')
24
25
        self._token = Settings.Notification.Telegram.token
26
        self.api_url = "https://api.telegram.org/bot{0:s}".format(self._token)
27
28
        bot_profile = self.get_me()
29
        if not bot_profile.ok:
30
            raise InvalidApiSettingsException('The provided API key is invalid')
31
32
        self._chat_id = Settings.Notification.Telegram.chat_id
33
34
    def get_me(self):
35
        """getMe implementation
36
        https://core.telegram.org/bots/api#getme
37
38
        :return:
39
        """
40
        api_url = "{0:s}/getMe".format(self.api_url)
41
        link = requests.get(api_url)
42
        return APIResponse(link.text)
43
44
    def get_updates(self, offset=None, limit=None, timeout=None, allowed_updates=None):
45
        """getUpdates implementation
46
        https://core.telegram.org/bots/api#getupdates
47
48
        :param offset:
49
        :param limit:
50
        :param timeout:
51
        :param allowed_updates:
52
        :return:
53
        """
54
        api_url = "{0:s}/getUpdates".format(self.api_url)
55
56
        payload = {}
57
58
        if offset:
59
            payload['offset'] = offset
60
        if limit:
61
            payload['limit'] = limit
62
        if timeout:
63
            payload['timeout'] = timeout
64
        if allowed_updates:
65
            payload['allowed_updates'] = allowed_updates
66
67
        link = requests.get(api_url)
68
        return APIResponse(link.text)
69
70
    def send_message(self, text, chat_id: int, parse_mode=None, disable_web_page_preview=None,
71
                     disable_notification=False, reply_to_message_id=None, reply_markup=None):
72
        """sendMessage implementation
73
        https://core.telegram.org/bots/api#sendmessage
74
75
        :param text:
76
        :param chat_id:
77
        :param parse_mode:
78
        :param disable_web_page_preview:
79
        :param disable_notification:
80
        :param reply_to_message_id:
81
        :param reply_markup:
82
        :return:
83
        """
84
        api_url = "{0:s}/sendMessage".format(self.api_url)
85
86
        payload = {
87
            'text': text,
88
            'chat_id': chat_id
89
        }
90
91
        if parse_mode:
92
            payload['parse_mode'] = parse_mode
93
        if disable_web_page_preview:
94
            payload['disable_web_page_preview'] = disable_web_page_preview
95
        if disable_notification:
96
            payload['disable_notification'] = disable_notification
97
        if reply_to_message_id:
98
            payload['reply_to_message_id'] = reply_to_message_id
99
        if reply_markup:
100
            payload['reply_markup'] = reply_markup
101
102
        link = requests.get(api_url, params=payload)
103
        return APIResponse(link.text)
104
105
    def get_last_chat_id_and_text(self):
106
        """Retrieve the last message and chat id
107
108
        :return:
109
        """
110
        updates = self.get_updates()
111
        if not updates.result:
112
            # Bot didn't receive any new messages
113
            return None, ""
114
115
        last_update = len(updates.result) - 1
116
        text = updates.result[last_update].message.text
117
        chat_id = updates.result[last_update].message.chat.id
118
        return chat_id, text
119