def delete(event, context): """ Handle Delete events To return a failure to CloudFormation simply raise an exception, the exception message will be sent to CloudFormation Events. """ bot_name = _bot_name(event) resources = event.get('ResourceProperties') intents = _extract_intents(bot_name, resources) bot = Bot.create_bot(bot_name, intents, resources.get('messages'), locale=resources.get('locale'), description=resources.get('description')) slot_builder = slot_builder_instance(context) lex_bot_builder = lex_builder_instance(context) lex_bot_builder.delete(bot) slot_types = event.get('ResourceProperties').get('slotTypes') slot_types = [] if slot_types is None else slot_types for slot_type in slot_types: name = _name_prefix(event) + slot_type slot_builder.delete_slot_type(name)
def test_delete_bot_intents_called(intent_builder, put_bot_response, mocker): lex, intents = setup() delete_intent_response = {'test': 'response'} # delete_response = {'test': 'bot response'} with Stubber(lex) as stubber: context = mocker.Mock() intent_builder_instance = intent_builder.return_value intent_builder_instance.delete_intents.return_value = delete_intent_response stub_get_request(stubber) stubber.add_response('delete_bot', {}, {'name': BOT_NAME}) bot_builder = LexBotBuilder(Mock(), context, lex_sdk=lex, intent_builder=intent_builder_instance) bot = Bot.create_bot(BOT_NAME, intents, {}) bot_builder.delete(bot) assert intent_builder_instance.delete_intents.call_count == 1 intent_builder_instance.delete_intents.assert_called_with( ['greeting', 'farewell']) stubber.assert_no_pending_responses()
def create(event, context): """ Handle Create events To return a failure to CloudFormation simply raise an exception, the exception message will be sent to CloudFormation Events. """ slot_builder = slot_builder_instance(context) lex_bot_builder = lex_builder_instance(context) resources = event.get('ResourceProperties') slot_types = SlotType.create_slot_types(resources.get('slotTypes'), prefix=_name_prefix(event)) for slot_type in slot_types: slot_builder.put_slot_type(slot_type) bot_name = _bot_name(event) intents = _extract_intents(bot_name, resources) _validate_intents(intents) bot = Bot.create_bot(bot_name, intents, resources.get('messages'), locale=resources.get('locale'), description=resources.get('description')) bot_put_response = lex_bot_builder.put(bot) return dict(BotName=bot_put_response['name'], BotVersion=bot_put_response['version'])
def test_update_puts_bot(intent_builder, intent_defs, put_bot_response, bot_properties, mocker): """ test_update_puts_bot""" lex, intents = setup() expected_put_params = put_bot_request(BOT_NAME, intents, MESSAGES, has_checksum=True) with Stubber(lex) as stubber: context = mock_context(mocker) intent_builder_instance = stub_put_intent(intent_builder) stub_get_request(stubber) stub_put_bot(stubber, put_bot_response, expected_put_params) bot_builder = LexBotBuilder(Mock(), context, lex_sdk=lex, intent_builder=intent_builder_instance) bot = Bot.create_bot(BOT_NAME, intents, MESSAGES, **bot_properties) response = bot_builder.put(bot) assert response['name'] == BOT_NAME assert response['version'] == BOT_VERSION stubber.assert_no_pending_responses()
def test_create_bot(bot_properties): name = 'test name' intents = 'testi intents' messages = 'test messages' bot = Bot.create_bot(name, intents, messages, **bot_properties) assert bot.name == name assert bot.intents == intents assert bot.messages == messages assert bot.attrs == bot_properties
def get_bot(self) -> Bot: r = requests.get(f'https://botlista.pl/api/bots/{self.bot_id}').json() if 'status' in r: message = r['message'] if r['status'] == 404: raise BotNotFound(message) elif r['status'] == 500: raise InternalServerError(message) return Bot(r)
def add_user_info(sender_id): # look for customer customer = Customer.find_by_psid(sender_id) vendor = customer.vendor bot = Bot(access_token=vendor.access_token) # creat order object and fill it from temp dict order = Order(sender_id, vendor.page_id) if sender_id in orders: items = orders[sender_id] for item in items: order.add_item(category=item['category'], name=item['name'], quantity=item['quantity'], price=item['price'], combo=item['combo'], notes=item['notes']) else: bot.send_text_message( sender_id, 'انتهت صلاحية الأوردر من فضلك ابدأ أوردر جديد') return 'Order Expired', 200 # update customer info customer.name = request.form.get('name') customer.phone_number = request.form.get('phone_number') customer.address = request.form.get('address') customer.save() # imp # make a receipt receipt = ReceiptTemplate( recipient_name=customer.name, order_number=order.number) for item in order.items: # fill receipt with order from database if item['combo'] == 15: details = '{} + Combo'.format(item['type']) else: details = '{}'.format(item['type']) receipt.add_element( title=item['name'], subtitle=details, quantity=item['quantity'], price=item['price']) receipt.set_summary(total_cost=order.total) bot.send_template_message(sender_id, {'payload': receipt.get_receipt()}) bot.send_text_message( sender_id, 'يتم الآن تحضير الأوردر وسيصلك في خلال 45 - 60 دقيقة') result = orders.pop(sender_id, None) # remove order from temp dict # receipt.send(restaurant) order.save() # imp send_order_to_vendor(order, vendor.uid) return 'Customer info was added', 200
def load_test(): data = {'object': 'page', 'entry': [{'id': '103750251156613', 'time': 1582133174018, 'messaging': [{'sender': {'id': '1826620787462649'}, 'recipient': { 'id': '103750251156613'}, 'timestamp': 1582133173655, 'postback': {'title': 'Show Menu', 'payload': 'family_menu'}}]}]} webhook_type = helper.get_type_from_payload(data) page_id = helper.get_vendor_from_message(data) vendor = helper.handle_vendor(page_id) bot = Bot(access_token=vendor.access_token) sender_id = helper.get_customer_from_message(data) customer = helper.handle_customer(sender_id, page_id) if webhook_type == "text": # HANDLE TEXT MESSAGES HERE # bot.send_before_message(sender_id) blocks = vendor.menu block = blocks['welcome_message'] return "text", 200 elif webhook_type == "quick_reply": # HANDLE QUICK REPLIES HERE # bot.send_before_message(sender_id) block_name = helper.quick_replies_events(data) blocks = vendor.menu if block_name in blocks: block = blocks[block_name] # bot.send_template_message(sender_id, block) return "quick_reply", 200 elif webhook_type == "postback": # HANDLE POSTBACK HERE # bot.send_before_message(sender_id) block_name = helper.postback_events(data) blocks = vendor.menu if block_name in blocks: block = blocks[block_name] # bot.send_template_message(sender_id, block) return "postback", 200 else: return "ok", 200 return "ok", 200
def test_create_put_intent_called(intent_builder, put_bot_response, bot_properties, mocker): """ create put intent called test """ lex, intents = setup() expected_put_params = put_bot_request(BOT_NAME, intents, MESSAGES) with Stubber(lex) as stubber: context = mock_context(mocker) intent_builder_instance = stub_put_intent(intent_builder) stub_not_found_get_request(stubber) stub_put_bot(stubber, put_bot_response, expected_put_params) bot_builder = LexBotBuilder(Mock(), context, lex_sdk=lex, intent_builder=intent_builder_instance) bot = Bot.create_bot(BOT_NAME, intents, MESSAGES, **bot_properties) bot_builder.put(bot) assert intent_builder_instance.put_intent.call_count == 2 intent_builder_instance.put_intent.assert_called_with(intents[1])
def test_delete_bot_on_deleted_bot(intent_builder, put_bot_response, mocker): """ delete bot does not fail test """ lex, intents = setup() delete_intent_response = {'test': 'response'} with Stubber(lex) as stubber: context = mocker.Mock() intent_builder_instance = intent_builder.return_value intent_builder_instance.delete_intents.return_value = delete_intent_response stub_not_found_get_request(stubber) stubber.add_response('delete_bot', {}, {'name': BOT_NAME}) bot_builder = LexBotBuilder(Mock(), context, lex_sdk=lex, intent_builder=intent_builder_instance) bot = Bot.create_bot(BOT_NAME, intents, {}) bot_builder.delete(bot) assert intent_builder_instance.delete_intents.call_count == 1
def test_delete_bot_called(intent_builder, put_bot_response, bot_properties, mocker): """ delete bot called test """ lex, intents = setup() delete_intent_response = {'test': 'response'} with Stubber(lex) as stubber: context = mocker.Mock() intent_builder_instance = intent_builder.return_value intent_builder_instance.delete_intents.return_value = delete_intent_response stub_get_request(stubber) stubber.add_response('delete_bot', {}, {'name': BOT_NAME}) bot_builder = LexBotBuilder(Mock(), context, lex_sdk=lex, intent_builder=intent_builder_instance) bot = Bot.create_bot(BOT_NAME, intents, MESSAGES, **bot_properties) bot_builder.delete(bot) stubber.assert_no_pending_responses()
from firebase_admin import db from time import sleep from models.bot import Bot from repositories.json import FirebaseJSON bot: Bot = Bot() etag: str = None def change(event): global bot global etag if etag == None: ret = db.reference().child('bot').get(etag=True) etag = ret[1] bot.updateFromJSON(ret[0]) else: ret = db.reference().child('bot').get_if_changed(etag) if ret[0]: etag = ret[2] bot.updateFromJSON(ret[1]) def sync(): return change() def update(data): try: db.reference().child('bot').set(FirebaseJSON().encode(data))
from models.apiaccess import APIAccess from models.country import Country from models.group import Group from models.keywordmodel import Keyword from models.notifications import Notifications from models.user import User from models.suggestion import Suggestion from models.favorite import Favorite from models.message import Message from models.statistic import Statistic from models.statistic import track_activity from models.revision import Revision if __name__ == "__main__": Category.create_table(fail_silently=True) Bot.create_table(fail_silently=True) Country.create_table(fail_silently=True) Channel.create_table(fail_silently=True) User.create_table(fail_silently=True) Suggestion.create_table(fail_silently=True) Group.create_table(fail_silently=True) Notifications.create_table(fail_silently=True) Keyword.create_table(fail_silently=True) Favorite.create_table(fail_silently=True) APIAccess.create_table(fail_silently=True) APIAccess.insert({ 'user': User.get(User.username == 'Josxa'), 'token': '5f25218eb541b992b926c2e831d9e611853158e9bd69af56a760b717922029a9',
def buildProtocol(self, addr): bot = Bot() bot.factory = self return bot
import discord import os from discord.ext import commands from models.bot import Bot from models.config import Config from models.exceptions import LoadConfigException from models.giveaway import Giveaway if __name__ == "__main__": try: cfg = Config().data bot = Bot(cfg["token"], cfg["version"], cfg["bot_prefix"], cfg["music_prefix"], cfg["msg_req"], cfg["msg_req_active"]) for filename in os.listdir('./cogs'): if filename.endswith('.py'): bot.load_extension(f"cogs.{filename[:-3]}") bot.run() except LoadConfigException as LCEx: print(f"Failed to load config.\r\nResponse: {LCEx.response}") wait = input() except Exception as Ex: print(Ex)