Reputation:
I followed this tutorial to create a Telegram bot in Python. In the end, I ran it locally on my machine with ngrok. To test the bot I sent messages to it in Telegram and it worked, so that was a good tutorial.
However, now I want to host the bot on the cloud because I certainly don't want to have my PC always turned on with a terminal running.
I've been reading many tutorials on the web on how to host a serverless Telegram bot. I've tried hosting it in AWS Lambda, Heroku, Google Cloud Platform and Glitch.com. But I still haven't managed to host it successfully. There was always something that prevented me from hosting it: either my code was not properly accepted, the tutorial was not descriptive enough or I was not understanding something important.
When following tutorials I sometimes had to adapt my code to the platform I was trying to host the bot in. I still couldn't make it work.
What am I doing wrong? How can I successfully host my Telegram bot written in Python on the cloud (for free)?
This is my code:
import requests
import os
from bottle import Bottle, response, request as bottle_request
from unidecode import unidecode
# get credentials
bot_url = os.environ['BOT_URL']
class BotChangei:
def get_chat_id(self, data):
chat_id = data['message']['chat']['id']
return chat_id
def get_message(self, data):
message_text = data['message']['text']
return message_text
def send_message(self, prepared_data):
"""
Prepared data should be json which includes at least `chat_id` and `text`
"""
message_url = self.bot_url + 'sendMessage'
requests.post(message_url, json=prepared_data)
class TelegramBot(BotChangei, Bottle):
def __init__(self, *args, **kwargs):
super(TelegramBot, self).__init__()
self.route('/', callback=self.post_handler, method="POST")
def is_vowel(self, letter):
return unidecode(letter) in {'a', 'e', 'o', 'u'}
def is_capital(self, letter):
return unidecode(letter) in {'A', 'E', 'O', 'U'}
def change_text_message(self, text):
mutable_list = list(text)
i = 0
for letter in mutable_list:
if self.is_vowel(letter):
mutable_list[i] = 'i'
elif self.is_capital(letter):
mutable_list[i] = 'I'
i += 1
return "".join(mutable_list)
def prepare_data_for_answer(self, data):
message = self.get_message(data)
answer = self.change_text_message(message)
chat_id = self.get_chat_id(data)
json_data = {
"chat_id": chat_id,
"text": answer,
}
return json_data
def post_handler(self):
data = bottle_request.json
answer_data = self.prepare_data_for_answer(data)
self.send_message(answer_data)
return response
if __name__ == '__main__':
app = TelegramBot()
app.run(host='localhost', port=8080)
Upvotes: 4
Views: 7117
Reputation: 512
Google Cloud gives your $300 free with sign-up. I'm not familiar with ngrok, but I would try creating a google cloud account then:
You could do the same with with AWS EC2 (not lambda) - just make sure you pick a 'free tier' machine image - AWS gives your 750 free hours of 'free tier' EC2 instances per month.
Not sure how familiar you are with cloud computing but you would definitely want to look at AWS EC2 or Google Cloud's 'Compute Engine'. Those allow you to run a literal computer in the cloud that you have complete control over. SSH'ing into the machine is usually the 'gotcha' task if you've never done it before. Once you're SSH'd in, you can install / run anything you want.
SSH instructions for AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html
For Google, once you launch the VM, you can click 'SSH' next to it and it will allow you to SSH in right in the web browser (with AWS you have to download the private key used to launch the EC2, then SSH in from your local terminal).
Upvotes: 3
Reputation: 181280
You could leave it running on a Machine you have in your home, and use a service such as ngrok (my favorite). It has a free tier that might work for you.
Upvotes: 0