cannot run telegram bot with proxy - java

I wrote a robot under the server
And I want to run with a proxy
But I miss the following error
mybot is:
public class StoreBot extends TelegramLongPollingBot {
public StoreBot( DefaultBotOptions botOptions) {
super(botOptions);
}
public int creatorId() {
return 0;
}
public StoreBot() {
}
....
}
mycode for run bot is:
ApiContextInitializer.init();
// Create the TelegramBotsApi object to register your bots
TelegramBotsApi botsApi = new TelegramBotsApi();
// Set up Http proxy
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
botOptions.setProxyHost("2.181.254.198");
botOptions.setProxyPort(8580);
// Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
// Register your newly created AbilityBot
StoreBot bot = new StoreBot(botOptions);
botsApi.registerBot(bot);
when run get this error:
org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException: Error removing old webhook
...
Caused by: org.telegram.telegrambots.meta.exceptions.TelegramApiException: Unable to execute deleteWebhook method
...
Caused by: java.net.SocketException: Connection timed out: connect

RequestConfig requestConfig = RequestConfig.custom()
.setProxy(new HttpHost(host, port))
.build();
DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setRequestConfig(requestConfig);
StoreBot bot = new StoreBot(botOptions);

Related

Testcontainers: communication between containers + mapped outside port

I have such test setup:
MyService connects to PostgtreSQL
MyService endpoint is being called from test suite
Both MyService and PostgreSQL are being run with Testcontainers.
Here is the network schema I want to achieve.
At first I tried to arrange communication by exposing ports.
static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse(POSTGRES_VERSION));
static final GenericContainer<?> myService = new GenericContainer<>(DockerImageName.parse(MY_SERVICE_IMAGE))
.withEnv(
Map.of(
"SPRING_DATASOURCE_URL", postgres.getJdbcUrl(),
"SPRING_DATASOURCE_USERNAME", postgres.getUsername(),
"SPRING_DATASOURCE_PASSWORD", postgres.getPassword()
)
)
.withExposedPorts(8080)
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("MyService")))
According to logs MyService couldn't establish connection to PostgreSQL.
Caused by: java.net.ConnectException: Connection refused
Then I configured both services to share the same network.
static final Network SHARED_NETWORK = Network.newNetwork();
static final PostgreSQLContainer<?> postgres =
new PostgreSQLContainer<>(DockerImageName.parse(POSTGRES_VERSION))
.withNetwork(SHARED_NETWORK)
.withNetworkAliases("postgres");
static final GenericContainer<?> myService = new GenericContainer<>(DockerImageName.parse(MY_SERVICE_IMAGE))
.withEnv(
Map.of(
"SPRING_DATASOURCE_URL", "jdbc:postgresql://postgres:5432/" + postgres.getDatabaseName(),
"SPRING_DATASOURCE_USERNAME", postgres.getUsername(),
"SPRING_DATASOURCE_PASSWORD", postgres.getPassword()
)
)
.withExposedPorts(8080)
.withNetwork(SHARED_NETWORK)
.withNetworkAliases("MyService")
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger("MyService")))
Now MyService has established connection with PostgreSQL successfully. But when I perform HTTP request to MyService from the test suite, I get the same error.
restTemplate.getForObject("http://" + myService.getHost() + ":" + myService.getMappedPort(8080) +"/api/endpoint", Void.class)
Caused by: java.net.ConnectException: Connection refused
My question is how can I setup the containers network to make this architecture work?
You need to specify port bindings to expose a port to the "outside world".
Example similar to what you want:
Network network = Network.newNetwork();
GenericContainer mariaDbServer = getMariaDbContainer(network);
GenericContainer flywayRunner = getFlywayContainer(network);
...
#SuppressWarnings("rawtypes")
private GenericContainer getMariaDbContainer(Network network) {
return new GenericContainer<>("mariadb:10.4.21-focal")
.withEnv(Map.of("MYSQL_ROOT_PASSWORD", "password", "MYSQL_DATABASE", "somedatabase"))
.withCommand(
"mysqld", "--default-authentication-plugin=mysql_native_password", "--character-set-server=utf8mb4",
"--collation-server=utf8mb4_unicode_ci").withNetwork(network).withNetworkAliases("somedatabasedb")
.withNetworkMode(network.getId())
.withExposedPorts(3306).withCreateContainerCmdModifier(
cmd -> cmd.withNetworkMode(network.getId()).withHostConfig(
new HostConfig()
.withPortBindings(new PortBinding(Ports.Binding.bindPort(20306), new ExposedPort(3306))))
.withNetworkMode(network.getId())).withStartupTimeout(Duration.ofMinutes(2L));
}
#SuppressWarnings("rawtypes")
private GenericContainer getFlywayContainer(Network network) {
return new GenericContainer<>("flyway/flyway:7.15.0-alpine")
.withEnv(Map.of("MYSQL_ROOT_PASSWORD", "password", "MYSQL_DATABASE", "somedatabase"))
.withCommand(
"-url=jdbc:mariadb://somedatabasedb -schemas=somedatabase-user=root -password=password -connectRetries=300 migrate")
.withFileSystemBind(Paths.get(".", "infrastructure/database/schema").toAbsolutePath().toString(),
"/flyway/sql", BindMode.READ_ONLY).withNetwork(network).waitingFor(
Wait.forLogMessage(".*Successfully applied.*", 1)
).withStartupTimeout(Duration.of(60, ChronoUnit.SECONDS));
}
Container two communicates with container one using "internal" port.
Container one exposes 20306 (that redirects to 3306) port to the "outside world".

