diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d4a2c44 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,21 @@ +# http://editorconfig.org + +root = true + +[*] +indent_style = space +indent_size = 4 +trim_trailing_whitespace = true +insert_final_newline = true +charset = utf-8 +end_of_line = lf + +[*.bat] +indent_style = tab +end_of_line = crlf + +[LICENSE] +insert_final_newline = false + +[Makefile] +indent_style = tab diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 0000000..f4a65b8 --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,15 @@ +* Soroush Python SDK version: +* Python version: +* Operating System: + +### Description + +Describe what you were trying to get done. +Tell us what happened, what went wrong, and what you expected to happen. + +### What I Did + +``` +Paste the command(s) you ran and the output. +If there was a crash, please include the traceback here. +``` diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ac7a5cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,103 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# dotenv +.env + +# virtualenv +.venv +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.idea/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..5eeec94 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,29 @@ +# Config file for automatic testing at travis-ci.org + +language: python +python: + - 3.6 + - 3.5 + - 3.4 + - 2.7 + +# Command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors +install: pip install -U tox-travis + +# Command to run tests, e.g. python setup.py test +script: tox + +# Assuming you have installed the travis-ci CLI tool, after you +# create the Github repo and add it to Travis, run the +# following command to finish PyPI deployment setup: +# $ travis encrypt --add deploy.password +deploy: + provider: pypi + distributions: sdist bdist_wheel + user: Ali.Nikneshan + password: + secure: PLEASE_REPLACE_ME + on: + tags: true + repo: alinik/soroush_python_sdk + python: 3.6 diff --git a/AUTHORS.rst b/AUTHORS.rst new file mode 100644 index 0000000..d2ccf1f --- /dev/null +++ b/AUTHORS.rst @@ -0,0 +1,13 @@ +======= +Credits +======= + +Development Lead +---------------- + +* Ali Nikneshan + +Contributors +------------ + +None yet. Why not be the first? diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst new file mode 100644 index 0000000..039d3d7 --- /dev/null +++ b/CONTRIBUTING.rst @@ -0,0 +1,128 @@ +.. highlight:: shell + +============ +Contributing +============ + +Contributions are welcome, and they are greatly appreciated! Every little bit +helps, and credit will always be given. + +You can contribute in many ways: + +Types of Contributions +---------------------- + +Report Bugs +~~~~~~~~~~~ + +Report bugs at https://github.com/alinik/soroush_python_sdk/issues. + +If you are reporting a bug, please include: + +* Your operating system name and version. +* Any details about your local setup that might be helpful in troubleshooting. +* Detailed steps to reproduce the bug. + +Fix Bugs +~~~~~~~~ + +Look through the GitHub issues for bugs. Anything tagged with "bug" and "help +wanted" is open to whoever wants to implement it. + +Implement Features +~~~~~~~~~~~~~~~~~~ + +Look through the GitHub issues for features. Anything tagged with "enhancement" +and "help wanted" is open to whoever wants to implement it. + +Write Documentation +~~~~~~~~~~~~~~~~~~~ + +Soroush Python SDK could always use more documentation, whether as part of the +official Soroush Python SDK docs, in docstrings, or even on the web in blog posts, +articles, and such. + +Submit Feedback +~~~~~~~~~~~~~~~ + +The best way to send feedback is to file an issue at https://github.com/alinik/soroush_python_sdk/issues. + +If you are proposing a feature: + +* Explain in detail how it would work. +* Keep the scope as narrow as possible, to make it easier to implement. +* Remember that this is a volunteer-driven project, and that contributions + are welcome :) + +Get Started! +------------ + +Ready to contribute? Here's how to set up `soroush_python_sdk` for local development. + +1. Fork the `soroush_python_sdk` repo on GitHub. +2. Clone your fork locally:: + + $ git clone git@github.com:your_name_here/soroush_python_sdk.git + +3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: + + $ mkvirtualenv soroush_python_sdk + $ cd soroush_python_sdk/ + $ python setup.py develop + +4. Create a branch for local development:: + + $ git checkout -b name-of-your-bugfix-or-feature + + Now you can make your changes locally. + +5. When you're done making changes, check that your changes pass flake8 and the + tests, including testing other Python versions with tox:: + + $ flake8 soroush_python_sdk tests + $ python setup.py test or py.test + $ tox + + To get flake8 and tox, just pip install them into your virtualenv. + +6. Commit your changes and push your branch to GitHub:: + + $ git add . + $ git commit -m "Your detailed description of your changes." + $ git push origin name-of-your-bugfix-or-feature + +7. Submit a pull request through the GitHub website. + +Pull Request Guidelines +----------------------- + +Before you submit a pull request, check that it meets these guidelines: + +1. The pull request should include tests. +2. If the pull request adds functionality, the docs should be updated. Put + your new functionality into a function with a docstring, and add the + feature to the list in README.rst. +3. The pull request should work for Python 2.7, 3.4, 3.5 and 3.6, and for PyPy. Check + https://travis-ci.org/alinik/soroush_python_sdk/pull_requests + and make sure that the tests pass for all supported Python versions. + +Tips +---- + +To run a subset of tests:: + +$ py.test tests.test_soroush_python_sdk + + +Deploying +--------- + +A reminder for the maintainers on how to deploy. +Make sure all your changes are committed (including an entry in HISTORY.rst). +Then run:: + +$ bumpversion patch # possible: major / minor / patch +$ git push +$ git push --tags + +Travis will then deploy to PyPI if tests pass. diff --git a/HISTORY.rst b/HISTORY.rst new file mode 100644 index 0000000..a5f6845 --- /dev/null +++ b/HISTORY.rst @@ -0,0 +1,8 @@ +======= +History +======= + +0.1.0 (2018-10-25) +------------------ + +* First release on PyPI. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..af3ca68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2018, Ali Nikneshan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..965b2dd --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,11 @@ +include AUTHORS.rst +include CONTRIBUTING.rst +include HISTORY.rst +include LICENSE +include README.rst + +recursive-include tests * +recursive-exclude * __pycache__ +recursive-exclude * *.py[co] + +recursive-include docs *.rst conf.py Makefile make.bat *.jpg *.png *.gif diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2cf0844 --- /dev/null +++ b/Makefile @@ -0,0 +1,88 @@ +.PHONY: clean clean-test clean-pyc clean-build docs help +.DEFAULT_GOAL := help + +define BROWSER_PYSCRIPT +import os, webbrowser, sys + +try: + from urllib import pathname2url +except: + from urllib.request import pathname2url + +webbrowser.open("file://" + pathname2url(os.path.abspath(sys.argv[1]))) +endef +export BROWSER_PYSCRIPT + +define PRINT_HELP_PYSCRIPT +import re, sys + +for line in sys.stdin: + match = re.match(r'^([a-zA-Z_-]+):.*?## (.*)$$', line) + if match: + target, help = match.groups() + print("%-20s %s" % (target, help)) +endef +export PRINT_HELP_PYSCRIPT + +BROWSER := python -c "$$BROWSER_PYSCRIPT" + +help: + @python -c "$$PRINT_HELP_PYSCRIPT" < $(MAKEFILE_LIST) + +clean: clean-build clean-pyc clean-test ## remove all build, test, coverage and Python artifacts + +clean-build: ## remove build artifacts + rm -fr build/ + rm -fr dist/ + rm -fr .eggs/ + find . -name '*.egg-info' -exec rm -fr {} + + find . -name '*.egg' -exec rm -f {} + + +clean-pyc: ## remove Python file artifacts + find . -name '*.pyc' -exec rm -f {} + + find . -name '*.pyo' -exec rm -f {} + + find . -name '*~' -exec rm -f {} + + find . -name '__pycache__' -exec rm -fr {} + + +clean-test: ## remove test and coverage artifacts + rm -fr .tox/ + rm -f .coverage + rm -fr htmlcov/ + rm -fr .pytest_cache + +lint: ## check style with flake8 + flake8 soroush_python_sdk tests + +test: ## run tests quickly with the default Python + py.test + +test-all: ## run tests on every Python version with tox + tox + +coverage: ## check code coverage quickly with the default Python + coverage run --source soroush_python_sdk -m pytest + coverage report -m + coverage html + $(BROWSER) htmlcov/index.html + +docs: ## generate Sphinx HTML documentation, including API docs + rm -f docs/soroush_python_sdk.rst + rm -f docs/modules.rst + sphinx-apidoc -o docs/ soroush_python_sdk + $(MAKE) -C docs clean + $(MAKE) -C docs html + $(BROWSER) docs/_build/html/index.html + +servedocs: docs ## compile the docs watching for changes + watchmedo shell-command -p '*.rst' -c '$(MAKE) -C docs html' -R -D . + +release: dist ## package and upload a release + twine upload dist/* + +dist: clean ## builds source and wheel package + python setup.py sdist + python setup.py bdist_wheel + ls -l dist + +install: clean ## install the package to the active Python's site-packages + python setup.py install diff --git a/README.md b/README.md index 7aae189..c67e6f3 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ # Soroush Messenger Bot Python SDK Soroush Messenger Bot Wrapper for Python +![shiled](https://img.shields.io/pypi/v/soroush_python_sdk.svg) +![travis](https://img.shields.io/travis/alinik/soroush_python_sdk.svg) +![badge](https://readthedocs.org/projects/soroush-python-sdk/badge/?version=latest) +[![Documentation Status](https://readthedocs.org/projects/bot-python-sdk/badge/?version=latest)](https://bot-python-sdk.readthedocs.io/en/latest/?badge=latest) +[![Updates](https://pyup.io/repos/github/alinik/bot-python-sdk/shield.svg)](https://pyup.io/repos/github/alinik/bot-python-sdk/) ## Dependencies ## - Python 2.7+ @@ -8,6 +13,10 @@ Soroush Messenger Bot Wrapper for Python - sseclient-py ## Installation ## +```bash +pip install soroush-python-sdk +``` + Run the below commands ```bash git clone https://github.com/soroush-app/bot-python-sdk @@ -18,7 +27,7 @@ pip install -r requirements.txt ## Usage ## ```python -from client import Client +from soroush_python_sdk import Client bot_token = 'your bot token' diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..9351b57 --- /dev/null +++ b/README.rst @@ -0,0 +1,71 @@ +Soroush Messenger Bot Python SDK +================================ + +Soroush Messenger Bot Wrapper for Python |shiled| |travis| |badge| +|Documentation Status| |Updates| + +Dependencies +------------ + +- Python 2.7+ +- requests +- sseclient-py + +Installation +------------ + +.. code:: bash + + pip install soroush-python-sdk + +Run the below commands + +.. code:: bash + + git clone https://github.com/soroush-app/bot-python-sdk + cd bot-python-sdk + pip install -r requirements.txt + +Usage +----- + +.. code:: python + + from soroush_python_sdk import Client + + bot_token = 'your bot token' + + bot = Client(bot_token) + + try: + to = 'user chat_id' + + [error, success] = bot.send_text(to, 'Your text') + + if success: + print('Message sent successfully') + else: + print('Sending message failed: {}' .format(error)) + + except Exception as e: + print(e.args[0]) + +“to” value in above example is chat_id of a bot user. You can find it in +front of ‘from’ key in a message that user has sent to your bot. You can +see more examples in the `examples`_ directory. + +## Contribute ## Contributions to the package are always welcome! - +Report any idea, bugs or issues you find on the `issue tracker`_. - You +can grab the source code at the package’s `Git repository`_. + +.. _examples: https://github.com/soroush-app/bot-python-sdk/tree/master/examples +.. _issue tracker: https://github.com/soroush-app/bot-python-sdk/issues +.. _Git repository: https://github.com/soroush-app/bot-python-sdk.git + +.. |shiled| image:: https://img.shields.io/pypi/v/soroush_python_sdk.svg +.. |travis| image:: https://img.shields.io/travis/alinik/soroush_python_sdk.svg +.. |badge| image:: https://readthedocs.org/projects/soroush-python-sdk/badge/?version=latest +.. |Documentation Status| image:: https://readthedocs.org/projects/bot-python-sdk/badge/?version=latest + :target: https://bot-python-sdk.readthedocs.io/en/latest/?badge=latest +.. |Updates| image:: https://pyup.io/repos/github/alinik/bot-python-sdk/shield.svg + :target: https://pyup.io/repos/github/alinik/bot-python-sdk/ diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..349af68 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = python -msphinx +SPHINXPROJ = soroush_python_sdk +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/docs/_static/.gitdir b/docs/_static/.gitdir new file mode 100644 index 0000000..e69de29 diff --git a/docs/authors.rst b/docs/authors.rst new file mode 100644 index 0000000..e122f91 --- /dev/null +++ b/docs/authors.rst @@ -0,0 +1 @@ +.. include:: ../AUTHORS.rst diff --git a/docs/conf.py b/docs/conf.py new file mode 100755 index 0000000..bb0d1c5 --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,163 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# +# soroush_python_sdk documentation build configuration file, created by +# sphinx-quickstart on Fri Jun 9 13:47:02 2017. +# +# This file is execfile()d with the current directory set to its +# containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +# If extensions (or modules to document with autodoc) are in another +# directory, add these directories to sys.path here. If the directory is +# relative to the documentation root, use os.path.abspath to make it +# absolute, like shown here. +# +import os +import sys +sys.path.insert(0, os.path.abspath('..')) + +import soroush_python_sdk + +# -- General configuration --------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +# +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.viewcode'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +# +# source_suffix = ['.rst', '.md'] +source_suffix = '.rst' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'Soroush Python SDK' +copyright = u"2018, Ali Nikneshan" +author = u"Ali Nikneshan" + +# The version info for the project you're documenting, acts as replacement +# for |version| and |release|, also used in various other places throughout +# the built documents. +# +# The short X.Y version. +version = soroush_python_sdk.__version__ +# The full version, including alpha/beta/rc tags. +release = soroush_python_sdk.__version__ + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = None + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This patterns also effect to html_static_path and html_extra_path +exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store'] + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# If true, `todo` and `todoList` produce output, else they produce nothing. +todo_include_todos = False + + +# -- Options for HTML output ------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a +# theme further. For a list of options available for each theme, see the +# documentation. +# +# html_theme_options = {} + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + + +# -- Options for HTMLHelp output --------------------------------------- + +# Output file base name for HTML help builder. +htmlhelp_basename = 'soroush_python_sdkdoc' + + +# -- Options for LaTeX output ------------------------------------------ + +latex_elements = { + # The paper size ('letterpaper' or 'a4paper'). + # + # 'papersize': 'letterpaper', + + # The font size ('10pt', '11pt' or '12pt'). + # + # 'pointsize': '10pt', + + # Additional stuff for the LaTeX preamble. + # + # 'preamble': '', + + # Latex figure (float) alignment + # + # 'figure_align': 'htbp', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass +# [howto, manual, or own class]). +latex_documents = [ + (master_doc, 'soroush_python_sdk.tex', + u'Soroush Python SDK Documentation', + u'Ali Nikneshan', 'manual'), +] + + +# -- Options for manual page output ------------------------------------ + +# One entry per manual page. List of tuples +# (source start file, name, description, authors, manual section). +man_pages = [ + (master_doc, 'soroush_python_sdk', + u'Soroush Python SDK Documentation', + [author], 1) +] + + +# -- Options for Texinfo output ---------------------------------------- + +# Grouping the document tree into Texinfo files. List of tuples +# (source start file, target name, title, author, +# dir menu entry, description, category) +texinfo_documents = [ + (master_doc, 'soroush_python_sdk', + u'Soroush Python SDK Documentation', + author, + 'soroush_python_sdk', + 'One line description of project.', + 'Miscellaneous'), +] + + + diff --git a/docs/contributing.rst b/docs/contributing.rst new file mode 100644 index 0000000..e582053 --- /dev/null +++ b/docs/contributing.rst @@ -0,0 +1 @@ +.. include:: ../CONTRIBUTING.rst diff --git a/docs/history.rst b/docs/history.rst new file mode 100644 index 0000000..2506499 --- /dev/null +++ b/docs/history.rst @@ -0,0 +1 @@ +.. include:: ../HISTORY.rst diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..12996f8 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,20 @@ +Welcome to Soroush Python SDK's documentation! +============================================== + +.. toctree:: + :maxdepth: 2 + :caption: Contents: + + readme + installation + usage + modules + contributing + authors + history + +Indices and tables +================== +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` diff --git a/docs/installation.rst b/docs/installation.rst new file mode 100644 index 0000000..03142c0 --- /dev/null +++ b/docs/installation.rst @@ -0,0 +1,51 @@ +.. highlight:: shell + +============ +Installation +============ + + +Stable release +-------------- + +To install Soroush Python SDK, run this command in your terminal: + +.. code-block:: console + + $ pip install soroush_python_sdk + +This is the preferred method to install Soroush Python SDK, as it will always install the most recent stable release. + +If you don't have `pip`_ installed, this `Python installation guide`_ can guide +you through the process. + +.. _pip: https://pip.pypa.io +.. _Python installation guide: http://docs.python-guide.org/en/latest/starting/installation/ + + +From sources +------------ + +The sources for Soroush Python SDK can be downloaded from the `Github repo`_. + +You can either clone the public repository: + +.. code-block:: console + + $ git clone git://github.com/alinik/soroush_python_sdk + +Or download the `tarball`_: + +.. code-block:: console + + $ curl -OL https://github.com/alinik/soroush_python_sdk/tarball/master + +Once you have a copy of the source, you can install it with: + +.. code-block:: console + + $ python setup.py install + + +.. _Github repo: https://github.com/alinik/soroush_python_sdk +.. _tarball: https://github.com/alinik/soroush_python_sdk/tarball/master diff --git a/docs/make.bat b/docs/make.bat new file mode 100644 index 0000000..8503399 --- /dev/null +++ b/docs/make.bat @@ -0,0 +1,36 @@ +@ECHO OFF + +pushd %~dp0 + +REM Command file for Sphinx documentation + +if "%SPHINXBUILD%" == "" ( + set SPHINXBUILD=python -msphinx +) +set SOURCEDIR=. +set BUILDDIR=_build +set SPHINXPROJ=soroush_python_sdk + +if "%1" == "" goto help + +%SPHINXBUILD% >NUL 2>NUL +if errorlevel 9009 ( + echo. + echo.The Sphinx module was not found. Make sure you have Sphinx installed, + echo.then set the SPHINXBUILD environment variable to point to the full + echo.path of the 'sphinx-build' executable. Alternatively you may add the + echo.Sphinx directory to PATH. + echo. + echo.If you don't have Sphinx installed, grab it from + echo.http://sphinx-doc.org/ + exit /b 1 +) + +%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% +goto end + +:help +%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% + +:end +popd diff --git a/docs/modules.rst b/docs/modules.rst new file mode 100644 index 0000000..704fc1e --- /dev/null +++ b/docs/modules.rst @@ -0,0 +1,7 @@ +soroush_python_sdk +================== + +.. toctree:: + :maxdepth: 4 + + soroush_python_sdk diff --git a/docs/readme.rst b/docs/readme.rst new file mode 100644 index 0000000..72a3355 --- /dev/null +++ b/docs/readme.rst @@ -0,0 +1 @@ +.. include:: ../README.rst diff --git a/docs/soroush_python_sdk.rst b/docs/soroush_python_sdk.rst new file mode 100644 index 0000000..255dd33 --- /dev/null +++ b/docs/soroush_python_sdk.rst @@ -0,0 +1,22 @@ +soroush\_python\_sdk package +============================ + +Submodules +---------- + +soroush\_python\_sdk.soroush\_python\_sdk module +------------------------------------------------ + +.. automodule:: soroush_python_sdk.soroush_python_sdk + :members: + :undoc-members: + :show-inheritance: + + +Module contents +--------------- + +.. automodule:: soroush_python_sdk + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/usage.rst b/docs/usage.rst new file mode 100644 index 0000000..253c6c7 --- /dev/null +++ b/docs/usage.rst @@ -0,0 +1,7 @@ +===== +Usage +===== + +To use Soroush Python SDK in a project:: + + import soroush_python_sdk diff --git a/examples/download_file.py b/examples/download_file.py index 90f5e51..b5c8dc0 100644 --- a/examples/download_file.py +++ b/examples/download_file.py @@ -1,6 +1,4 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client bot_token = 'your bot token' bot = Client(bot_token) @@ -17,4 +15,4 @@ print('error in downloading file: {}' .format(error)) except Exception as e: - print(e.args[0]) \ No newline at end of file + print(e.args[0]) diff --git a/examples/echo_bot.py b/examples/echo_bot.py index ab874a6..cf2a8f8 100644 --- a/examples/echo_bot.py +++ b/examples/echo_bot.py @@ -1,6 +1,4 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client bot_token = 'your bot token' bot = Client(bot_token) diff --git a/examples/get_messages.py b/examples/get_messages.py index 793f6c5..f1b0b58 100644 --- a/examples/get_messages.py +++ b/examples/get_messages.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + bot_token = 'your bot token' @@ -12,4 +11,4 @@ print("New message from {} \nType: {}\nBody: {}" .format(message['from'], message['type'], message['body'])) except Exception as e: - print(e.args[0]) \ No newline at end of file + print(e.args[0]) diff --git a/examples/send_attachment.py b/examples/send_attachment.py index 7710ae8..67540a1 100644 --- a/examples/send_attachment.py +++ b/examples/send_attachment.py @@ -1,8 +1,7 @@ -from sys import path -path.append('..') -from client import Client -from os.path import getsize import ntpath +from os.path import getsize + +from soroush_python_sdk import Client bot_token = 'your bot token' diff --git a/examples/send_change_keyboard.py b/examples/send_change_keyboard.py index 441e483..aa111c2 100644 --- a/examples/send_change_keyboard.py +++ b/examples/send_change_keyboard.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + bot_token = 'your bot token' diff --git a/examples/send_image.py b/examples/send_image.py index 964f80c..71bf0b6 100644 --- a/examples/send_image.py +++ b/examples/send_image.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + from os.path import getsize import ntpath diff --git a/examples/send_location.py b/examples/send_location.py index a9d54ea..fa1058c 100644 --- a/examples/send_location.py +++ b/examples/send_location.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + bot_token = 'your bot token' diff --git a/examples/send_text.py b/examples/send_text.py index 0ce16b3..2f8a8c7 100644 --- a/examples/send_text.py +++ b/examples/send_text.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + bot_token = 'your bot token' diff --git a/examples/send_text_with_keyboard.py b/examples/send_text_with_keyboard.py index 57172f0..3f658cd 100644 --- a/examples/send_text_with_keyboard.py +++ b/examples/send_text_with_keyboard.py @@ -1,7 +1,5 @@ -from sys import path +from soroush_python_sdk import Client -path.append('..') -from client import Client bot_token = 'your bot token' diff --git a/examples/send_video.py b/examples/send_video.py index e7a7769..686c848 100644 --- a/examples/send_video.py +++ b/examples/send_video.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + from os.path import getsize import ntpath diff --git a/examples/send_voice.py b/examples/send_voice.py index c370303..332b380 100644 --- a/examples/send_voice.py +++ b/examples/send_voice.py @@ -1,6 +1,5 @@ -from sys import path -path.append('..') -from client import Client +from soroush_python_sdk import Client + from os.path import getsize import ntpath diff --git a/requirements.txt b/requirements.txt index a944a1c..5e6da72 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -requests -sseclient-py +requests==2.21.0 +sseclient-py==1.7 diff --git a/requirements_dev.txt b/requirements_dev.txt new file mode 100644 index 0000000..2433d0b --- /dev/null +++ b/requirements_dev.txt @@ -0,0 +1,13 @@ +pip==18.1 +bumpversion==0.5.3 +wheel==0.32.2 +watchdog==0.9.0 +flake8==3.6.0 +coverage==4.5.3 +tox==3.8.6 +coverage==4.5.1 +Sphinx==2.0.1 +twine==1.12.1 + +pytest==4.4.0 +pytest-runner==4.2 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..1ff8222 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,21 @@ +[bumpversion] +current_version = 0.2.4 +commit = True +tag = True + +[bumpversion:file:setup.py] + +[bumpversion:file:soroush_python_sdk/__init__.py] + +[bdist_wheel] +universal = 1 + +[flake8] +exclude = docs + +[aliases] +test = pytest + +[tool:pytest] +collect_ignore = ['setup.py'] + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0295fec --- /dev/null +++ b/setup.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""The setup script.""" + +from setuptools import setup, find_packages + +with open('README.rst') as readme_file: + readme = readme_file.read() + +with open('HISTORY.rst') as history_file: + history = history_file.read() + +requirements = [] + +setup_requirements = ['pytest-runner', ] + +test_requirements = ['pytest', ] + +setup( + author="Ali Nikneshan", + author_email='ali@nikneshan.com', + classifiers=[ + 'Development Status :: 2 - Pre-Alpha', + 'Intended Audience :: Developers', + 'License :: OSI Approved :: MIT License', + 'Natural Language :: English', + "Programming Language :: Python :: 2", + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + ], + description="api to access soroush social network sdk", + install_requires=requirements, + license="MIT license", + long_description=readme + '\n\n' + history, + include_package_data=True, + keywords='soroush_python_sdk', + name='soroush_python_sdk', + packages=find_packages(include=['soroush_python_sdk']), + setup_requires=setup_requirements, + test_suite='tests', + tests_require=test_requirements, + url='https://github.com/alinik/soroush_python_sdk', + version='0.2.4', + zip_safe=False, +) diff --git a/soroush_python_sdk/__init__.py b/soroush_python_sdk/__init__.py new file mode 100644 index 0000000..b942288 --- /dev/null +++ b/soroush_python_sdk/__init__.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +"""Top-level package for Soroush Python SDK.""" + +__author__ = """Ali Nikneshan""" +__email__ = 'ali@nikneshan.com' +__version__ = '0.2.4' + +from .soroush_python_sdk import Client diff --git a/client.py b/soroush_python_sdk/soroush_python_sdk.py similarity index 89% rename from client.py rename to soroush_python_sdk/soroush_python_sdk.py index 92d71ea..0b0f75c 100644 --- a/client.py +++ b/soroush_python_sdk/soroush_python_sdk.py @@ -1,25 +1,30 @@ +# -*- coding: utf-8 -*- + +"""Main module.""" import requests import sseclient import json import os +from time import sleep class Client: + HEADERS = {'Content-Type': 'Application/json', 'Accept': 'Application/json'} + BASE_URL = 'https://bot.sapp.ir/' + GET_MESSAGE_URL = '/getMessage' + SEND_MESSAGE_URL = '/sendMessage' + DOWNLOAD_FILE_URL = '/downloadFile/' + UPLOAD_FILE_URL = '/uploadFile' + RETRY_DELAY = 10 def __init__(self, token): - self.base_url = 'https://bot.sapp.ir/' - self.get_message_url = '/getMessage' - self.send_message_url = '/sendMessage' - self.download_file_url = '/downloadFile/' - self.upload_file_url = '/uploadFile' self.token = token - self.retry_delay = 3 def get_upload_file_url(self): if not self.token: raise ValueError('Invalid bot token') - return self.base_url + self.token + self.upload_file_url + return self.BASE_URL + self.token + self.UPLOAD_FILE_URL def get_download_file_url(self, file_url): if not self.token: @@ -27,39 +32,41 @@ def get_download_file_url(self, file_url): if not file_url: raise ValueError('Invalid file url') - return self.base_url + self.token + self.download_file_url + file_url + return self.BASE_URL + self.token + self.DOWNLOAD_FILE_URL + file_url def get_messages(self): if not self.token: raise ValueError('Invalid bot token') - url = self.base_url + self.token + self.get_message_url + url = self.BASE_URL + self.token + self.GET_MESSAGE_URL - response = requests.get(url, stream=True) - if 'Content-Type' in response.headers: - client = sseclient.SSEClient(response) + while True: + response = requests.get(url, stream=True) + if 'Content-Type' in response.headers: + client = sseclient.SSEClient(response) - for event in client.events(): - try: - message_event = json.loads(event.data) - yield message_event - except Exception as e: - print(e.args[0]) - continue - else: - raise ValueError('Invalid bot token') + for event in client.events(): + try: + message_event = json.loads(event.data) + yield message_event + except Exception as e: + print(e.args[0]) + continue + else: + print('Invalid bot token OR Invalid connection response from server') + + sleep(self.RETRY_DELAY) def send_message(self, post_data): if not self.token: raise ValueError('Invalid bot token') - url = self.base_url + self.token + self.send_message_url - headers = {'Content-Type': 'Application/json', 'Accept': 'Application/json'} + url = self.BASE_URL + self.token + self.SEND_MESSAGE_URL post_data = json.dumps(post_data, separators=(',', ':')) try: - response = requests.post(url, post_data, headers=headers) + response = requests.post(url, post_data, headers=self.HEADERS) if response: response_json = json.loads(response.text) @@ -218,7 +225,8 @@ def change_keyboard(self, to, keyboard): return self.send_message(post_data) - def make_keyboard(self, keyboard_data): + @staticmethod + def make_keyboard(keyboard_data): keyboard = [] if isinstance(keyboard_data, str): @@ -338,5 +346,3 @@ def upload_file(self, file_path): return ['Bad Response: ' + str(response.status_code) + ' status code', False] except Exception as e: return [e.args[0], False] - - diff --git a/tests/test_soroush_python_sdk.py b/tests/test_soroush_python_sdk.py new file mode 100644 index 0000000..9ce6ebd --- /dev/null +++ b/tests/test_soroush_python_sdk.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +"""Tests for `soroush_python_sdk` package.""" + +import pytest + + +from soroush_python_sdk import soroush_python_sdk + + +@pytest.fixture +def response(): + """Sample pytest fixture. + + See more at: http://doc.pytest.org/en/latest/fixture.html + """ + # import requests + # return requests.get('https://github.com/audreyr/cookiecutter-pypackage') + + +def test_content(response): + """Sample pytest test function with the pytest fixture as an argument.""" + # from bs4 import BeautifulSoup + # assert 'GitHub' in BeautifulSoup(response.content).title.string diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..69d85eb --- /dev/null +++ b/tox.ini @@ -0,0 +1,28 @@ +[tox] +envlist = py27, py34, py35, py36, flake8 + +[travis] +python = + 3.6: py36 + 3.5: py35 + 3.4: py34 + 2.7: py27 + +[testenv:flake8] +basepython = python +deps = flake8 +commands = flake8 soroush_python_sdk + +[testenv] +setenv = + PYTHONPATH = {toxinidir} +deps = + -r{toxinidir}/requirements_dev.txt +; If you want to make tox run the tests with the same versions, create a +; requirements.txt with the pinned versions and uncomment the following line: +; -r{toxinidir}/requirements.txt +commands = + pip install -U pip + py.test --basetemp={envtmpdir} + +