├── .gitattributes ├── LICENSE ├── README.md └── workers.js /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 azhuge233 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Teleflare-Messenger 2 | Send Telegram bot messages with Cloudflare workers 3 | 4 | ## Usage 5 | 6 | 1. Create a cloudflare workers 7 | 8 | 2. Copy the code in workers.js to the workers 9 | 10 | 3. Fill in your Telegram Bot Token and your Chat ID 11 | 12 | 4. Send GET request to your workers URL 13 | 14 | ``` 15 | https://yourworkersname.yourcloudflarename.workers.dev/?title=Title&msg=test_message 16 | ``` 17 | 18 | **Note**: The script uses [MarkdownV2](https://core.telegram.org/bots/api#markdownv2-style) as default parse mode, if you want to use plain text or HTML, you need to modify the title bold symbol(**) at line 46. 19 | -------------------------------------------------------------------------------- /workers.js: -------------------------------------------------------------------------------- 1 | const OPT = { 2 | BotToken: '', // Telegram Bot Token 3 | ChatID:'', // User ChatID 4 | ParseMode: 'markdownv2' //keep blank, html, markdown or markdownv2 5 | } 6 | 7 | addEventListener('fetch', event => { 8 | event.respondWith(handleRequest(event.request)) 9 | }) 10 | 11 | async function handleRequest(request) { 12 | let url = new URL(request.url); 13 | 14 | let title = url.searchParams.get('title'); 15 | let msg = url.searchParams.get('msg'); 16 | 17 | if (title == null || title == "") { 18 | return new Response("No Title provided.", { 19 | status: 200 20 | }) 21 | } 22 | 23 | if (msg == null || msg == "") { 24 | return new Response("No Message provided.", { 25 | status: 200 26 | }) 27 | } 28 | 29 | if(msg.errcode){ 30 | return new Response(JSON.stringify(msg), { 31 | status: 200, 32 | headers:{ 33 | 'content-type':'application/json; charset=UTF-8' 34 | } 35 | }) 36 | } 37 | 38 | return await sendMessage(title, msg); 39 | } 40 | 41 | async function sendMessage(title, msg){ 42 | let url = "https://api.telegram.org/"; 43 | url += "bot" + OPT.BotToken + "/sendMessage?"; 44 | url += "chat_id=" + OPT.ChatID + "&"; 45 | url += "parse_mode=" + OPT.ParseMode + "&"; 46 | url += "text=*" + title + "*%0A%0A"; 47 | url += msg; 48 | 49 | return fetch(url ,{ 50 | method:'get', 51 | headers: { 52 | 'Accept': 'text/html,application/xhtml+xml,application/xml;', 53 | 'Accept-Encoding': 'gzip, deflate, br', 54 | 'User-Agent': 'Mozilla/5.0 Chrome/90.0.4430.72' 55 | } 56 | }); 57 | } 58 | --------------------------------------------------------------------------------