Repository: python-telegram-bot/python-telegram-bot

Issue:
Compatibility with GAE
<!--
Thanks for reporting issues of python-telegram-bot!
To make it easier for us to help you please enter detailed information below.
-->
### Steps to reproduce
1. Upgrade to latest pip version
2. Upload and run application to GAE with sample code
3. Get errors

Seems that latest update broke some kind of compatibility with Google App Engine, as it doesn't support threading and so on.
### Configuration

**Operating System:**
Google App Engine 

version: 1
runtime: python27
api_version: 1
threadsafe: yes

**Version of Python:**

2.7

`$ python -V`

**Version of python-telegram-bot:**

Latest from pip.

`$ python -c 'import telegram; print(telegram.__version__)'`

python_telegram_bot-4.3.1-py2.py3-none-any.whl
### Logs

Insert logs here (if necessary)

`mws-114:xxx-1333 user$ pip install -U python-telegram-bot -t lib
Collecting python-telegram-bot
  Using cached python_telegram_bot-4.3.1-py2.py3-none-any.whl
Collecting urllib3>=1.10 (from python-telegram-bot)
  Using cached urllib3-1.16-py2.py3-none-any.whl
Collecting certifi (from python-telegram-bot)
  Using cached certifi-2016.2.28-py2.py3-none-any.whl
Collecting future>=0.15.2 (from python-telegram-bot)
Installing collected packages: urllib3, certifi, future, python-telegram-bot
Successfully installed certifi future-0.15.2 python-telegram-bot-4.1 urllib3`

`2a02:8109:a4bf:ebc4:950b:8c3f:2d5a:b4e2 - - [03/Jul/2016:04:32:37 -0700] "GET /favicon.ico HTTP/1.1" 500 0 "http://example.ru/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
    3:1467545557.211349 
    : Traceback (most recent call last):
    :   File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    :     handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
    :   File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    :     handler, path, err = LoadObject(self._handler)
    :   File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    :     obj = __import__(path[0])
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/main.py", line 4, in <module>
    :     from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/telegram/ext/__init__.py", line 21, in <module>
    :     from .dispatcher import Dispatcher
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/telegram/ext/dispatcher.py", line 27, in <module>
    :     from future.builtins import range
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/builtins/__init__.py", line 10, in <module>
    :     from future.builtins.iterators import (filter, map, zip)
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/builtins/iterators.py", line 43, in <module>
    :     from future.types import newrange as range
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/types/__init__.py", line 243, in <module>
    :     from .newrange import newrange
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/types/newrange.py", line 25, in <module>
    :     from future.backports.misc import count   # with step parameter on Py2.6
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/backports/__init__.py", line 17, in <module>
    :     from .misc import (ceil,
    :   File "/base/data/home/apps/e~xxx-1333/1.393941255070644276/lib/future/backports/misc.py", line 900, in <module>
    :     from subprocess import check_output
    : ImportError: cannot import name check_output`



Discussion:
This is kinda confusing. We already imported (and used, in some cases) the `subprocess` library in `telegram.ext.Updater` in earlier versions. 

Also, in response to this: 

> Seems that latest update broke some kind of compatibility with Google App Engine, as it doesn't support threading and so on.

The `threading` library is used throughout `telegram.ext`. 

I'm unsure why it could have worked with GAE in the first place, as it's not really designed to do so (also see #290).

It'd be nice if you could give a bit more info here. Are you using a paid subscription? Were you using `telegram.ext` before, and if so, which parts of it (eg. did you use `start_polling` or `start_webhook`)?

Maybe we can make it fully compatible to GAE with the release of v5.

Sorry for late answer. I was setting up environment on DigitalOcean after failure with GAE.

> Are you using a paid subscription?

Yes. At least I have my card in my profile.

> Were you using telegram.ext before, and if so, which parts of it (eg. did you use start_polling or start_webhook)?

I was trying to use dispatcher with process_update. I never had my bot working on GAE. But before update at least script was starting without any errors.

Exactly the same error here, i cannot have bot with updater working with google app engine. My situation is the same as gaiar. Any suggestion?

GAE support only `urllib`, `urllib2`, or `httplib`
From 4.3 version (3076dfc0860062f5216ab5a03feb38b21a533b94) project move to `urllib3`.
Use 4.2 branch

Maybe this change is causing problems with GAE Socket API such as "FeatureNotEnabledError: The Socket API will be enabled for this application once billing has been enabled in the admin console."
I think that with the elder version such problems didn't occur.

A user of our library recently published a thing for this, perhaps you could give it a try:

https://github.com/FollonSaxBass/python-telegram-bot-GAE

Thank you very much!

> GAE support only urllib, urllib2, or httplib
> From 4.3 version (3076dfc) project move to urllib3.
> Use 4.2 branch

I can get 5.3.1 working on GAE with `urllib3` through this simple patch in [request.py](https://github.com/python-telegram-bot/python-telegram-bot/blob/da4ed9b316b6b76bab9f4b500a2faed533610d68/telegram/utils/request.py):
```patch
30a31
> import urllib3.contrib.appengine
74c75,78
<             mgr = urllib3.PoolManager(**kwargs)
---
>             if urllib3.contrib.appengine.is_appengine_sandbox():
>                 mgr = urllib3.contrib.appengine.AppEngineManager()
>             else:
>                 mgr = urllib3.PoolManager(**kwargs)
```
@whipermr5 1. Do you need to install some "extra" pip packages to make the import work?
2. Please open a PR for this change.
@tsnoam 1. Nope; it comes with standard `urllib3`. 2. Okay!

Resolving patch:
diff --git a/telegram/utils/request.py b/telegram/utils/request.py
--- a/telegram/utils/request.py
+++ b/telegram/utils/request.py
@@ -28,6 +28,7 @@
 
 import certifi
 import urllib3
+import urllib3.contrib.appengine
 from urllib3.connection import HTTPConnection
 from urllib3.util.timeout import Timeout
 try:
@@ -93,7 +94,11 @@ def __init__(self,
             proxy_url = os.environ.get('HTTPS_PROXY') or os.environ.get('https_proxy')
 
         if not proxy_url:
-            mgr = urllib3.PoolManager(**kwargs)
+            if urllib3.contrib.appengine.is_appengine_sandbox():
+                # Use URLFetch service if running in App Engine
+                mgr = urllib3.contrib.appengine.AppEngineManager()
+            else:
+                mgr = urllib3.PoolManager(**kwargs)
         else:
             kwargs.update(urllib3_proxy_kwargs)
             if proxy_url.startswith('socks'):


Test patch: