如何在云上成功托管我用 Python 编写的 Telegram 机器人(免费)? [英] How can I successfully host my Telegram bot written in Python on the cloud (for free)?

查看:58
本文介绍了如何在云上成功托管我用 Python 编写的 Telegram 机器人(免费)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了本教程 在 Python 中创建一个 Telegram 机器人.最后,我使用 ngrok 在我的机器上本地运行它.为了测试机器人,我在 Telegram 中向它发送了消息,它工作了,这是一个很好的教程.

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.

我在网上阅读了许多关于如何托管无服务器 Telegram 机器人的教程.我尝试在 AWS Lambda、Heroku、Google Cloud Platform 和 Glitch.com 中托管它.但是我仍然没有成功地托管它.总是有一些事情阻止我托管它:要么我的代码没有被正确接受,教程描述性不够,要么我没有理解一些重要的东西.

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.

我做错了什么?如何在云端(免费)成功托管我用 Python 编写的 Telegram 机器人?

这是我的代码:

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)

推荐答案

Google Cloud 注册即可免费获得 300 美元.我不熟悉 ngrok,但我会尝试创建一个谷歌云帐户:

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:

  1. 启动Compute Engine"虚拟机实例
  2. 通过 SSH 连接到机器,安装您的依赖项
  3. git clone(或任何你想做的事情)来获取您在机器上的项目并从命令行运行它

您可以对 AWS EC2(而不是 lambda)执行相同的操作 - 只需确保选择免费套餐"机器映像即可 - AWS 每月为您提供 750 个免费小时的免费套餐"EC2 实例.

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.

不确定您对云计算有多熟悉,但您肯定想了解 AWS EC2 或 Google Cloud 的计算引擎".这些允许您在云中运行您可以完全控制的文字计算机.如果您以前从未这样做过,SSH 进入机器通常是陷阱"任务.进入 SSH 后,您可以安装/运行任何您想要的东西.

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.

AWS SSH 说明:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

SSH instructions for AWS: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AccessingInstancesLinux.html

对于 Google,一旦您启动 VM,您可以单击它旁边的SSH",它将允许您直接在 Web 浏览器中进行 SSH(使用 AWS,您必须下载用于启动 EC2 的私钥,然后从本地终端通过 SSH 登录).

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).

这篇关于如何在云上成功托管我用 Python 编写的 Telegram 机器人(免费)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