Jul 16, 2024
/newbotexample_youtube_video_bottelegram_example)main.py:
import os
from telegram import Bot
def telegram_bot(request):
bot = Bot(token=os.environ['TELEGRAM_TOKEN'])
if request.method == 'POST':
update = request.get_json()
chat_id = update['message']['chat']['id']
text = update['message']['text']
bot.send_message(chat_id=chat_id, text=text)
return 'OK'
.gitignore: Standard Python and PyCharm exclusion.gcloudignore: Exclude .gitignore, README.md, and other unnecessary files for deploymentrequirements.txt: Should include python-telegram-botgcloud functions deploy telegram_bot \
--runtime python38 \
--trigger-http \
--set-env-vars TELEGRAM_TOKEN=<your-api-key> \
--project=<your-project-id>
curl -F "url=https://<your-cloud-function-url>" https://api.telegram.org/bot<your-api-key>/setWebhook
main.py, modify the content returned by the bot:
def telegram_bot(request):
bot = Bot(token=os.environ['TELEGRAM_TOKEN'])
if request.method == 'POST':
update = request.get_json()
chat_id = update['message']['chat']['id']
text = update['message']['text'] + ' from the bot'
bot.send_message(chat_id=chat_id, text=text)
return 'test'