Skip to content

Commit

Permalink
Merged
Browse files Browse the repository at this point in the history
  • Loading branch information
CrispyBacon1999 committed Oct 23, 2017
2 parents 489cbe0 + a6ed120 commit 260e0b6
Show file tree
Hide file tree
Showing 6 changed files with 993 additions and 299 deletions.
58 changes: 57 additions & 1 deletion vyper/basebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class BaseBot(vyper.API):

<<<<<<< HEAD
start_message = 'Starting bot. Break the loop by using Ctrl-C on Windows, or Command+C on Mac.'
web_app = None
def __init__(self, token, debug=False, start_loop=False, loop_time=.05, web_app=None, name=""):
Expand Down Expand Up @@ -60,4 +61,59 @@ def shipping_query(self, msg):
print('Create a shipping_query(self, msg) method in order to handle this update!')

def pre_checkout_query(self, msg):
print('Create a pre_checkout_query(self, msg) method in order to handle this update!')
print('Create a pre_checkout_query(self, msg) method in order to handle this update!')
=======
start_message = 'Starting bot. Break the loop by using Ctrl-C on Windows, or Command+C on Mac.'

def __init__(self, token, debug=False, start_loop=False, loop_time=.05):
self.functions = {
'message': self.message,
'edited_message': self.edited_message,
'channel_post': self.channel_post,
'edited_channel_post': self.edited_channel_post,
'inline_query': self.inline_query,
'chosen_inline_result': self.chosen_inline_result,
'callback_query': self.callback_query,
'shipping_query': self.shipping_query,
'pre_checkout_query': self.pre_checkout_query
}
self.configure(token, functions=self.functions, debug=debug)
if start_loop:
self.start_loop(loop_time)

def start_loop(self, loop_time):
print(self.start_message)
while True:
self.getUpdates()
time.sleep(loop_time)

def set_functions(self, functions):
self.functions = functions

def message(self, msg):
print('Create a message(self, msg) method in order to handle this update!')

def edited_message(self, msg):
print('Create an edited_message(self, msg) method in order to handle this update!')

def channel_post(self, msg):
print('Create a channel_post(self, msg) method in order to handle this update!')

def edited_channel_post(self, msg):
print('Create an edited_channel_post(self, msg) method in order to handle this update!')

def inline_query(self, msg):
print('Create an inline_query(self, msg) method in order to handle this update!')

def chosen_inline_result(self, msg):
print('Create a chosen_inline_result(self, msg) method in order to handle this update!')

def callback_query(self, msg):
print('Create a callback_query(self, msg) method in order to handle this update!')

def shipping_query(self, msg):
print('Create a shipping_query(self, msg) method in order to handle this update!')

def pre_checkout_query(self, msg):
print('Create a pre_checkout_query(self, msg) method in order to handle this update!')
>>>>>>> a6ed12094081b453fddc13f9379f372dc12de45d
80 changes: 40 additions & 40 deletions vyper/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,48 @@
import itertools

class Keyboard:
keyboard = {'inline_keyboard': []}
def __init__(self, rows=1, columns=1):
self.keyboard['inline_keyboard'] = [[{'text': 'Sample Text'} for col in range(columns)] for row in range(rows)]
keyboard = {'inline_keyboard': []}
def __init__(self, rows=1, columns=1):
self.keyboard['inline_keyboard'] = [[{'text': 'Sample Text'} for col in range(columns)] for row in range(rows)]

def write_to_excel(self, file):
raise FutureWarning('The write_to_excel() method will be implemented in a future update.')
def write_to_excel(self, file):
raise FutureWarning('The write_to_excel() method will be implemented in a future update.')

def load_from_excel(self, file, click_type='callback_data', default_click='default_blank_callback'):
buttons = []
wb = open_workbook(file, formatting_info=True)
sheet = wb.sheet_by_name("Sheet1")
print('Reading keyboard from:', file)
for col in range(sheet.ncols):
text = data = ''
buttons.append([])
for row in range(sheet.nrows):
cell = sheet.cell(row, col)
fmt = wb.xf_list[cell.xf_index]
border = fmt.border
has_bottom = bool(border.bottom_line_style)
if not has_bottom:
text = str(cell.value)
else:
data = str(cell.value)
if data and text:
buttons[col].append({'text': text, click_type: data})
else:
buttons[col].append({'text': data, click_type: default_click})
text = ''
data = ''
def load_from_excel(self, file, click_type='callback_data', default_click='default_blank_callback'):
buttons = []
wb = open_workbook(file, formatting_info=True)
sheet = wb.sheet_by_name("Sheet1")
print('Reading keyboard from:', file)
for col in range(sheet.ncols):
text = data = ''
buttons.append([])
for row in range(sheet.nrows):
cell = sheet.cell(row, col)
fmt = wb.xf_list[cell.xf_index]
border = fmt.border
has_bottom = bool(border.bottom_line_style)
if not has_bottom:
text = str(cell.value)
else:
data = str(cell.value)
if data and text:
buttons[col].append({'text': text, click_type: data})
else:
buttons[col].append({'text': data, click_type: default_click})
text = ''
data = ''

