Skip to content

almightyseir/pythoncfworker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ꜱɪᴍᴘʟᴇ ᴄᴏᴅᴇ ꜰᴏʀ ᴛɢʙᴏᴛ ɪɴ ᴄꜰ ᴡᴏʀᴋᴇʀ ᴜꜱɪɴɢ ᴘʏᴛʜᴏɴ #

from js import Response, Headers, fetch, console
from urllib.parse import urlparse
import json

token = "" #or use env,declare them in wrangler.toml
async def send_message(chat_id, text):
    url = f"https://api.telegram.org/bot{token}/sendMessage?chat_id={chat_id}&text={text}"
    await fetch(url)

async def on_fetch(request):
    url = urlparse(request.url)
    if url.path == "/bot" and request.method == 'POST':
        request_body = await request.text()
        tg_json = json.loads(request_body)
        if "message" in tg_json:
            chat_id = tg_json["message"]["chat"]["id"]
            if tg_json["message"]["text"] == "/start":
                firstname = tg_json["message"]["from"]["first_name"]
                text = f"Hey {firstname} ❣️!\nThis is a simple telegram bot made in cf workers using python😁"
                await send_message(chat_id, text)
                return Response.new("OK...", {"status": 200})
    elif url.path == "/":
        return Response.new("Python Worker :)", {"status": 200})

    return Response.new("Invalid request", {"status": 400})
#



   

About

Template for Telegram Bot on cf worker using python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages