# Path: stampy/stampy.py
# def sendmessage(chat_id=0, text="", reply_to_message_id=False,
#                 disable_web_page_preview=True, parse_mode=False,
#                 extra=False):
#     """
#     Sends a message to a chat
#     :param chat_id: chat_id to receive the message
#     :param text: message text
#     :param reply_to_message_id: message_id to reply
#     :param disable_web_page_preview: do not expand links to include preview
#     :param parse_mode: use specific format (markdown, html)
#     :param extra: extra parameters to send
#                  (for future functions like keyboard_markup)
#     :return:
#     """
# 
#     logger = logging.getLogger(__name__)
#     url = "%s%s/sendMessage" % (plugin.config.config(key="url"),
#                                 plugin.config.config(key='token'))
#     lines = text.split("\n")
#     maxlines = 15
#     if len(lines) > maxlines:
#         # message might be too big for single message (max 4K)
#         if "```" in text:
#             markdown = True
#         else:
#             markdown = False
# 
#         texto = string.join(lines[0:maxlines], "\n")
#         if markdown:
#             texto = "%s```" % texto
# 
#         # Send first batch
#         sendmessage(chat_id=chat_id, text=texto,
#                     reply_to_message_id=reply_to_message_id,
#                     disable_web_page_preview=disable_web_page_preview,
#                     parse_mode=parse_mode, extra=extra)
#         # Send remaining batch
#         texto = string.join(lines[maxlines:], "\n")
#         if markdown:
#             texto = "```%s" % texto
#         sendmessage(chat_id=chat_id, text=texto, reply_to_message_id=False,
#                     disable_web_page_preview=disable_web_page_preview,
#                     parse_mode=parse_mode, extra=extra)
#         return
# 
#     overridegid = plugin.config.config(key='overridegid', gid=0,
#                                        default=False)
#     if overridegid:
#         chat_id = overridegid
# 
#     message = "%s?chat_id=%s&text=%s" % (
#               url, chat_id, urllib.quote_plus(text.encode('utf-8')))
#     if reply_to_message_id:
#         message += "&reply_to_message_id=%s" % reply_to_message_id
#     if disable_web_page_preview:
#         message += "&disable_web_page_preview=1"
#     else:
#         message += "&disable_web_page_preview=0"
#     if parse_mode:
#         message += "&parse_mode=%s" % parse_mode
#     if extra:
#         message += "&%s" % extra
# 
#     code = False
#     attempt = 0
#     while not code:
#         # It this is executed as per unit testing, skip sending message
#         UTdisable = not plugin.config.config(key='unittest', default=False)
#         Silent = not plugin.config.gconfig(key='silent', default=False, gid=geteffectivegid(gid=chat_id))
#         if UTdisable and Silent:
#             result = json.load(urllib.urlopen(message))
#             code = result['ok']
#         else:
#             code = True
#             result = ""
# 
#         if attempt > 0:
#             logger.error(msg=_L("ERROR (%s) sending message: Code: %s : Text: %s") % (attempt, code, result))
# 
#         attempt += 1
#         sleep(1)
#         if not code:
#             error = result['description']
#             if 'entity starting at byte offset' in error:
#                 # Trim the byte offset to make this error more generic
#                 error = error[:103]
# 
#             for case in Switch(error):
#                 if case(u"Bad Request: message text is empty"):
#                     # Message is empty, no need to resend
#                     attempt = 61
#                     break
# 
#                 if case(u"Forbidden: bot can't initiate conversation with a user"):
#                     # Bot hasn't been authorized by user, cancelling
#                     attempt = 61
#                     break
# 
#                 if case(u"Bad Request: reply message not found"):
#                     # Original reply has been deleted
#                     attempt = 61
#                     break
# 
#                 if case(u"Forbidden: bot was blocked by the user"):
#                     # User blocked the bot
#                     attempt = 61
#                     break
# 
#                 if case(u"Bad Request: can't parse entities in message text: Can't find end of the entity starting at byte offset"):
#                     attempt = 61
#                     break
# 
#                 if case(u'Forbidden: bot was kicked from the supergroup chat'):
#                     attempt = 61
#                     break
# 
#                 if case(u'Bad Request: chat not found'):
#                     attempt = 61
#                     break
# 
#                 if case(u'Bad Request: reply message not found'):
#                     attempt = 61
#                     break
# 
#         # exit after 60 retries with 1 second delay each
#         if attempt > 60:
#             logger.error(msg=_L("PERM ERROR sending message: Code: %s : Text: %s") % (code, result))
#             code = True
# 
#     try:
#         sent = {"message": result['result']}
#     except:
#         sent = False
# 
#     if sent:
#         # Check if there's something to forward and do it
#         plugin.forward.forwardmessage(sent)
# 
#     logger.debug(msg=_L("Sending message: Code: %s : Text: %s") % (code, text))
#     return code


import argparse
from stampy.stampy import sendmessage
#!/usr/bin/env python
# encoding: utf-8
#
# Description: Sends a message to a chatID via CLI
# Author: Pablo Iranzo Gomez (Pablo.Iranzo@gmail.com)




description = 'Sends a message using bot to chatid'

# Option parsing
p = argparse.ArgumentParser("sendmessage.py [arguments]", description=description)
p.add_argument("-i", "--id", dest="chat_id", help="chat_id to send message to", default=False)
p.add_argument("-t", "--text", dest="texto", help="Text to send", default=False)


