pIRCbot won't join Twitch channel or send messages - java

The following code connects to Twitch's IRC successfully and triggers the onConnect method which is where everything stops.
Because pIRCbot has verbose enabled I can see that it does send JOIN #twitchplayspokemon after connecting but the specification says that I should get an immediate response with the same text followed by a list of users (which would trigger the onJoin and onUserList methods) which doesn't happen.
I have also tried connecting to my channel and sending a message with
joinChannel("#[mychannelname]");
sendMessage("#[mychannelname]", "Hello World");
All this does is send two JOIN commands and doesn't ever join nor message appear in chat.
The tutorial/reference I'm using is http://help.twitch.tv/customer/portal/articles/1302780-twitch-irc
Other responses are also not being output. I am getting the MOTD but not seeing the "End of /MOTD command".
import org.jibble.pircbot.*;
public class MyBotMain extends PircBot {
public static void main(String[] args) throws Exception {
MyBotMain bot = new MyBotMain();
bot.setVerbose(true);
bot.setName("[myname]");
bot.setLogin("[myname]");
try {
bot.connect("irc.twitch.tv", 6667, "oauth:db4aai4mh474ikbgzzuh76fv67n"); // Not the key I'm using
} catch (NickAlreadyInUseException e) {
System.err.println("Nickname is currently in use");
} catch (IrcException e) {
System.err.println("Server did not accept connection");
e.printStackTrace();
}
}
#Override
protected void onConnect() {
System.out.println("Connected!");
joinChannel("#witchplayspokemon");
super.onConnect();
}
#Override
protected void onJoin(String channel, String sender, String login, String hostname) {
System.out.println(login + " joined channel " + channel);
super.onJoin(channel, sender, login, hostname);
}
#Override
protected void onUserList(String channel, User[] users) {
for (User user : users) {
System.out.println(user);
}
super.onUserList(channel, users);
}
}

Your code works perfectly fine, the only problem is that the channel you are trying to join is "witchplayspokemon" instead of "twitchplayspokemon" the onUserList(String channel, User[] users) is only giving me one user, But I have been having that problem recently on my own Bot So I am not yet sure on the cause.

Related

FileWriter closes websocket connection?