if not has_bottom and text:
raise ExcelNoBottomException('Cell({0},{1}) has no bottom border.'.format(row, col))
# Flip columns and rows
buttons = list(map(list, itertools.zip_longest(*buttons)))
buttons = [[button for button in row if button is not None] for row in buttons]
self.keyboard['inline_keyboard'] = buttons
@property
def json(self):
return json.dumps(self.keyboard)
if not has_bottom and text:
raise ExcelNoBottomException('Cell({0},{1}) has no bottom border.'.format(row, col))
# Flip columns and rows
buttons = list(map(list, itertools.zip_longest(*buttons)))
buttons = [[button for button in row if button is not None] for row in buttons]
self.keyboard['inline_keyboard'] = buttons
@property
def json(self):
return json.dumps(self.keyboard)

class ExcelNoBottomException(BaseException):
def __init__(self, message='There\'s no bottom border for one of your buttons.'):
self.message = message
def __init__(self, message='There\'s no bottom border for one of your buttons.'):
self.message = message
63 changes: 58 additions & 5 deletions vyper/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ def hello():
return render_template('index.html', bot='Web Interface')

def log(func):
def wrapper(*args, **kwargs):
for p in args:
if type(p) == dict:
p = types.build(p)
def wrapper(*args, **kwargs):
for p in args:
if type(p) == dict:
p = types.build(p)

<<<<<<< HEAD
if type(p) == types.Message:
if hasattr(p, 'text'):
text = p.text
Expand Down Expand Up @@ -93,4 +94,56 @@ def prnt(msg):
if len(logged_info) > 50:
logged_info.pop(0)
else:
print(msg)
print(msg)
=======
if type(p) == types.Message:
if hasattr(p, 'text'):
text = p.text
elif hasattr(p, 'successful_payment'):
text = 'Successful Payment - ' + p.successful_payment.currency + ' ' + str(p.successful_payment.total_amount)
text = text[:-2] + '.' + text[-2:]
else:
text = 'Invalid Text'
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
chatname = p.chat.title if hasattr(p.chat, 'title') else p.chat.id
print('{0} : {1} {2} : {3}'.format(chatname, first, last, text))

if type(p) == types.InlineQuery:
query = p.query
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
print('Inline Query : {0} {1} : {2}'.format(first, last, query))

if type(p) == types.ChosenInlineResult:
result = p.result_id
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
print('Inline Query Chosen : {0} {1} : {2}'.format(first, last, result))

if type(p) == types.CallbackQuery:
id = p.id
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
print('Callback : {0} {1} : {2}'.format(first, last, id))

if type(p) == types.ShippingQuery:
payload = p.invoice_payload
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
print('Shipping : {0} {1} : {2}'.format(first, last, payload))

if type(p) == types.PreCheckoutQuery:
payload = p.invoice_payload
f = p.frm
first = f.first_name
last = f.last_name if hasattr(f, 'last_name') else '\b'
print('Pre-Checkout : {0} {1} : {2}'.format(first, last, payload))
return func(*args, **kwargs)
return wrapper
>>>>>>> a6ed12094081b453fddc13f9379f372dc12de45d
61 changes: 52 additions & 9 deletions vyper/pluginbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class PluginBot(basebot.BaseBot):

<<<<<<< HEAD
def __init__(self, token, debug=False, start_loop=False, loop_time=.05, ping=True, list_plugins=False, web_app=None, name=None):
if not os.path.exists('plugins'):
os.mkdir('plugins')
Expand Down Expand Up @@ -45,19 +46,61 @@ def test_plugins(self, msg):
if 'text' in msg:
for plugin in list(self.plugins):
plugin.message(msg)
=======
def __init__(self, token, debug=False, start_loop=False, loop_time=.05, ping=True, list_plugins=False):
if not os.path.exists('plugins'):
os.mkdir('plugins')
with open('plugins/__init__.py', 'w') as ini:
ini.write("""import pkgutil
__path__ = pkgutil.extend_path(__path__, __name__)
for importer, modname, ispkg in pkgutil.walk_packages(path=__path__, prefix=__name__+'.'):
__import__(modname)""")
import plugins
Ping.enabled = ping
self.functions = {
'message': self.message,
'edited_message': self.edited_message,
'channel_post': self.channel_post,
'edited_channel_post': self.edited_channel_post,
'inline_query': self.inline_query,
'chosen_inline_result': self.chosen_inline_result,
'callback_query': self.callback_query,
'shipping_query': self.shipping_query,
'pre_checkout_query': self.pre_checkout_query
}
self.configure(token, functions=self.functions, debug=debug)
self.plugins = list(self._get_plugins())
if list_plugins:
for plugin in self.plugins:
print(plugin)
if start_loop:
self.start_loop(loop_time)

def _get_plugins(self):
for plugin in Plugin.__subclasses__():
if plugin.enabled:
plugin.bot = self
yield plugin()

def test_plugins(self, msg):
if 'text' in msg:
for plugin in list(self.plugins):
plugin.message(msg)
>>>>>>> a6ed12094081b453fddc13f9379f372dc12de45d

class Plugin:
bot = None
enabled = True
def __repr__(self):
return "Plugin: {0}".format(self.__class__.__name__)
bot = None
enabled = True
def __repr__(self):
return "Plugin: {0}".format(self.__class__.__name__)

def message(self, msg):
pass
def message(self, msg):
pass


class Ping(Plugin):
def message(self, msg):
if msg['text'] == '/ping':
self.bot.sendMessage(msg['chat']['id'], 'PONG!')
def message(self, msg):
if msg['text'] == '/ping':
self.bot.sendMessage(msg['chat']['id'], 'PONG!')

0 comments on commit 260e0b6

Please sign in to comment.