# Path: stampy/stampy.py
# def sendmessage(chat_id=0, text="", reply_to_message_id=False,
#                 disable_web_page_preview=True, parse_mode=False,
#                 extra=False):
#     """
#     Sends a message to a chat
#     :param chat_id: chat_id to receive the message
#     :param text: message text
#     :param reply_to_message_id: message_id to reply
#     :param disable_web_page_preview: do not expand links to include preview
#     :param parse_mode: use specific format (markdown, html)
#     :param extra: extra parameters to send
#                  (for future functions like keyboard_markup)
#     :return:
#     """
# 
#     logger = logging.getLogger(__name__)
#     url = "%s%s/sendMessage" % (plugin.config.config(key="url"),
#                                 plugin.config.config(key='token'))
#     lines = text.split("\n")
#     maxlines = 15
#     if len(lines) > maxlines:
#         # message might be too big for single message (max 4K)
#         if "```" in text:
#             markdown = True
#         else:
#             markdown = False
# 
#         texto = string.join(lines[0:maxlines], "\n")
#         if markdown:
#             texto = "%s```" % texto
# 
#         # Send first batch
#         sendmessage(chat_id=chat_id, text=texto,
#                     reply_to_message_id=reply_to_message_id,
#                     disable_web_page_preview=disable_web_page_preview,
#                     parse_mode=parse_mode, extra=extra)
#         # Send remaining batch
#         texto = string.join(lines[maxlines:], "\n")
#         if markdown:
#             texto = "```%s" % texto
#         sendmessage(chat_id=chat_id, text=texto, reply_to_message_id=False,
#                     disable_web_page_preview=disable_web_page_preview,
#                     parse_mode=parse_mode, extra=extra)
#         return
# 
#     overridegid = plugin.config.config(key='overridegid', gid=0,
#                                        default=False)
#     if overridegid:
#         chat_id = overridegid
# 
#     message = "%s?chat_id=%s&text=%s" % (
#               url, chat_id, urllib.quote_plus(text.encode('utf-8')))
#     if reply_to_message_id:
#         message += "&reply_to_message_id=%s" % reply_to_message_id
#     if disable_web_page_preview:
#         message += "&disable_web_page_preview=1"
#     else:
#         message += "&disable_web_page_preview=0"
#     if parse_mode:
#         message += "&parse_mode=%s" % parse_mode
#     if extra:
#         message += "&%s" % extra
# 
#     code = False
#     attempt = 0
#     while not code:
#         # It this is executed as per unit testing, skip sending message
#         UTdisable = not plugin.config.config(key='unittest', default=False)
#         Silent = not plugin.config.gconfig(key='silent', default=False, gid=geteffectivegid(gid=chat_id))
#         if UTdisable and Silent:
#             result = json.load(urllib.urlopen(message))
#             code = result['ok']
#         else:
#             code = True
#             result = ""
# 
#         if attempt > 0:
#             logger.error(msg=_L("ERROR (%s) sending message: Code: %s : Text: %s") % (attempt, code, result))
# 
#         attempt += 1
#         sleep(1)
#         if not code:
#             error = result['description']
#             if 'entity starting at byte offset' in error:
#                 # Trim the byte offset to make this error more generic
#                 error = error[:103]
# 
#             for case in Switch(error):
#                 if case(u"Bad Request: message text is empty"):
#                     # Message is empty, no need to resend
#                     attempt = 61
#                     break
# 
#                 if case(u"Forbidden: bot can't initiate conversation with a user"):
#                     # Bot hasn't been authorized by user, cancelling
#                     attempt = 61
#                     break
# 
#                 if case(u"Bad Request: reply message not found"):
#                     # Original reply has been deleted
#                     attempt = 61
#                     break
# 
#                 if case(u"Forbidden: bot was blocked by the user"):
#                     # User blocked the bot
#                     attempt = 61
#                     break
# 
#                 if case(u"Bad Request: can't parse entities in message text: Can't find end of the entity starting at byte offset"):
#                     attempt = 61
#                     break
# 
#                 if case(u'Forbidden: bot was kicked from the supergroup chat'):
#                     attempt = 61
#                     break
# 
#                 if case(u'Bad Request: chat not found'):
#                     attempt = 61
#                     break
# 
#                 if case(u'Bad Request: reply message not found'):
#                     attempt = 61
#                     break
# 
#         # exit after 60 retries with 1 second delay each
#         if attempt > 60:
#             logger.error(msg=_L("PERM ERROR sending message: Code: %s : Text: %s") % (code, result))
#             code = True
# 
#     try:
#         sent = {"message": result['result']}
#     except:
#         sent = False
# 
#     if sent:
#         # Check if there's something to forward and do it
#         plugin.forward.forwardmessage(sent)
# 
#     logger.debug(msg=_L("Sending message: Code: %s : Text: %s") % (code, text))
#     return code


# Path: sendmessage.py
import argparse
from stampy.stampy import sendmessage

#!/usr/bin/env python
# encoding: utf-8
#
# Description: Sends a message to a chatID via CLI
# Author: Pablo Iranzo Gomez (Pablo.Iranzo@gmail.com)




description = 'Sends a message using bot to chatid'

# Option parsing
p = argparse.ArgumentParser("sendmessage.py [arguments]", description=description)
p.add_argument("-i", "--id", dest="chat_id", help="chat_id to send message to", default=False)
p.add_argument("-t", "--text", dest="texto", help="Text to send", default=False)


options, unknown = p.parse_known_args()