Getting java.lang.IllegalStateException: Connection is not open

I am getting the below error while my code is trying to access a 3rd party endpoint
This request is sent via Service Stub generated code
We are using Axis2 version 1.7.7
Caused by: java.lang.IllegalStateException: Connection is not open
at org.apache.commons.httpclient.HttpConnection.assertOpen(HttpConnection.java:1277)
at org.apache.commons.httpclient.HttpConnection.getResponseInputStream(HttpConnection.java:858)
at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpConnectionAdapter.getResponseInputStream(MultiThreadedHttpConnectionManager.java:1297)
What could be the reason for this and how can we solve this.
Below is the code for reference
public com.test.Token validateToken(com.test.Token actionToken)throws java.rmi.RemoteException
{
org.apache.axis2.context.MessageContext _messageContext = new org.apache.axis2.context.MessageContext();
try{
org.apache.axis2.client.OperationClient _operationClient = _serviceClient.createClient(_operations[0].getName());
_operationClient.getOptions().setAction("http://serviceURL/action");
_operationClient.getOptions().setExceptionToBeThrownOnSOAPFault(true);
addPropertyToOperationClient(_operationClient,org.apache.axis2.description.WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR,"&");
org.apache.axiom.soap.SOAPEnvelope env = null;
env = toEnvelope(getFactory(_operationClient.getOptions().getSoapVersionURI()),
actionToken,
optimizeContent(new javax.xml.namespace.QName("http://serviceURL", "action")),
new javax.xml.namespace.QName("http://serviceURL", "action"));
_serviceClient.addHeadersToEnvelope(env);
_messageContext.setEnvelope(env);
_operationClient.addMessageContext(_messageContext);
_operationClient.execute(true);
org.apache.axis2.context.MessageContext _returnMessageContext = _operationClient.getMessageContext(
org.apache.axis2.wsdl.WSDLConstants.MESSAGE_LABEL_IN_VALUE);
org.apache.axiom.soap.SOAPEnvelope _returnEnv = _returnMessageContext.getEnvelope();
java.lang.Object object = fromOM(
_returnEnv.getBody().getFirstElement() ,
com.test.Token.class);
return (com.test.Token)object;
}

AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started

Im trying to start Appium server programmatically from Java
(OS: Windows7 x64)
using first method from source: http://www.automationtestinghub.com/3-ways-to-start-appium-server-from-java/
The code that I use for starting Appium sever is:
public void startServer() {
//Set Capabilities
cap = new DesiredCapabilities();
cap.setCapability("noReset", "false");
//Build the Appium service
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL, "error");
//added by myself:
builder.usingDriverExecutable(new File("C:/node/node.exe"));
builder.withAppiumJS(new File("C:/Users/[user]/AppData/Roaming/npm/node_modules/appium/lib/appium.js"));
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service.start();
}
I'm getting an exception:
Exception in thread "main" io.appium.java_client.service.local.AppiumServerHasNotBeenStartedLocallyException: The local appium server has not been started. The given Node.js executable: C:\node\node.exe Arguments: [C:\Users\Dima\AppData\Roaming\npm\node_modules\appium\lib\appium.js, --port, 4723, --address, 127.0.0.1, --log-level, error, --session-override, --default-capabilities, {\"noReset\": \"false\"}]
Process output: C:\Users[user]\AppData\Roaming\npm\node_modules\appium\lib\appium.js:1
(function (exports, require, module, __filename, __dirname) { import _ from 'lodash'; ^^^^^^
SyntaxError: Unexpected token import
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:616:28)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
I tried every way to start Appium server from the source, but second one causes to the same, but third causes to error
Any ideas? Thanks to all in advance!
Take a look at official documentation:
https://github.com/appium/java-client/blob/master/docs/The-starting-of-an-app-using-Appium-node-server-started-programmatically.md
Make sure your env setup for Appium is correct and that appium service/client versions are compatible.
It did mistake in code. Fixed method is:
public static String runAppiumService(int appiumPort) {
//Build parameters for appium server:
AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder();
appiumServiceBuilder.usingPort(appiumPort)
.withIPAddress(APPIUM_IP)
.withAppiumJS(new File(getAppiumJsPath()))
.withArgument(GeneralServerFlag.SESSION_OVERRIDE)
.withLogFile(new File(System.getProperty("user.dir") + "/target/resources/appium_server_logs" + Thread.currentThread().getId()));
AppiumDriverLocalService service = AppiumDriverLocalService.buildService(appiumServiceBuilder);
service.start();
}
public class ServerManager {
AppiumDriverLocalService service;
public static void main(String args[]) {
ServerManager sm = new ServerManager();
sm.startServer();
}
public void startServer() {
final DesiredCapabilities cap = new DesiredCapabilities();
//Set Capabilities
cap.setCapability("noReset", "false");
//Build the Appium service
AppiumServiceBuilder builder = new AppiumServiceBuilder();
builder = new AppiumServiceBuilder();
builder.withIPAddress("127.0.0.1");
builder.usingPort(4723);
builder.withCapabilities(cap);
builder.withArgument(GeneralServerFlag.SESSION_OVERRIDE);
builder.withArgument(GeneralServerFlag.LOG_LEVEL,"error");
//Start the server with the builder
service = AppiumDriverLocalService.buildService(builder);
service = AppiumDriverLocalService.buildService(builder);
service.start();
}
public void stopServer() {
service.stop();
}
}

Java Elasticsearch client: throws ConnectException when connecting from local to ES in Google Cloud VM

I have an elasticsearch cluster running in google-compute-engine(VMs), and I am trying to connect from my Java program.
ES instante in the google-cloud has an external-ip & internal-ip.
I have configured the external-ip for connecting, and all the firewall settings are enabled to connect.
Still it seems like, the internal-ip is using somewhere by the client while writing myd data to ES.
10.240.0.237 is the internal-ip
Caused by: java.net.ConnectException: Connection timed out: no further information: /10.240.0.237:9300
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(Unknown Source)
at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.connect(NioClientBoss.java:152)
at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:105)
at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.process(NioClientBoss.java:79)
at org.elasticsearch.common.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:337)
at org.elasticsearch.common.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42)
at org.elasticsearch.common.netty.util.ThreadRenamingRunnable.run(ThreadRenamingRunnable.java:108)
at org.elasticsearch.common.netty.util.internal.DeadLockProofWorker$1.run(DeadLockProofWorker.java:42)
Code
public class ESClient {
final static Node[] esNodes = new Node[1];
static String indexName = "sa-sonarshock-log";
public static Client getClient(){
if(esNodes[0] == null){
esNodes[0] = _setupNode();
}
return esNodes[0].client();
}
public static Node _setupNode(){
Settings settings = ImmutableSettings.settingsBuilder()
.put("cluster.name", "es-tifc-retro-testing")
.put("http.enabled", false)
.put("transport.tcp.port", "9300-9400")
.put("discovery.zen.ping.multicast.enabled", false)
.put("discovery.zen.ping.unicast.hosts", "estifcretrotest-es-tifc-retro-testing-1-vm")
.build();
return nodeBuilder().settings(settings).client(true).node();
}
public static Map<String, Object> putJsonDocument(String title, String content, Date postDate,
String[] tags, String author){
Map<String, Object> jsonDocument = new HashMap<String, Object>();
jsonDocument.put("title", title);
jsonDocument.put("conten", content);
jsonDocument.put("postDate", postDate);
jsonDocument.put("tags", tags);
jsonDocument.put("author", author);
return jsonDocument;
}
public static void main(String[] args) {
Client esClient = getClient();
CreateIndexRequestBuilder createIndexRequestBuilder = esClient.admin().indices().prepareCreate(indexName);
createIndexRequestBuilder.execute().actionGet();
esClient.prepareIndex(indexName, "default", "1")
.setSource(putJsonDocument("ElasticSearch: Java API",
"ElasticSearch provides the Java API, all operations "
+ "can be executed asynchronously using a client object.",
new Date(),
new String[]{"elasticsearch"},
"Remis Haroon")).execute().actionGet();
esNodes[0].close();
}
}
It looks like you are connecting on port 9300, but I think you want port 9200.

