from twitchAPI.twitch import Twitch
from twitchAPI.chat import Chat
from twitchAPI.helper import first
from twitchAPI.eventsub.webhook import EventSubWebhook
from twitchAPI.object.eventsub import ChannelFollowEvent, ChannelChatMessageEvent
from twitchAPI.oauth import UserAuthenticator
from twitchAPI.type import AuthScope
from loguru import logger
import asyncio
EVENTSUB_URL = '#'
APP_ID = '#'
APP_SECRET = '#'
TARGET_SCOPES = [AuthScope.CHANNEL_BOT, AuthScope.USER_WRITE_CHAT]
mod_id = "#"
br_id = "#"
async def on_follow(data: ChannelFollowEvent):
print(f'{data.event.user_name} now follows {data.event.broadcaster_user_name}!')
async def on_message(data: ChannelChatMessageEvent):
print("get message")
async def eventsub_webhook_example():
logger.info("start twitch")
try:
twitch = await Twitch(APP_ID, APP_SECRET)
logger.info("success twitch")
except Exception as e:
logger.error(e)
logger.info("start auth")
try:
token = "#"
refresh_token = "#"
await twitch.set_user_authentication(token, TARGET_SCOPES, refresh_token)
logger.info("end auth")
except Exception as e:
logger.error(e)
eventsub = EventSubWebhook(EVENTSUB_URL, 80, twitch)
await eventsub.unsubscribe_all()
eventsub.start()
await eventsub.listen_channel_follow_v2(br_id, mod_id, on_follow)
await eventsub.listen_channel_chat_message(br_id, mod_id, on_message)
await twitch.send_chat_message(br_id, mod_id, "bot start")
try:
input('press Enter to shut down...')
finally:
await eventsub.stop()
await twitch.close()
print('done')
asyncio.run(eventsub_webhook_example())
in chatters list bot has badge “chat bot“
but when i send with “await twitch.send_chat_message(br_id, mod_id, “bot start”)“ i get message without badge
EventSub is unrelated to sending chat messages it’s just for listening.
To send a chat message you need to use the Send Chat Message endpoint, with an App access token (so this can only be done server-side, with a confidential app). You also need to have the chatting user granting the user:bot scope to your app, and either that user be a moderator in the channel you’re sending messages to or the channel granting your app the channel:bot scope.
Because you’re not getting the badge when you’re sending messages, you’re likely using a User Token, rather than App token.
but with when I try to send a message
{‘error’: ‘Unauthorized’, ‘status’: 401, ‘message’: ‘The sender must have authorized the app with the user:write:chat and user:bot scopes.’}
First you get an oAuth token from the broadcaster with channel:bot
Then you get an oAuth token from the account that is the bot with user:botand the relevant chat scopes: user:write:chat
Store both user tokens if needed.
THEN you generate an app access token and use that with Send Chat. (note the scopes listed as minimal and you may need more for other operations) (My Reference Documenation this is for eventsub but the logic applies on how authentication operations, just for send chat needs different/extra scope(s))
Conceptually you already did some of these steps to get eventsub working (with the chat bot tag in the user list) as that requires channel:bot/user:bot from the various users.
The Send Chat API uses “prior user authentication” to allow access to send (in the same way as doing prior auth allows eventsub conduits or webhooks to allow type/topic access as that uses an app access token also)
For app tokens, you just generate a new one when current one expires.
As for combining EventSub and IRC, there’s nothing stopping you connecting to and using both services, how you ‘combine’ them will be entirely up to you within your app.