I am new to server programming and websockets and I've learnt a little bit of Java 8 this year. In school we had a project where a client webpage opens your webcam, takes a photo of a barcode and then shows a photo and the nutritional value of said product. You can also just send a raw barcode number and that is what is done in this example
My side of the project was to implement a java websocket server (the backend) using the glassfish tyrus library, then receiving the number of the barcode in string format and making a request to openfoodfacts.org using their api. Finally I parsed the json file and sent it back as string format so the client app can read the string and show the correct information (product name, image url, etc)
My code is organized into two files, Serveur.java establishes a websocket server for the client to connect to and ProduitApi.java gets the information from openfoodfacts.org with the given barcode from the client.
public class Serveur {
#javax.websocket.server.ServerEndpoint(value = "/websocket")
public static class EndPoint {
#javax.websocket.OnClose
public void onClose(javax.websocket.Session session, javax.websocket.CloseReason close_reason) {
System.out.println("onClose: " + close_reason.getReasonPhrase());
}
#javax.websocket.OnError
public void onError(javax.websocket.Session session, Throwable throwable) {
System.out.println("onError: " + throwable.getMessage());
}
#javax.websocket.OnMessage
public void onMessage(javax.websocket.Session session, String message) {
System.out.println("Message from client: " + message);
//Creation du produit avec le message du client
try {
ProduitApi produit = new ProduitApi(message);
session.getBasicRemote().sendText(produit.print());
} catch (Exception e) {
e.printStackTrace();
}
}
#javax.websocket.OnOpen
public void onOpen(javax.websocket.Session session, javax.websocket.EndpointConfig ec) throws java.io.IOException {
System.out.println("OnOpen... " + ec.getUserProperties().get("Author"));
session.getBasicRemote().sendText("{\"Handshaking\": \"Yes\"}");
}
}
public static void main(String[] args) {
Server server;
server = new Server ("localhost", 8025, "/BetterFood", null, EndPoint.class);
try {
server.start();
System.out.println("--- server is running");
System.out.println(java.nio.file.FileSystems.getDefault().getPath("client") );
System.out.print("Please press a key to stop the server.");
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
reader.readLine();
} catch (Exception e) {
e.printStackTrace();
} finally {
server.stop();
}
}
}
as you can see, when I receive the 'barcode' message, onMessage() gets called. There I instantiate an object of class ProduitApi to use the barcode to then return the information
This is my ProduitApi file without some unnecessary details
package com.gabi.serveur;
/**
*
* #author gabriel
*/
[imports]
public class ProduitApi {
private java.lang.String barcode;
final private java.net.URL url;
private java.net.URLConnection connection;
JsonObjectBuilder constructeur_objet = Json.createObjectBuilder();
String string_json;
ProduitApi(java.lang.String barcode)throws MalformedURLException, IOException {
this.barcode = barcode;
this.url = new java.net.URL("http://world.openfoodfacts.org/api/v0/product/" + this.barcode + ".json");
connection = url.openConnection();
stream();
}
public void stream() throws IOException{
if (connection != null) {
java.io.InputStreamReader response = new java.io.InputStreamReader(connection.getInputStream());
javax.json.stream.JsonParser parser=javax.json.Json.createParser(response);
while (parser.hasNext()) {
[parsing inputStream into JsonObject]
}
public String print()throws IOException{
string_json = constructeur_objet.build().toString();
System.out.print(string_json);
//FileWriter file = new FileWriter("serveur/src/main/java/com/gabi/serveur/json/final.json");
//file.write(string_json);
//file.close();
return string_json;
}
}
My problem comes from the last function ProduitApi.print() , it is supposed to return the parsed json in string form so I can send it via the sendText() as well as printing the result into my console so I can see if everything went right. As you can see there are somme commented lines; The FileWriter object that I had created was used with the purpose of writing said string to a file in my pc and let me check inside.
HOWEVER
and here is what I don't understand, If I uncomment those lines so that the print function can also write the file to my drive, The Connection Closes and then Opens again
It can be seen in the console where after printing the json string, it prints OnClose, followed by OnOpen Signaling the connection was reset for some reason.
If I remove the FileWriter section, the connection works normally, the client's connection stays open and he can make another request
End of Console Message after request:
...cuits x22 biscuits fourrés - 304g","qte":"304 g","img":"https://images.openfoodfacts.org/images/products/800/050/031/0427/front_fr.177.400.jpg"}onClose: OnOpen... null...
Finally, my question is just why writing to a file makes my program behave this way (resetting the connection). Does it have anything to do with how streams work?
I accidentally commented the filewriter portion when another Ide said it didn't find the file because I had opened the project from a different directory.

How to access the payload of the message arrived of the callback method (messageArrived) in the main method Eclipse Paho?

Problem statement:- I am trying to automate a MQTT flow, for that I a need to publish and subscribe to multiple topics but in a sequential order. The trick part is that the message received from the first publish has some value which will be passed in the next sub/pub commands.
For eg.
Sub to topicA/abc
Pub to topicA/abc
Message received on topicA/abc is xyz
sub to topic topicA/xyz
pub to topic topicA/xyz
I am able to receive the message on the first topic but I am not getting how to access the payload of the received message in the main method and pass and attach it to the next topic for next sub.
Is there a way to get the retrieved the message payload from messageArrived callback method to the main method where is client instance is created?
Note:- I am using a single client for publish and subscribe.
kindly help me out as I have ran out of options and methods to do so.
Edited:-
Code snippet
Main class
public class MqttOverSSL {
String deviceId;
MqttClient client = null;
public MqttOverSSL() {
}
public MqttOverSSL(String deviceId) throws MqttException, InterruptedException {
this.deviceId = deviceId;
MqttConnection mqttConObj = new MqttConnection();
this.client = mqttConObj.mqttConnection();
}
public void getLinkCodeMethod() throws MqttException, InterruptedException {
client.subscribe("abc/multi/" + deviceId + "/linkcode", 0);
publish(client, "abc/multi/" + deviceId + "/getlinkcode", 0, "".getBytes());
}
}
Mqtt Claback impl:-
public class SimpleMqttCallBack implements MqttCallback {
String arrivedMessage;
#Override
public void connectionLost(Throwable throwable) {
System.out.println("Connection to MQTT broker lost!");
}
#Override
public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
arrivedMessage = mqttMessage.toString();
System.out.println("Message received:\t" + arrivedMessage);
linkCode(arrivedMessage);
}
#Override
public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
System.out.println("Delivery complete callback: Publish Completed "+ Arrays.toString(iMqttDeliveryToken.getTopics()));
}
public void linkCode(String arrivedMessage) throws MqttException {
System.out.println("String is "+ arrivedMessage);
Gson g = new Gson();
GetCode code = g.fromJson(arrivedMessage, GetCode.class);
System.out.println(code.getLinkCode());
}
}
Publisher class:-
public class Publisher {
public static void publish(MqttClient client, String topicName, int qos, byte[] payload) throws MqttException {
String time = new Timestamp(System.currentTimeMillis()).toString();
log("Publishing at: "+time+ " to topic \""+topicName+"\" qos "+qos);
// Create and configure a message
MqttMessage message = new MqttMessage(payload);
message.setQos(qos);
// Send the message to the server, control is not returned until
// it has been delivered to the server meeting the specified
// quality of service.
client.publish(topicName, message);
}
static private void log(String message) {
boolean quietMode = false;
if (!quietMode) {
System.out.println(message);
}
}
}
OK, it's a little clearer what you are trying to do now.
Short answer No, you can not pass values back to the "main method". MQTT is asynchronous that means you have no idea when a message will arrive for a topic you subscribe to.
You need to update your code to deal check what the incoming message topic is and then deal do what ever action you wanted to do with that response in the messageArrived() handler. If you have a sequence of task to do then you may need to implement what is known as a state machine in order to keep track of where you are in the sequence.

