Skip to content

Instantly share code, notes, and snippets.

@rabbitix
Created January 11, 2021 12:03
Show Gist options
  • Save rabbitix/b2b93ca35e9aaaefe0a38c79e3ca46a0 to your computer and use it in GitHub Desktop.
Save rabbitix/b2b93ca35e9aaaefe0a38c79e3ca46a0 to your computer and use it in GitHub Desktop.
PTB init bot with proxy
import logging
from telegram.ext import (Updater, Dispatcher, ConversationHandler, CommandHandler,
MessageHandler, RegexHandler, Filters,
CallbackContext,)
from telegram import Update, KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove, ReplyMarkup
import requests
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG
)
logger = logging.getLogger(__name__)
def error(update, errorMsg):
logger.warning('Update "%s" caused "%s" ', update, errorMsg)
class Bot:
def __init__(self, token):
REQ = {
'proxy_url': 'socks5h://127.0.0.1:9050',
'urllib3_proxy_kwargs': {
'username': '1',
'password': '1',
},
'connect_timeout': 200,
}
self.updater = Updater(
token=token, use_context=True, request_kwargs=REQ)
self.dispatcher = self.updater.dispatcher
self.dispatcher.add_error_handler(error)
start = CommandHandler('start', self.start)
self.dispatcher.add_handler(start)
def start(self, update: Update, context: CallbackContext):
update.message.reply_text('hi')
def run(self):
self.updater.start_polling()
self.updater.idle()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment