# Bot can send messages, read from connection, but not receive messages

**URL:** <https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230>\
**Category:** Chat and IRC\
**Created:** [September 28, 2017, 10:26pm UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230 "2017-09-28T22:26:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pilchard123](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Pilchard123](https://discuss.dev.twitch.com/u/Pilchard123)\
**Post date:** [September 28, 2017, 10:26pm UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/1 "2017-09-28T22:26:55Z")

</div>

I have a bot that can connect to chat, and send messages, but not get anything from chat. Does anyone know why not? I’ve tried on my channel and on another, more active one but… nothing.

Output and VB .Net console app code below:

* * *

Output

```
< PASS oauth:TOKEN_GOES_HERE
< NICK pilchard123
< USER pilchard123 8 * :pilchard123
> :tmi.twitch.tv 001 pilchard123 :Welcome, GLHF!
> :tmi.twitch.tv 002 pilchard123 :Your host is tmi.twitch.tv
> :tmi.twitch.tv 003 pilchard123 :This server is rather new
> :tmi.twitch.tv 004 pilchard123 :-
> :tmi.twitch.tv 375 pilchard123 :-
> :tmi.twitch.tv 372 pilchard123 :You are in a maze of twisty passages, all alike.
> :tmi.twitch.tv 376 pilchard123 :>
< JOIN #pilchard123

```

* * *

Code

```
Imports System.IO
Imports System.Net.Sockets

Module Module1

Private Const TOKEN = "TOKEN_GOES_HERE"

Sub Main()

    Try
        MainAsync().GetAwaiter().GetResult()
    Catch ex As Exception
        Debugger.Break()
    End Try

    Debugger.Break()
    Console.WriteLine("Done!")
    Console.ReadLine()
End Sub

Public Async Function MainAsync() As Task

    Using client = New TwitchIrcClient

        client.AddSendListener(AddressOf PrintSentMessage)
        client.AddReceiveListener(AddressOf PrintReceivedMessage)

        client.SendMessage($"PASS oauth:{TOKEN}")
        client.SendMessage($"NICK pilchard123")
        client.SendMessage($"USER pilchard123 8 * :pilchard123")
        client.GetAllMessages()

        client.SendMessage($"JOIN #pilchard123")

        While True
            client.GetAllMessages()
            Await Task.Delay(100)
        End While

    End Using

    Debugger.Break()

    Await Task.Delay(-1)

End Function

Private Sub PrintSentMessage(message As String)
    Console.WriteLine($"< {message}")
End Sub

Private Sub PrintReceivedMessage(message As String)
    Console.WriteLine($"> {message}")
End Sub

End Module

Class TwitchIrcClient
Implements IDisposable

Private tcpClient As TcpClient
Private reader As StreamReader
Private writer As StreamWriter

Private SendListener As New List(Of Action(Of String))
Public Sub AddSendListener(listener As Action(Of String))
    SendListener.Add(listener)
End Sub
Public Sub RemoveSendListener(listener As Action(Of String))
    SendListener.Remove(listener)
End Sub

Private ReceiveListener As New List(Of Action(Of String))
Public Sub AddReceiveListener(listener As Action(Of String))
    ReceiveListener.Add(listener)
End Sub
Public Sub RemoveReceiveListener(listener As Action(Of String))
    ReceiveListener.Remove(listener)
End Sub

Public Sub New()
    tcpClient = New TcpClient("irc.chat.twitch.tv", 6667)
    reader = New StreamReader(tcpClient.GetStream)
    writer = New StreamWriter(tcpClient.GetStream)
End Sub

Public Sub SendMessage(message As String)
    writer.Flush()
    writer.WriteLine(message)
    writer.Flush()
    For Each l In SendListener
        l(message)
    Next
End Sub

Public Function HasMessage() As Boolean
    Return reader.Peek() >= 0
End Function

Public Function GetNextMessage() As String
    If HasMessage() Then
        Dim message = reader.ReadLine()
        For Each l In ReceiveListener
            l(message)
        Next l
        If message = "PING :tmi.twitch.tv" Then
            SendMessage("PONG :tmi.twitch.tv")
        End If
        Return message
    Else
        Return Nothing
    End If
End Function

Public Function GetAllMessages() As IEnumerable(Of String)
    Dim retVal = New List(Of String)
    While HasMessage()
        retVal.Add(GetNextMessage)
    End While
    Return retVal
End Function

Private disposedValue As Boolean

Protected Overridable Sub Dispose(disposing As Boolean)
    If Not disposedValue Then
        If disposing Then
            reader.Dispose()
            writer.Dispose()
            tcpClient.Close()
        End If
    End If
    disposedValue = True
End Sub

Public Sub Dispose() Implements IDisposable.Dispose
    Dispose(True)
End Sub
End Class
```

---

<div class="post-metadata">

**Author:** ![tduva](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/tduva/32/392_2.png) [@tduva](https://discuss.dev.twitch.com/u/tduva)\
**Post date:** [September 29, 2017, 1:54am UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/2 "2017-09-29T01:54:18Z")

</div>

Sorry I don’t really know the programming language, but be careful about posting oauth tokens publicly (in your log output), they need to be kept private. I suggest you revoke access to that immediately if it’s valid.

---

<div class="post-metadata">

**Author:** ![Pilchard123](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Pilchard123](https://discuss.dev.twitch.com/u/Pilchard123)\
**Post date:** [September 29, 2017, 6:59am UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/3 "2017-09-29T06:59:48Z")

</div>

And I was so careful to remove it from the code as well…

Thanks for the heads up!

---

<div class="post-metadata">

**Author:** ![BarryCarlyon](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/barrycarlyon/32/253_2.png) [@BarryCarlyon](https://discuss.dev.twitch.com/u/BarryCarlyon)\
**Post date:** [September 29, 2017, 11:08am UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/4 "2017-09-29T11:08:17Z")

</div>

When you send are you doing

```
PRIVMSG #room words to send

```

Syntax above wrong as on mobile. But I see no PRIVMSG in your code

---

<div class="post-metadata">

**Author:** ![Pilchard123](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Pilchard123](https://discuss.dev.twitch.com/u/Pilchard123)\
**Post date:** [September 29, 2017, 5:27pm UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/5 "2017-09-29T17:27:59Z")

</div>

Yeah, I use PRIVMSG to send. That works okay; the message shows up fine on the website. I just included the minimum code to reproduce the behaviour.

Or lack of it, as the case may be.

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [October 29, 2017, 5:28pm UTC](https://discuss.dev.twitch.com/t/bot-can-send-messages-read-from-connection-but-not-receive-messages/12230/6 "2017-10-29T17:28:03Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
