Mage Titans USA 2016 Magento/Facebook/RabbitMQ

907 Aufrufe

Veröffentlicht am

Demonstrate Magento and RabbitMQ integration on example of the Facebook Chat Bot

Veröffentlicht in: Software
0 Kommentare
1 Gefällt mir
Statistik
Notizen
  • Als Erste(r) kommentieren

Keine Downloads
Aufrufe
Aufrufe insgesamt
907
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
3
Aktionen
Geteilt
0
Downloads
15
Kommentare
0
Gefällt mir
1
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie
  • I want to show how the framework makes it easier
    Two directions of integrations
    Real-world example, built specifically for this conference, available on github
  • you download library for chat bots. Here is a good one vrann/fbcahtbot (because I’ve written it:)
    you pass authorization on facebook
    you need to deploy chatbot app on AWS and enable https
    Once Facebook authorized it, you will receive messages from the messenger to the callback
    You can respond invoking Facebook API with the response message and receiver id


    Callback
    Web endpoint
    https
    It should be registered with the facebook
    It should be subscribed to the webhooks
    It should send 200 OK when received the message
  • Facebook invokes callback with the message in JSON structure we’ve seen before
    The job of the Callback script will be simple – Make API request to Magento and pass message to Magento
    For this, we need to implement new Magento API which can accept such message structure
  • Let’s start from the Service Interface – it defines web API endpoint
    It tells few things:
    - Class and Method which will be invoked by web api call should implement this interface
    Input parameter of the method should be MessageTextInterface

    What is MessageTextInterface?
  • Let’s take original JSON request from Facebook
    We want little to no modification in it in order to have Magento accept it as Web API request. Otherwise it puts more work on the callback script

    Magento API Interfaces represent same data structure: we define every object in JSON structure as a separate interface in PHP. If objects are same we can re-use one interface like you see in sender and recipient


    Data Interfaces contains getters and setters for the fields
    Type of fields is either scalar value or data interface

    Why this is needed?
    It allows unified serialization pattern
    This is complex case, when we mock messages from external system. (Sometimes it is not possible, when conventions are different)
    In usual case you design objects for Magento and write a connector from external system. L
  • Request is blocking, it means that while Magento haven’t processed request, loaded required information from the database, sent response back to Facebook, the callback will keep the connection.
    Facebook will not send next request until previous request is processed, so this delays delivery of other messages

    Answer to such kind of problems is Queues
  • - simple receiver gets request and puts to rabbitmq exchange
    Callback.php sends message to the callback.received queue
  • - I’m lucky to have Magento Enterprise, which already has built-in support of the rabbit. I will just connect it to that Rabbit instance. (Otherwise, I would need to write a connector, etc)
    Write communication.xml and queue_consumer.xml and bind it to existing Web API

    topic-based exchange
    Topic name is the same as a queue name
    Here we just listen for the messages, we assume that queue exists on the rabbit (created manually)
    Now I can run consumers and have messages processed from the Rabbit!


    Handler uses same interface as web api endpoint
    Handler uses same input parameter
    Queue ”callback.received” is the one where callback will put messages
    Consumer name is an alias to run consumers

  • Difference in two configuration files and we have:
    sclability
    availability,
    Non-blocking requests

    For developers, you can two versions of extension: community edition and enterprise edition. For relatively small stores with not too many requests we can do blocking calls
  • - simple receiver gets request and puts to rabbitmq exchange
    Callback.php sends message to the callback.received queue
  • Show request/response of Facebook messages with the two rabbit listeners in CLI

    we can send message back directly, or we can put it back to the queue — this will save us from situations when facebook is unreachable.
    We create Interface with the semantic “send message to Facebook”
    We write “Remote preference” for it – it generates us implementation which sends message
    Write queue_publisher.xml
    Write simple rabbit listener which forwards messages to facebook.
  • These files available in 2.2, before it was just queue.xml
    Publisher allows to configure which exchange will be message with the topic posted to
    If we don’t define it, it will go to default exchange.

    Topology defines binding of exchanges to the queues. Here we need to bind to the queue which response.php script knows about, otherwise it will not receive the message
    We bind exchange to which publisher publishes topic to the queue which is read by response.php by the topic which is generated by Remote

    you can configure all RabbitMQ topology on the Magento side in the same way as you configure database schema
  • Mage Titans USA 2016 Magento/Facebook/RabbitMQ

    1. 1. © 2016 Magento, Inc. Page | 1 Integrations with Magento, end to end story: RabbitMQ, APIs
    2. 2. © 2016 Magento, Inc. Page | 2 Magento 2 Architect: - Magento Service Layer - Composer Integration - Magento CLI - Rabbit MQ Integration @vrann Eugene Tulika
    3. 3. © 2016 Magento, Inc. Page | 3 On average, Magento store integrates with 15 different external systems Integrations Framework: • API In supported by Web API (CE) and RabbitMQ (EE) • API Out supported by RabbitMQ (EE)
    4. 4. © 2016 Magento, Inc. Page | 4 Magento Chat Bot for Facebook Messenger
    5. 5. © 2016 Magento, Inc. Page | 5 Use-Case: Store With Hand-Picked Books • Niche customers with unusual requests
    6. 6. © 2016 Magento, Inc. Page | 6 Use-Case: Store With Hand-Picked Books • Facebook is the best way to reach larger and targeted audience • Facebook Page representing the store • Customers don’t want to leave Facebook to shop in store • Customers ask questions in Messenger
    7. 7. © 2016 Magento, Inc. Page | 7 Facebook Chat Bots API • Manager cannot handle all communications manually • Facebook webhooks for messages received in the chat • Facebook API for responses • Backed by simple AI to understand the context of discussion
    8. 8. © 2016 Magento, Inc. Page | 8 Facebook POSTs message to the Callback Callback.php 200 OK
    9. 9. © 2016 Magento, Inc. Page | 9 Callback POSTs response to Facebook API Callback.php API
    10. 10. © 2016 Magento, Inc. Page | 10 Integration with Magento IN/OUT Using Web API
    11. 11. © 2016 Magento, Inc. Page | 11 Callback makes API request to Magento Messenger Callback.php API Web API Request Magento POSTs response to Facebook
    12. 12. © 2016 Magento, Inc. Page | 12 Web API Service Interface
    13. 13. © 2016 Magento, Inc. Page | 13 Magento API Interfaces • https://github.com/vrann/magebot
    14. 14. © 2016 Magento, Inc. Page | 14 Map Service to the Web API endpoint
    15. 15. © 2016 Magento, Inc. Page | 15 What can go wrong? When customer writes in Messenger: • Blocking request from callback to Magento • Scalability: Magento can be overloaded processing other requests • Availability: Magento API is unreachable at that moment Messages from customer are lost!
    16. 16. © 2016 Magento, Inc. Page | 16 API IN Using RabbitMQ
    17. 17. © 2016 Magento, Inc. Page | 17 • Scripts: https://github.com/vrann/http-rabbitmq-writer Callback puts messages to the Queue Messenger API Callback.php Magento POSTs response to Facebook Rabbit MQ
    18. 18. © 2016 Magento, Inc. Page | 18 Magento: transform Web API to RabbitMQ Map message handler to topic in communication.xml Map consumer to queue in queue_consumer.xml Run consumer(s):
    19. 19. © 2016 Magento, Inc. Page | 19 With Communication & Queue configs We achieved:  Non-Blocking request, response to Facebook sent immediately  Scalability handled by Queue  Availability handled by Queue Communication vs Queue: • Communication Framework is an abstraction around publisher/subscriber. It can be implemented: – with database and HTTP requests instead (webhooks) – in-memory (observers) • Queue is a Transport layer for Communication – configuration of RabbitMQ adapter and topology
    20. 20. © 2016 Magento, Inc. Page | 20 OUT Using RabbitMQ
    21. 21. © 2016 Magento, Inc. Page | 21 • Scripts: https://github.com/vrann/http-rabbitmq-writer Goal: Magento sends messages to Rabbit Messenger API callback.php response.php
    22. 22. © 2016 Magento, Inc. Page | 22 Before: sender interface with the HTTP request
    23. 23. © 2016 Magento, Inc. Page | 23 Now: Sender interface with RabbitMQ Replace preference for MessageSenderInterface in di.xml, Add “Remote” suffix Interface implementation will be auto-generated:
    24. 24. © 2016 Magento, Inc. Page | 24 Publisher & Topology queue_publisher.xml maps generated topic to the exchange queue_topology.xml configures exchange to queue bindings Available from Magento 2.2, before it was in queue.xml
    25. 25. © 2016 Magento, Inc. Page | 25
    26. 26. © 2016 Magento, Inc. Page | 26 Input Messages Handler Input Classifier Search catalog by author Title, image url, description For Input Classifier it is better to use
    27. 27. © 2016 Magento, Inc. Page | 27 Wrap Up • We covered: – Reasons behind Magento Web API data structures – Reasons to use queues – Switch from Web API to the RabbitMQ – Code generation of API OUT for RabbitMQ – Communication framework vs Queue framework – Separation of publishers, consumers, topology in 2.2 – Input Message Handling – Facebook bots! • https://github.com/vrann/facebook-chatbot • https://github.com/vrann/http-rabbitmq-writer • https://github.com/vrann/magebot
    28. 28. © 2016 Magento, Inc. Page | 28 Q&A Write me @vrann (twitter, github)

    ×