Skip to content

Commit 44531cd

Browse files
committed
Added Flask Google Cloud bot's example
1 parent 3e82e69 commit 44531cd

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
env/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
runtime: python38
2+
3+
env_variables:
4+
BUCKET_NAME: "your-google-application"
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
'''
2+
Simple bot for Google cloud deployment.
3+
4+
Docs:
5+
https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-python-service
6+
7+
1. Receive your bot's token from https://t.me/BotFather
8+
9+
2. Create a Google Cloud project. https://cloud.google.com/resource-manager/docs/creating-managing-projects
10+
11+
3. Install the Google Cloud CLI. https://cloud.google.com/sdk/docs/install
12+
13+
4. Move to telegram_google_cloud_bot folder
14+
15+
cd telegram_google_cloud_bot/
16+
17+
5. Initialize the gcloud CLI:
18+
19+
gcloud init
20+
21+
6. To set the default project for your Cloud Run service:
22+
23+
gcloud config set project PROJECT_ID
24+
25+
7. Deploy:
26+
27+
gcloud run deploy
28+
'''
29+
30+
import os
31+
32+
from flask import Flask, request
33+
34+
import telebot
35+
36+
TOKEN = 'token_from_botfather'
37+
38+
bot = telebot.TeleBot(TOKEN)
39+
40+
app = Flask(__name__)
41+
42+
43+
@bot.message_handler(commands=['start'])
44+
def start(message):
45+
bot.reply_to(message, 'Hello, ' + message.from_user.first_name)
46+
47+
48+
@bot.message_handler(func=lambda message: True, content_types=['text'])
49+
def echo_message(message):
50+
bot.reply_to(message, message.text)
51+
52+
53+
@app.route('/' + TOKEN, methods=['POST'])
54+
def getMessage():
55+
json_string = request.get_data().decode('utf-8')
56+
update = telebot.types.Update.de_json(json_string)
57+
bot.process_new_updates([update])
58+
return '!', 200
59+
60+
61+
@app.route('/')
62+
def webhook():
63+
bot.remove_webhook()
64+
bot.set_webhook(url='https://mydomain.com/' + TOKEN)
65+
return '!', 200
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
certifi==2022.12.7
2+
charset-normalizer==3.1.0
3+
click==8.0.3
4+
Flask==2.0.2
5+
idna==3.4
6+
importlib-metadata==6.4.1
7+
itsdangerous==2.0.1
8+
Jinja2==3.0.3
9+
MarkupSafe==2.0.1
10+
pyTelegramBotAPI==4.11.0
11+
requests==2.28.2
12+
urllib3==1.26.15
13+
Werkzeug==2.0.2
14+
zipp==3.15.0

0 commit comments

Comments
 (0)