Forums

Telegram Bot

How a can deploy a telegram bot on pythonanywhere

Hi there,

There are people who have deployed a telegram bot on PythonAnywhere.

I would suggest reading this and this.

Thanks, in the links there are information for bot development have someone published a step by step guide to deploy a telegram on PythonAnywhere?

I don't think we've seen one, no.

Something strange here. I can't access telegram API. I tried this in console:

import telepot token = 'mytokennumbers' bot = telepot.Bot(token) bot.getMe()

It works on my local machine, but here i get an error: ... raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='api.telegram.org', port=443): Max retries exceeded with url: /bot<mytokennumbers>/getMe (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f35a6934750>: Failed to establish a new connection: [Errno 101] Network is unreachable',))

That looks like it's because you're not using the proxy. You'll need to configure bot to use the proxy server. The address is proxy.server on port 3128.

Many thanks! It works now.

python-telegram-bot is a pretty good API, I recommend using that.

Hi, I have the same connection problem into the bash. Can you explain me better how to solve it ? Many thanks!

How are you connecting over bash?

To use telepot from a PythonAnywhere free account, put this code at the top of your bot's code:

import telepot
import urllib3

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))

I've just published a detailed blog post explaining how to create a Telegram bot on PythonAnywhere.

Thanks guys, I've done! Thanks very much for the help, u r great! Now everything is running, it's my first bot and I'm still rookie :)

Great! Glad you got it working :-D

I need help I tried running the import telepot something but they keep telling me that "no Modula name" how can I get pass that one

You haven't installed the library or you're using a Python version that is different to the one that you installed the library into.

getting this error, maybe it is because i'm trying to use async version, but i have no idea how to fix that

AttributeError: 'ProxyManager' object has no attribute 'post'

Okay for aio version of telepot i should use something from aiohttp but last variant that worked (ProxyConnector) got deprecated and i don't know if there's any alternative for that

If you're running that in a web app, the async version will not work. Otherwise it should be ok.

Idk, i think i just need to write own async proxy transport so it can work, but i'm too lazy to read sources of 2 libraries . _.

What should I do when it shows ModuleNotFoundError: No module named 'telepot'?

You haven't installed telepot. You need to install telepot into the version of Python that you're using.

Can any one help me my first telegram bot want to deploy on pythonanywhere I do not know the procedure

Here's a blogpost we wrote about how to deploy a telegram bot.

nevermind

nevermind

how can I solve this problem?: ClientConnectorError: Cannot connect to host api.telegram.org:443 ssl:default

There are 2 things that are the most likely causes of that. The first is that you have not configured your bot library to use the proxy. Check the documentation for the library you're using to see how to do that and the details to use are available here. The second is that you are trying to use the websockets interface to telegram, which will not work from a free account on PythonAnywhere.

Here is a code example , libraries aiogram and aiohttp,websockets not use

    import telepot
import urllib3

proxy_url = "http://proxy.server:3128"
telepot.api._pools = {
    'default': urllib3.ProxyManager(proxy_url=proxy_url, num_pools=3, maxsize=10, retries=False, timeout=30),
}
telepot.api._onetime_pool_spec = (urllib3.ProxyManager, dict(proxy_url=proxy_url, num_pools=1, maxsize=1, retries=False, timeout=30))

import logging
import aiohttp

from aiogram import Bot, Dispatcher, executor, types

import exceptions
import expenses
from categories import Categories
from middlewares import AccessMiddleware


logging.basicConfig(level=logging.INFO)

API_TOKEN =""

ACCESS_ID =""

bot = Bot(
    token=API_TOKEN
          )
dp = Dispatcher(bot)
dp.middleware.setup(AccessMiddleware(ACCESS_ID))

How do you run your code? What is the full traceback of the error?

Hi all,

I have a telegram bot and I want my bot always runnig, how much CPU seconds I need? I don't know what kind of account I need to run my code.

There's no way for us to tell you that. It will depend entirely on your code. Your best bet is to run it and see how much CPU time it consumes.

Can i host my telegram bot on pythonAnywhere?

Sure, go ahead.

hello Glenn. I installed python-telegram-bot and I got a "requirement satisfied package installed successfully" message. However, when I run the code I get a "Module not found error". Is python-telegram-bot not supported on pythonanywhere? I just want to send a message, no replies, nothing else...just periodically send a message... The code works smoothly on my IDE(pycharm) with very little line of code(about 5 lines). Do I have to install another package?

Take a look at https://help.pythonanywhere.com/pages/DebuggingImportError/

please help and now is it possible to somehow launch a bot on pythonanywhere? I can't launch the bot for several days, please help.

Do you get an error? What is it?

I get this error: aiogram.utilities.exceptions.NetworkError: Io httpclient throws error: ClientConnectorError: Unable to connect to the host api.telegram.org:443 ssl:by default [Network unavailablŠµ

You need to configure aiogram to use the proxy. Check the documentation of aiogram for how to do that and the details you need to use are here: https://help.pythonanywhere.com/pages/403ForbiddenError/#proxy-details

and if I set it up , will asynchronous functions work for the bot ? or will it be necessary to remove asynchronous functions?

Yes, async functions should work.

Thank you very much!