# Buggy code
def _send_telegram_msg(self, title, msg, user_id=None, api_key=None):
    """
    Sends a Telegram notification

    :param title: The title of the notification to send
    :param msg: The message string to send
    :param id: The Telegram user/group id to send the message to
    :param api_key: Your Telegram bot API token

    :returns: True if the message succeeded, False otherwise
    """
    user_id = app.TELEGRAM_ID if user_id is None else user_id
    api_key = app.TELEGRAM_APIKEY if api_key is None else api_key

    log.debug("Telegram in use with API KEY: {0}", api_key)

    message = "%s : %s" % (title.encode(), msg.encode())
    payload = urlencode({"chat_id": user_id, "text": message})
    telegram_api = "https://api.telegram.org/bot%s/%s"

    req = Request(telegram_api % (api_key, "sendMessage"), payload)

    success = False
    try:
        urlopen(req)
        message = "Telegram message sent successfully."
        success = True
    except IOError as e:
        message = "Unknown IO error: %s" % e
        if hasattr(e, b"code"):
            error_message = {
                400: "Missing parameter(s). Double check your settings or if the channel/user exists.",
                401: "Authentication failed.",
                420: "Too many messages.",
                500: "Server error. Please retry in a few moments.",
            }
            if e.code in error_message:
                message = error_message.get(e.code)
            else:
                http_status_code.get(e.code, message)
    except Exception as e:
        message = "Error while sending Telegram message: %s " % e
    finally:
        log.info(message)
    return success, message


# Traceback (UnicodeEncodeError)
2018-07-11 00:55:37 DEBUG    TORNADO :: [7e4da69] 296762: Formatting pattern: %SN - %Sx03 - %EN - %QN -> Westworld - 2x03 - Virtù e Fortuna - 1080p WEB-DL
2018-07-11 00:55:37 DEBUG    TORNADO :: [7e4da69] Notification for NMJ scan update not enabled, skipping this notification
2018-07-11 00:55:37 DEBUG    TORNADO :: [7e4da69] Sending a Telegram message for Westworld - 2x03 - Virtù e Fortuna - 1080p WEB-DL
2018-07-11 00:55:37 DEBUG    TORNADO :: [7e4da69] Telegram in use with API KEY: **********
2018-07-11 00:55:37 ERROR    TORNADO :: [7e4da69] API :: ''
Traceback (most recent call last):
File "C:\Medusa\medusa\server\api\v1\core.py", line 141, in get
out_dict = _call_dispatcher(args, kwargs)
File "C:\Medusa\medusa\server\api\v1\core.py", line 211, in call_dispatcher
cur_out_dict = func(cur_args, cur_kwargs).run()  # call function and get response
File "C:\Medusa\medusa\server\api\v1\core.py", line 1309, in run
failed=self.failed, proc_type=self.type
File "C:\Medusa\medusa\process_tv.py", line 145, in process
ignore_subs=ignore_subs)
File "C:\Medusa\medusa\process_tv.py", line 333, in process_files
self.process_media(path, self.video_files, force, is_priority, ignore_subs)
File "C:\Medusa\medusa\process_tv.py", line 554, in process_media
self.result = processor.process()
File "C:\Medusa\medusa\post_processor.py", line 1279, in process
notifiers.notify_download(ep_obj._format_pattern('%SN - %Sx%0E - %EN - %QN'))
File "C:\Medusa\medusa\notifiers\__init__.py", line 92, in notify_download
n.notify_download(ep_name)
File "C:\Medusa\medusa\notifiers\telegram.py", line 111, in notify_download
self._notify_telegram(title, ep_name)
File "C:\Medusa\medusa\notifiers\telegram.py", line 165, in _notify_telegram
return self._send_telegram_msg(title, message, user_id, api_key)
File "C:\Medusa\medusa\notifiers\telegram.py", line 62, in _send_telegram_msg
message = '%s : %s' % (title.encode(), msg.encode())
File "C:\Python27\lib\encodings\cp1255.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character u'\xf9' in position 23: character maps to <undefined>

# Fixed code
def _send_telegram_msg(self, title, msg, user_id=None, api_key=None):
    """
    Sends a Telegram notification

    :param title: The title of the notification to send
    :param msg: The message string to send
    :param id: The Telegram user/group id to send the message to
    :param api_key: Your Telegram bot API token

    :returns: True if the message succeeded, False otherwise
    """
    user_id = app.TELEGRAM_ID if user_id is None else user_id
    api_key = app.TELEGRAM_APIKEY if api_key is None else api_key

    log.debug("Telegram in use with API KEY: {0}", api_key)

    message = "{0} : {1}".format(title, msg).encode("utf-8")
    payload = urlencode({"chat_id": user_id, "text": message})
    telegram_api = "https://api.telegram.org/bot%s/%s"

    req = Request(telegram_api % (api_key, "sendMessage"), payload)

    success = False
    try:
        urlopen(req)
        message = "Telegram message sent successfully."
        success = True
    except IOError as e:
        message = "Unknown IO error: %s" % e
        if hasattr(e, b"code"):
            error_message = {
                400: "Missing parameter(s). Double check your settings or if the channel/user exists.",
                401: "Authentication failed.",
                420: "Too many messages.",
                500: "Server error. Please retry in a few moments.",
            }
            if e.code in error_message:
                message = error_message.get(e.code)
            else:
                http_status_code.get(e.code, message)
    except Exception as e:
        message = "Error while sending Telegram message: %s " % e
    finally:
        log.info(message)
    return success, message