Se1dhe
17.03.2019, 20:31
Все уже давно в курсе, что РКН пытается блочить ТГ на территории РФ. Сам клиент заблочить у них пока так и не получилось, а вот боты работать перестают.
Тут мы научим нашего бота работать через http proxy. Отдельное спасибо человеку с ником "Vyacheslav @bvn13"
Тело нашего пустого бота:
Java:
public
class
MyBot
extends
AbilityBot
{
protected
MyBot
(
String
botToken
,
String
botUsername
)
{
super
(
botToken
,
botUsername
)
;
}
public
int
creatorId
(
)
{
return
0
;
}
public
Ability
hello
(
)
{
return
Ability
.
builder
(
)
.
name
(
"test"
)
.
info
(
"hello bot"
)
.
locality
(
ALL
)
.
privacy
(
PUBLIC
)
.
action
(
ctx
->
silent
.
send
(
"hello!"
,
ctx
.
chatId
(
)
)
)
.
build
(
)
;
}
}
Регистрация и его запуск:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
Подключаем зависимость:
Java:
Maven
org
.
telegram
telegrambots
-
abilities
4.1
.2
Gradle
// telegram bot api
compile
'org.telegram:telegrambots:3.6.1'
Дальше пример реализации классов
В классе бота:
Java:
public
class
MyBot
extends
AbilityBot
{
protected
MyBot
(
String
botToken
,
String
botUsername
,
DefaultBotOptions
options
)
{
super
(
botToken
,
botUsername
,
options
)
;
}
/* ... */
}
В регистрации, если прокся без авторизации:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
private
static
String
PROXY_HOST
=
"..."
/* proxy host */
;
private
static
Integer
PROXY_PORT
=
3128
/* proxy port */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Set up Http proxy
DefaultBotOptions
botOptions
=
ApiContext
.
getInstance
(
DefaultBotOptions
.
class
)
;
HttpHost
httpHost
=
new
HttpHost
(
PROXY_HOST
,
PROXY_PORT
)
;
RequestConfig
requestConfig
=
RequestConfig
.
custom
(
)
.
setProxy
(
httpHost
)
.
setAuthenticationEnabled
(
false
)
.
build
(
)
;
botOptions
.
setRequestConfig
(
requestConfig
)
;
botOptions
.
setHttpProxy
(
httpHost
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
,
botOptions
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
С авторизацией:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
private
static
String
PROXY_HOST
=
"..."
/* proxy host */
;
private
static
Integer
PROXY_PORT
=
3128
/* proxy port */
;
private
static
String
PROXY_USER
=
"..."
/* proxy user */
;
private
static
String
PROXY_PASSWORD
=
"..."
/* proxy password */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Set up Http proxy
DefaultBotOptions
botOptions
=
ApiContext
.
getInstance
(
DefaultBotOptions
.
class
)
;
CredentialsProvider
credsProvider
=
new
BasicCredentialsProvider
(
)
;
credsProvider
.
setCredentials
(
new
AuthScope
(
PROXY_HOST
,
PROXY_PORT
)
,
new
UsernamePasswordCredentials
(
PROXY_USER
,
PROXY_PASSWORD
)
)
;
HttpHost
httpHost
=
new
HttpHost
(
PROXY_HOST
,
PROXY_PORT
)
;
RequestConfig
requestConfig
=
RequestConfig
.
custom
(
)
.
setProxy
(
httpHost
)
.
setAuthenticationEnabled
(
true
)
.
build
(
)
;
botOptions
.
setRequestConfig
(
requestConfig
)
;
botOptions
.
setCredentialsProvider
(
credsProvider
)
;
botOptions
.
setHttpProxy
(
httpHost
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
,
botOptions
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
Ну, собсно, все.
Тут мы научим нашего бота работать через http proxy. Отдельное спасибо человеку с ником "Vyacheslav @bvn13"
Тело нашего пустого бота:
Java:
public
class
MyBot
extends
AbilityBot
{
protected
MyBot
(
String
botToken
,
String
botUsername
)
{
super
(
botToken
,
botUsername
)
;
}
public
int
creatorId
(
)
{
return
0
;
}
public
Ability
hello
(
)
{
return
Ability
.
builder
(
)
.
name
(
"test"
)
.
info
(
"hello bot"
)
.
locality
(
ALL
)
.
privacy
(
PUBLIC
)
.
action
(
ctx
->
silent
.
send
(
"hello!"
,
ctx
.
chatId
(
)
)
)
.
build
(
)
;
}
}
Регистрация и его запуск:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
Подключаем зависимость:
Java:
Maven
org
.
telegram
telegrambots
-
abilities
4.1
.2
Gradle
// telegram bot api
compile
'org.telegram:telegrambots:3.6.1'
Дальше пример реализации классов
В классе бота:
Java:
public
class
MyBot
extends
AbilityBot
{
protected
MyBot
(
String
botToken
,
String
botUsername
,
DefaultBotOptions
options
)
{
super
(
botToken
,
botUsername
,
options
)
;
}
/* ... */
}
В регистрации, если прокся без авторизации:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
private
static
String
PROXY_HOST
=
"..."
/* proxy host */
;
private
static
Integer
PROXY_PORT
=
3128
/* proxy port */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Set up Http proxy
DefaultBotOptions
botOptions
=
ApiContext
.
getInstance
(
DefaultBotOptions
.
class
)
;
HttpHost
httpHost
=
new
HttpHost
(
PROXY_HOST
,
PROXY_PORT
)
;
RequestConfig
requestConfig
=
RequestConfig
.
custom
(
)
.
setProxy
(
httpHost
)
.
setAuthenticationEnabled
(
false
)
.
build
(
)
;
botOptions
.
setRequestConfig
(
requestConfig
)
;
botOptions
.
setHttpProxy
(
httpHost
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
,
botOptions
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
С авторизацией:
Java:
public
class
Main
{
private
static
String
BOT_NAME
=
"My test bot"
;
private
static
String
BOT_TOKEN
=
"..."
/* your bot's token here */
;
private
static
String
PROXY_HOST
=
"..."
/* proxy host */
;
private
static
Integer
PROXY_PORT
=
3128
/* proxy port */
;
private
static
String
PROXY_USER
=
"..."
/* proxy user */
;
private
static
String
PROXY_PASSWORD
=
"..."
/* proxy password */
;
public
static
void
main
(
String
[
]
args
)
{
try
{
ApiContextInitializer
.
init
(
)
;
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi
botsApi
=
new
TelegramBotsApi
(
)
;
// Set up Http proxy
DefaultBotOptions
botOptions
=
ApiContext
.
getInstance
(
DefaultBotOptions
.
class
)
;
CredentialsProvider
credsProvider
=
new
BasicCredentialsProvider
(
)
;
credsProvider
.
setCredentials
(
new
AuthScope
(
PROXY_HOST
,
PROXY_PORT
)
,
new
UsernamePasswordCredentials
(
PROXY_USER
,
PROXY_PASSWORD
)
)
;
HttpHost
httpHost
=
new
HttpHost
(
PROXY_HOST
,
PROXY_PORT
)
;
RequestConfig
requestConfig
=
RequestConfig
.
custom
(
)
.
setProxy
(
httpHost
)
.
setAuthenticationEnabled
(
true
)
.
build
(
)
;
botOptions
.
setRequestConfig
(
requestConfig
)
;
botOptions
.
setCredentialsProvider
(
credsProvider
)
;
botOptions
.
setHttpProxy
(
httpHost
)
;
// Register your newly created AbilityBot
MyBot
bot
=
new
MyBot
(
BOT_TOKEN
,
BOT_NAME
,
botOptions
)
;
botsApi
.
registerBot
(
bot
)
;
}
catch
(
TelegramApiException
e
)
{
e
.
printStackTrace
(
)
;
}
}
}
Ну, собсно, все.