├── README.md └── app.py /README.md: -------------------------------------------------------------------------------- 1 | # Host your bot on pythonanywhere 2 | You can run your telegram bot on PAW (pythonanywhere) for **0$**. If you choose webhook method instead of long pooling, the entire bot can be run as a flask website which is gonna be online for 3 month (Your site can be run indefinitely but to avoid having the site offline, you need to log in to your account and extend it every three months). Copy this sample to your space, place your PAW username and telegram bot API, and finally reload your website. 3 | **PAW provides SSL for all subdomians so you don't have to buy any certificate.** 4 | 5 | ## Dependencies 6 | ```` 7 | pip3.6 install --user telepot 8 | ```` 9 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import telepot 2 | import re 3 | import os 4 | import random 5 | import string 6 | import time 7 | from flask import Flask, render_template, session, url_for, redirect, request 8 | from telepot.namedtuple import * 9 | 10 | USERNAME = "PYTHONANYWHERE_USERNAME" 11 | TOKEN = "TELEGRAM_BOT_API" 12 | SECRET = ''.join(random.choice(string.ascii_letters) for x in range(20)) 13 | URL = f"https://{USERNAME}.pythonanywhere.com/{SECRET}" 14 | 15 | telepot.api.set_proxy('http://proxy.server:3128') 16 | bot = telepot.Bot(TOKEN) 17 | bot.setWebhook(URL, max_connections=10) 18 | 19 | def processing(msg): 20 | if 'chat' in msg and msg['chat']['type'] == 'channel': 21 | return 22 | 23 | id = msg['from']['id'] 24 | 25 | if 'text' in msg: 26 | msg['text'] = str(msg['text']) # FEELING SAFER ;) 27 | msg['type'] = 'text' 28 | 29 | elif 'data' in msg: 30 | msg['type'] = 'callback' 31 | msg['text'] = f"%callback {msg['data']}" 32 | 33 | else: 34 | msg['type'] = 'nontext' 35 | types = ['audio', 'voice', 'document', 'photo', 36 | 'video', 'contact', 'location'] 37 | 38 | for type in types: 39 | if type in msg: 40 | msg['text'] = f'%{type}' 41 | break 42 | 43 | if 'text' in msg: 44 | for entry in regex: 45 | if re.match(entry, msg["text"]): 46 | matches = re.match(entry, msg["text"]).groups() 47 | parser(msg, list(matches)) 48 | return 49 | 50 | 51 | app = Flask(__name__) 52 | 53 | @app.route(f'/{SECRET}', methods=["POST"]) 54 | def webhook(): 55 | update = request.get_json() 56 | if "message" in update: 57 | processing(update['message']) 58 | 59 | elif 'callback_query' in update: 60 | processing(update['callback_query']) 61 | 62 | return 'OK' 63 | 64 | regex = [ 65 | r'^[!/](start)', 66 | r'^[!/](echo) (.*)' 67 | ] 68 | 69 | def parser(msg, matches): 70 | usr = msg['from'] 71 | 72 | if msg['type'] == "text": 73 | if matches[0] == 'start': 74 | text = "welcome message" 75 | markup = InlineKeyboardMarkup(inline_keyboard=[ 76 | [InlineKeyboardButton(text='BTN1', callback_data='btn1'), 77 | InlineKeyboardButton(text='BTN2', callback_data='btn2')] 78 | ]) 79 | 80 | bot.sendMessage(usr['id'], text, reply_markup=markup) 81 | return 82 | 83 | if matches[0] == 'echo' and matches[1]: 84 | bot.sendMessage(usr['id'], matches[1]) 85 | return 86 | 87 | if __name__ == "__main__": 88 | app.run() 89 | --------------------------------------------------------------------------------