Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting socks5 proxy for long polling bot #57

Closed
Bartosso opened this issue Apr 19, 2018 · 2 comments
Closed

Setting socks5 proxy for long polling bot #57

Bartosso opened this issue Apr 19, 2018 · 2 comments

Comments

@Bartosso
Copy link

Hello! It is possible to set socks5 proxy for long polling bot? And how it can be done if it is?

@mukel
Copy link
Member

mukel commented Apr 19, 2018

Sorry for the late reponse, I just added SOCKS support to the ScalajHttpClient, there's a big update coming soon, check the refactor branch.

A proxied bot (via long polling) would look like this:

import java.net.{InetSocketAddress, Proxy}

import info.mukel.telegrambot4s.api.Polling
import info.mukel.telegrambot4s.api.declarative.Commands
import info.mukel.telegrambot4s.clients.ScalajHttpClient

/**
  * Tunnel the bot through a SOCKS proxy.
  *
  * To test locally via [[https://linux.die.net/man/8/sshd sshd]] use:
  *   ssh -D 1337 -C -N you_user_name@localhost
  */
class ProxyBot(token: String) extends ExampleBot(token)
  with Polling
  with Commands {

  val proxy = new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved("localhost", 1337))
  override val client = new ScalajHttpClient(token, proxy)

  onCommand('hello) { implicit msg =>
    reply("Hi " + msg.from.fold("Mr. X")(_.firstName))
  }
}

See instructions below on how to get the latest build (with proxy support).

@mukel
Copy link
Member

mukel commented Apr 20, 2018

To pull 3.1.0-RC1 please add to your build.sbt

resolvers += Resolver.sonatypeRepo("staging")

// Core with Scalaj-Http backend: TelegramBot + Polling; it just works out-of-the box.
libraryDependencies ++= Seq("info.mukel" %% "telegrambot4s-core" % "3.1.0-RC1")

// Extensions with AkkHttp backend: AkkaTelegramBot + Webhooks + Game support
libraryDependencies ++= Seq("info.mukel" %% "telegrambot4s-akka" % "3.1.0-RC1")

Example bot + runner:

import info.mukel.telegrambot4s.api.{Polling, TelegramBot}
import info.mukel.telegrambot4s.api.declarative.Commands

import scala.concurrent.Await
import scala.concurrent.duration.Duration

class Botogram extends TelegramBot with Polling with Commands {
  override def token: String = "TOKEN"
  onCommand('hello) { implicit msg =>
    reply("Botogram")
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val bot = new Botogram()
    val eol = bot.run()
    scala.io.StdIn.readLine()
    bot.shutdown()
    Await.result(eol, Duration.Inf)
  }
}

Please report any issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants