{ "info": { "author": "Engine Bai", "author_email": "enginebai@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "# PyMessager\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![PyPI version](https://badge.fury.io/py/PyMessager.svg)](https://badge.fury.io/py/PyMessager)\n\nPyMessager is a Python API for [Facebook Messenger](https://developers.facebook.com/docs/messenger-platform) and a sample project to demonstrate how to develop a chatbot on Facebook Messenger.\n\n![](https://raw.githubusercontent.com/enginebai/PyMessager/master/art/graphic.png)\n\nComplete tutorials are on [Develop a Facebook Bot Using Python](https://medium.com/@enginebai/%E7%94%A8python%E9%96%8B%E7%99%BCfacebook-bot-26594f13f9f7#.7iwm148ag) and [Chatbot: From 0 To 1](https://medium.com/dualcores-studio/%E8%81%8A%E5%A4%A9%E6%A9%9F%E5%99%A8%E4%BA%BA%E5%85%A5%E9%96%80-%E5%BE%9E0%E5%88%B01-4792b53a1318) where you can find more detailed information to setup and develop.\n\n## Before Starting\n1. Prepare a Facebook Page. (to create if you don't have one)\n2. Create a developer application on [Facebook for Developers](https://developers.facebook.com).\n3. Start a python project, and install the required packages and modules: [Flask](http://flask.pocoo.org), [Requests](http://docs.python-requests.org/en/master/).\n4. Use [Let's Encrypt](https://letsencrypt.org/getting-started/) to apply SSL certification for your domain name.\n\n## Install\nTo install PyMessager, simply run:\n\n```shell\n$ pip install pymessager\n```\n\nor install from the repository:\n\n```shell\n$ git clone git@github.com:enginebai/PyMessager.git\n$ cd PyMessager\n$ pip install -r requirements.txt\n```\n\n## Get Started\n\n\n### Import\n```python\nfrom pymessager.message import Messager, ... # something else you need\n```\n\n### Initialization\nYou can initialize a messager client via a Facebook Access Token from the developer console:\n\n```python\nfrom pymessager.message import Messager\nclient = Messager(config.facebook_access_token)\n```\n\n## Receiver APIs\nThe following code is used to build a message receiver, there are three main steps to prepare for your bot:\n\n1. Setup the Webhook\n\n```python\n@app.route(API_ROOT + FB_WEBHOOK, methods=[\"GET\"])\ndef fb_webhook():\n verification_code = 'I_AM_VERIFICIATION_CODE'\n verify_token = request.args.get('hub.verify_token')\n if verification_code == verify_token:\n return request.args.get('hub.challenge')\n```\n\n2. Receive the message\n\n```python\n@app.route(API_ROOT + FB_WEBHOOK, methods=['POST'])\ndef fb_receive_message():\n message_entries = json.loads(request.data.decode('utf8'))['entry']\n for entry in message_entries:\n for message in entry['messaging']:\n if message.get('message'):\n print(\"{sender[id]} says {message[text]}\".format(**message))\n return \"Hi\"\n```\n\n3. Start the server with https\n\n```python\nif __name__ == '__main__':\n context = ('ssl/fullchain.pem', 'ssl/privkey.pem')\n app.run(host='0.0.0.0', debug=True, ssl_context=context)\n```\n\n\n## Sender APIs\n![](https://raw.githubusercontent.com/enginebai/PyMessager/master/art/usage.png)\n\nThere are several types of message: `text`, `image`, `quick replies`, `button template` or `generic template`. API provides different classes to generate the message template.\n\n### Sending a text and image\nSend a simple text or an image to a recipient, just make sure that image URL is a valid link.\n\n```python\nclient.send_text(recipient_id, \"Hello, I'm enginebai.\"\nclient.send_image(recipient_id, \"http://image-url.jpg\")\n```\n\n### Quick Replies\nThe `QuickReply(title, payload, image_url, content_type)` class defines a present buttons to the user in response to a message.\n\n| Parameter | Description | Required |\n| -------------- | ------------------------ | -------- |\n| `title` | The button title | Y |\n| `payload` | The click payload string | Y |\n| `image_url` | The icon image URL | N |\n| `content_type` | `TEXT` or `LOCATION` | Y |\n\n```python\nclient.send_quick_replies(recipient_id, \"Help\", [\n QuickReply(\"Projects\", Intent.PROJECT),\n QuickReply(\"Blog\", Intent.BLOG),\n QuickReply(\"Contact Me\", Intent.CONTACT_ME)\n ])\n```\n\n### Button Template\nThe `ActionButton(button_type, title, url, payload)` class defines button template which contains a text and buttons attachment to request input from the user.\n\n| Parameter | Description | Required |\n| ------------- | ------------------------ | ----------------------------------- |\n| `button_type` | `WEB_URL` or `POSTBACK` | Y |\n| `title` | The button title | Y |\n| `url` | The link | Only if `button_type` is `url` |\n| `payload` | The click payload string | Only if `button_type` is `POSTBACK` |\n\n```python\nclient.send_buttons(recipient_id, \"You can find me with below\", [\n ActionButton(ButtonType.WEB_URL, \"Blog\", \"http://blog.enginebai.com\"),\n\tActionButton(ButtonType.POSTBACK, \"Email\", Intent.EMAIL)\n])\n```\n### Generic Template\nThe `GenericElement(title, subtitle, image_url, buttons)` class defines a horizontal scrollable carousel of items, each composed of an image attachment, short description and buttons to request input from the user.\n\n| Parameter | Description | Required |\n| --------------- | ---------------------------------------- | -------- |\n| `title_text` | The message main title | Y |\n| `subtitle_text` | The message subtitle, leave it empty if you don't need it | N |\n| `button_list` | The list of `ActionButton` | Y |\n\n\n```python\nproject_list = []\nfor project_id, project in projects.items():\n project_list.append(GenericElement(\n project[\"title\"],\n project[\"description\"],\n config.api_root + project[\"image_url\"], [\n ActionButton(ButtonType.POSTBACK,\n self._get_string(\"button_more\"),\n # Payload use Intent for the beginning\n payload=Intent.PROJECTS.name + project_id)\n ]))\nclient.send_generic(recipient_id, project_list)\n```\n\n## Utility APIs\n\n### Subscribe the pages\nBefore your chatbot starts to receive messages, you have to subscribe the application to your chatbot page. To subscribe a page, just call it:\n\n```python\nclient.subscribe_to_page()\n```\n\n### Set the welcome message and get-started button\n![](https://raw.githubusercontent.com/enginebai/PyMessager/master/art/onboarding.png)\n\nThe greeting text will show at the first time you open this chatbot on mobile only. The payload is the trigger when the users click \"Get Started\" button.\n\n```python\nclient.set_greeting_text(\"Hi, this is Engine Bai. Nice to meet you!\")\nclient.set_get_started_button_payload(\"HELP\") # Specify a payload string.\n```\n\n## Issues\nFeel free to submit bug reports or feature requests and make sure you read the contribution guideline before opening any issue.\n\n\n## Contributing\n1. Check the open/close issues or open a fresh issue for feature request or bug report with different labels (`feature`/`bug`).\n2. Fork this [repository](https://github.com/enginebai/PyMessager) on GitHub to start customizing on master or new branch.\n3. Write a test which shows that the feature works as expected or the bug was fixed.\n4. Send a pull request and wait for code review.\n\n[Read more on contributing](./CONTRIBUTING.md).\n\n## License\n\n\tThe MIT License (MIT)\n\n\tCopyright \u00a9 2017 Engine Bai.\n\n\tPermission is hereby granted, free of charge, to any person obtaining a copy\n\tof this software and associated documentation files (the \"Software\"), to deal\n\tin the Software without restriction, including without limitation the rights\n\tto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n\tcopies of the Software, and to permit persons to whom the Software is\n\tfurnished to do so, subject to the following conditions:\n\t\n\tThe above copyright notice and this permission notice shall be included in\n\tall copies or substantial portions of the Software.\n\t\n\tTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n\tIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n\tFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n\tAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n\tLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n\tOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n\tTHE SOFTWARE.\n", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/enginebai/PyMessager", "keywords": "", "license": "The MIT License (MIT) Copyright \u00a9 2017 Engine Bai.", "maintainer": "", "maintainer_email": "", "name": "PyMessager", "package_url": "https://pypi.org/project/PyMessager/", "platform": "", "project_url": "https://pypi.org/project/PyMessager/", "project_urls": { "Homepage": "https://github.com/enginebai/PyMessager" }, "release_url": "https://pypi.org/project/PyMessager/1.0.1/", "requires_dist": null, "requires_python": "", "summary": "A Python SDK and Flask API to develop Facebook Messenger application", "version": "1.0.1" }, "last_serial": 3251424, "releases": { "1.0.0": [ { "comment_text": "", "digests": { "md5": "1f15b31e1b3297d3a50d1b11a147de6d", "sha256": "e518e6efa3e4d6b9ec65f3144dadb2bf1da8c6927adc11e1bfa047685888431f" }, "downloads": -1, "filename": "PyMessager-1.0.0.tar.gz", "has_sig": false, "md5_digest": "1f15b31e1b3297d3a50d1b11a147de6d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 7477, "upload_time": "2017-06-26T15:18:31", "url": "https://files.pythonhosted.org/packages/42/9d/ef7c0089df8edef8903ed76f3aa0c84c8ef6f25da9a47db0b983af377a5f/PyMessager-1.0.0.tar.gz" } ], "1.0.1": [ { "comment_text": "", "digests": { "md5": "01da43e80001df8bd2aa050c9e65520b", "sha256": "e7b6d8d5ae9bbc93d608b2bb55b369e1483f60d6fc9dd9e3863910bd3555e36d" }, "downloads": -1, "filename": "PyMessager-1.0.1.tar.gz", "has_sig": false, "md5_digest": "01da43e80001df8bd2aa050c9e65520b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6986, "upload_time": "2017-10-15T13:09:18", "url": "https://files.pythonhosted.org/packages/46/2d/228c56970d78604ad11829cd03956a7da52f56f13269e3cbf4e811b08dd3/PyMessager-1.0.1.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "01da43e80001df8bd2aa050c9e65520b", "sha256": "e7b6d8d5ae9bbc93d608b2bb55b369e1483f60d6fc9dd9e3863910bd3555e36d" }, "downloads": -1, "filename": "PyMessager-1.0.1.tar.gz", "has_sig": false, "md5_digest": "01da43e80001df8bd2aa050c9e65520b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6986, "upload_time": "2017-10-15T13:09:18", "url": "https://files.pythonhosted.org/packages/46/2d/228c56970d78604ad11829cd03956a7da52f56f13269e3cbf4e811b08dd3/PyMessager-1.0.1.tar.gz" } ] }