Python Server / Java Client "Connection refused: connect"

I am communicating using TCP Socket.
While Working, a problem arises
Client can't make 'Socket' Instance.
Strange Point is that (In Server using Python)
Using 'socket' class in python don't cause problem,
but using 'SocketServer.TCPServer' class in python cause problem
This is my environment.
Server : Python
Client : Java / Many Users will try connect.
Server Code (using Python):
SooMain.py
if name == "main":
server = SooServer('localhost', PORT_DEBUG, SooRequestHandler)
server.serve_forever()
SooServer.py
class SooServer(SocketServer.TCPServer):
"This is Server For Project201201"
def __init__(self,
host='localhost',
port=PORT_DEBUG,
handler=SooRequestHandler):
#SocketServer.ThreadingTCPServer.__init__(self, (host, port), handler)
SocketServer.TCPServer.__init__(self, (host, port), handler)
print "SooServer <State> __init__"
self.abort=0
self.timeout=10
def shutdown(self):
SocketServer.ThreadingTCPServer.shutdown(self)
print "SooServer <State> shutdown"
Client Code (using Java):
TestJava.java
public class TestJava {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
try {
String[] aStrData = {
"Test",
"Test" };
InetAddress m_oInetAddr = InetAddress.getByName(DEBUG_ADDR);
Socket m_oSocket = new Socket(m_oInetAddr, DEBUG_PORT); //This Line Makes Exception!!!!!
PrintWriter out =
new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(m_oSocket.getOutputStream())), true);
int nNumData = aStrData.length;
out.println(Integer.toString(nNumData));
for (int i=0 ; i<nNumData ; i++) {
out.println(aStrData[i]);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Error Code (using Java):
Releated Line : Socket m_oSocket = new Socket(m_oInetAddr, DEBUG_PORT);
java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:241)
at TestJava.main(TestJava.java:51)

Categories

Resources