Creating a Bot for twitch chat. Getting a connection Error

I'm trying to make a bot for my twitch chat but i'm having some problems to connect to the chat with it.
I read a bit about the library at:
http://www.jibble.org/pircbot.php
And tried to connect to my own chat but i'm getting an error.
1462989951913 *** Connected to server.
1462989951915 >>>PASS oauth:cencoring oath.
1462989951915 >>>NICK FredsBot
1462989951915 >>>USER FredsBot 8 * :PircBot 1.5.0 Java IRC Bot - www.jibble.org
1462989952324 :tmi.twitch.tv NOTICE * :Error logging in
1462989962324 *** Logged onto server.
Connected!
1462989962324 >>>JOIN #mychannel
1462989962324 *** Disconnected.
As you can see i'm getting an error while trying to login, but it does print out the connected message and after it disconnects.
So far i've tried different oath keys without any luck. Have anyone stumble across this problem?
Heres my code:
import org.jibble.pircbot.*;
public class MyBot extends PircBot {
private static final String channelName = "#mychannel";
private final String botName = "FredsBot";
public MyBot() {
this.setName(botName);
this.setLogin(botName);
}
public String getchannelName() {
return channelName;
}
#Override
public void onMessage(String channel, String sender,
String login, String hostname, String message) {
if (message.equalsIgnoreCase("time")) {
String time = new java.util.Date().toString();
sendMessage(channel, sender + ": The time is now " + time);
}
}
#Override
protected void onConnect() {
System.out.println("Connected!");
joinChannel(channelName);
super.onConnect();
sendMessage(getchannelName(), "Hello, i am a bot");
}
My main looks like this
private static final String OAUTH = "myoath";
private static final String ADRESS = "irc.chat.twitch.tv.";
private static final int PORT = 6667;
public static void main(String[] args) {
MyBot bot = new MyBot();
bot.setVerbose(true);
try {
bot.connect(ADRESS, PORT, OAUTH);
// bot.onMessage(channelName, "Bot", channelName, channelName, channelName);
} catch (IOException ex) {
Logger.getLogger(MainFile.class.getName()).log(Level.SEVERE, null, ex);
} catch (IrcException ex) {
Logger.getLogger(MainFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
i've just cencored my oath and channel name so dont mind that. I did read up on similar post but most of them were solved just but getting a new oath key and it did not help me.
So i found out what the problem was. I had no idea that i needed to create an account for the bot with the exact name which i had declared in my code.
Silly mistake.

Pusher: Decrease timeout for connection state

I'm currently experimenting with websockets using the Pusher library for Java.
Pusher automatically changes its connection state from CONNECTED to DISCONNECTED if the internet connection is lost. However, this only seems to happen after 150 seconds of being disconnected. This is very unfortunate as in those 150s, a lot of messages can get lost, and a de facto old message can still be seen as the most up-to-date.
How can I know if the last received message is the most up-to-date? Or is there any way to decrease the timeout for the connection state?
Here is the pusher code I'm using:
import com.pusher.client.Pusher;
import com.pusher.client.channel.Channel;
import com.pusher.client.channel.ChannelEventListener;
import com.pusher.client.channel.SubscriptionEventListener;
import com.pusher.client.connection.ConnectionEventListener;
import com.pusher.client.connection.ConnectionState;
import com.pusher.client.connection.ConnectionStateChange;
public class Testing {
public static void main(String[] args) throws Exception {
// Create a new Pusher instance
Pusher pusher = new Pusher("PusherKey");
pusher.connect(new ConnectionEventListener() {
#Override
public void onConnectionStateChange(ConnectionStateChange change) {
System.out.println("State changed to " + change.getCurrentState() +
" from " + change.getPreviousState());
}
#Override
public void onError(String message, String code, Exception e) {
System.out.println("There was a problem connecting!");
}
}, ConnectionState.ALL);
// Subscribe to a channel
Channel channel = pusher.subscribe("channel", new ChannelEventListener() {
#Override
public void onSubscriptionSucceeded(String channelName) {
System.out.println("Subscribed!");
}
#Override
public void onEvent(String channelName, String eventName, String data) {
System.out.println("desilo se");
}
});
// Bind to listen for events called "my-event" sent to "my-channel"
channel.bind("my-event", new SubscriptionEventListener() {
#Override
public void onEvent(String channel, String event, String data) {
System.out.println("Received event with data: " + data);
}
});
while(true){
try {
Thread.sleep(1000);
} catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}
}
}
Just found the answer: Initiate Pusher-object with PusherOptions-object.
Here is the PusherOptions-class: http://pusher.github.io/pusher-java-client/src-html/com/pusher/client/PusherOptions.html
Here is a simple example how I decreased my connection-timeout from 150s to 15s:
// Define timeout parameters
PusherOptions opt = new PusherOptions();
opt.setActivityTimeout((long)10000L);
opt.setPongTimeout((long)5000L);
// Create a new Pusher instance
Pusher pusher = new Pusher(PUSHER_KEY, opt);
ActivityTimeout defines how often a ping is sent out to check the connectivity, PongTimeout defines the waiting time until a response from the ping-signal is expected.
The minimum ActivityTimeout is 1000ms, however such a low value is strongly discouraged by Pusher, probably to decrease the server-traffic.

How to read recieved messages in j2me midlet using pushregistry?

i don't know how to read received messages in mobile device using J2ME midlet.Actually I have been sending the messages to other mobiles using SMS gateway.The sms gateway reply to the same mobile device,but i want to read the reply message on the device directly, not going to check the inbox.how to do this in j2me midlet using the PushRegistry concept.please give me the good idea or sample code for me...thanks in advance.
You should use the PushRegistry mechanism for this. In order to do so, you should mark in the .jad file that your application reacts to incoming SMS and also mark the SMS permission.
Put these properties to the JAD file:
MIDlet-Push-1: sms://:10214,hu.bute.daai.example.sms.MidletSMSPushDemo,*
MIDlet-Permissions: javax.microedition.io.PushRegistry, javax.microedition.io.Connector.sms, javax.wireless.messaging.sms.receive
Please note that you should use your own package and MIDlet name. In addition to that you should use the same port for SMS sending as it was defined in the JAD (10214 in the example).
After that when your MIDlet starts, you should call your SMS receiver code to get the SMS:
public class MidletJSMSProxy extends MIDlet {
public void startApp() {
initalize();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void initSMSReceive() {
new Thread() {
public void run() {
MessageConnection conn = null;
try {
String url = "sms://:10214";
MessageConnection conn = (MessageConnection) Connector.open(url);
TextMessage message = (TextMessage)conn.receive();
System.out.println("SMS: "+message.getAddress()+" - "+message.getPayloadText());
}
catch(Exception e){
e.printStackTrace();
} finally {
try {
if (conn != null)
conn.close();
} catch(Exception e){
e.printStackTrace();
}
}
}
}.start();
}
}
More info:
http://www.developer.nokia.com/Community/Wiki/How_to_launch_a_MIDlet_remotely_via_SMS_or_locally_via_an_Alarm_with_PushRegistry_in_Java_ME

Categories

Resources